This is rather tricky. I've found that the easiest way is to use the IN
statement, so your WHERE clause would look like "field IN (item1, item2...)"
I have a standard function I use for just this purpose. The function
requires a string and listbox input, and returns a string of all the selected
items in the listbox, separated by commas. I then use this string with the IN
statement in a WHERE clause.
#######################################
BEGIN CODE
#######################################
Public Function buildList(strString As String, lstList As ListBox) As String
Dim i As Integer
For i = 0 To lstList.ListCount - 1
If lstList.Selected(i) Then
If strString = "" Then
strString = Chr$(39) & lstList.ItemData(i) & Chr$(39) 'use
me for text
strString = lstList.ItemData(i) 'use me for numeric
Else
strString = strString & "," & Chr$(39) & lstList.ItemData(i)
& Chr$(39) 'use me for text
strString = strString & "," & lstList.ItemData(i) 'use me
for numeric
End If
End If
Next i
buildList = strString
End Function
##################################
Note that in the above code I've included two assignment statements in both
the if and the else -- the ones with Chr$(39) will insert quotes around the
ItemData -- use this version if the listbox contains text. Note that I use
the Chr$(39) function to assign quotes around the listbox text just in case
the text itself will contain quotes.
Use the other version for numeric data.
I then call it like this
dim strS as string, strT as string
strS = "SELECT * FROM tbl "
strS = strS & "WHERE fld IN (" & buildList(strT, myListbox) & ")"
If you have other criteria to go into the WHERE clause, you can keep
appending ANDs and ORs.
I've used this code over and over on all kinds of forms, and it works slick.
Let me know if anything isn't clear.