B
Barkley via AccessMonster.com
I have finally been able to create a form with a multi-select list box and a
command button where a click of the command button opens a query with the
selections in the multi-select list box as its criteria. So far so good...
now I am wanting to use more than one multi-select list box as the criteria
for the query.
The code I used to successfully run a query off a multi-select list box is as
follows (with Command6 being the name of my command button, TESTQ being the
name of my parameter query, MyTable being the name of the table my list box
gets its values from, MyTableID the name of the field my list box gets its
values from, and List4 being the name of my multi-select list box):
Private Sub Command6_Click()
On Error GoTo Err_Handler
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("TESTQ")
For Each varItem In Me!List4.ItemsSelected
strCriteria = strCriteria & "MyTable.MyTableID = " & Chr(34) _
& Me!List4.ItemData(varItem) & Chr(34) & "OR "
Next varItem
strCriteria = Left(strCriteria, Len(strCriteria) - 3)
strSQL = "SELECT * FROM MyTable " & _
"WHERE " & strCriteria & ";"
qdf.SQL = strSQL
DoCmd.OpenQuery "TESTQ"
Set db = Nothing
Set qdf = Nothing
Exit_Handler:
Exit Sub
Err_Handler:
MsgBox Err.Number & " " & Err.Description
Resume Exit_Handler
End Sub
Given the above code, does anyone know how I can add more than one multi-
select list box to use as criteria for my parameter query?
All suggestions are appreciated…
Thanks,
Barkley
command button where a click of the command button opens a query with the
selections in the multi-select list box as its criteria. So far so good...
now I am wanting to use more than one multi-select list box as the criteria
for the query.
The code I used to successfully run a query off a multi-select list box is as
follows (with Command6 being the name of my command button, TESTQ being the
name of my parameter query, MyTable being the name of the table my list box
gets its values from, MyTableID the name of the field my list box gets its
values from, and List4 being the name of my multi-select list box):
Private Sub Command6_Click()
On Error GoTo Err_Handler
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("TESTQ")
For Each varItem In Me!List4.ItemsSelected
strCriteria = strCriteria & "MyTable.MyTableID = " & Chr(34) _
& Me!List4.ItemData(varItem) & Chr(34) & "OR "
Next varItem
strCriteria = Left(strCriteria, Len(strCriteria) - 3)
strSQL = "SELECT * FROM MyTable " & _
"WHERE " & strCriteria & ";"
qdf.SQL = strSQL
DoCmd.OpenQuery "TESTQ"
Set db = Nothing
Set qdf = Nothing
Exit_Handler:
Exit Sub
Err_Handler:
MsgBox Err.Number & " " & Err.Description
Resume Exit_Handler
End Sub
Given the above code, does anyone know how I can add more than one multi-
select list box to use as criteria for my parameter query?
All suggestions are appreciated…
Thanks,
Barkley