Susie said:
Thanks for your help Rick. Can you send me the code to choose more
than 1 category (your first response). I'll try my hand at it and
take a chance. Thank you so much for your help.
A bit more involved than you might think. The problem is that even after
you use code to extract the values selected in the ListBox it is not easy to
retrieve them in a fashion that your query can do anything with. The
simplest way I could think of was this...
In a standard module create a public variable and a function that returns
its value...
Public SelectedColors As String
Public Function GetSelectedColors() As String
GetSelectedColors = SelectedColors
End Function
Modify your query criteria to...
WHERE InStr(1,GetSelectedColors(),[ColorField]) > 0
OR GetSelectedColors() = ""
In your dialog form I assume you have a button that opens the report. Its
code will need to look like...
Private Sub CommandButton_Click()
SelectedColors = ""
Dim VarItem As Variant
For Each VarItem In Me.ColorList.ItemsSelected
SelectedColors = SelectedColors & ", " & Me.ColorList.Column(0,
VarItem)
Next VarItem
DoCmd.OpenReport "ReportName", acViewPreview
End Sub
How the above works...
The button will loop through the selections in the ListBox and store them as
a comma separated list in the public variable named SelectedColors. EX:
"Red, Blue, Black"
A variable cannot be used directly in a query, but a user-define function
can be which is why we created a function that does nothing more than return
the value of the variable.
The InStr() part of the query will return a non-zero value for any row where
the color field is contained within the SelectedColors variable. If no
selections were made the value of GetSelectedColors will be a zero length
string "" which will cause the query to return all rows.
The criteria of InStr() is not very efficient but the alternative is to use
code to actually construct the SQL for the query based on the selections and
then construct a proper IN() clause which would be more efficient.