How can i create a query based on two column listbox, i have copied a public code wherein you can create
a query but only based on 1 column inside the listbox,
here is the code: this is only for 1 selection "fiscal
years", i want to have a selection of fiscal years and budget type, i dont know how to incorporate in the code
Private Sub ForecastButton_Click()
On Error GoTo Err_forecastButton_Click
Dim MyDB As dao.Database
Dim qdef As dao.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM [Order Entry File Modified
'Build the IN string by looping through the listbox
For i = 0 To Forecastyears.ListCount - 1
If Forecastyears.Selected(i) Then
If Forecastyears.Column(0, i) = "All" Then
flgSelectAll = True
strIN = strIN & "'" & Forecastyears.Column(0, i) & "',"
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE [fiscal year] in (" & Left(strIN, Len(strIN) - 1) & ")"
'If "All" was selected in the
listbox, don't add the WHERE condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
MyDB.QueryDefs.Delete "Forecast Table source"
Set qdef = MyDB.CreateQueryDef("Forecast Table source", strSQL)
'Open the query, built using the IN clause to
set the criteria
'DoCmd.OpenQuery "Standard Table source", acViewNormal
'Clear listbox selection after running query
For Each varItem In Me.Forecastyears.ItemsSelected
Me.Forecastyears.Selected(varItem) = False
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list", , "Selection Required !"
'Write out the error and exit the sub
Can you help me on this?