rclark1850 said:
Sorry, I have both list boxes and combo boxes. Would the code be similar
for
a list box?
The code would be the same for a single-select list box, which lets you
select only one value at a time. If it's a multiselect list box, though,
the code has to allow for multiple values. Here's an example of how to do
that. This example assumes that the field to be filtered on is numeric.
'------ start of code for multiselect list box ------
Private Sub cmdReport_Click()
Dim strCriteria As String
Dim varItem As Variant
With Me.lstYourListbox
If .ItemsSelected.Count = 0 Then
MsgBox "Please select something first."
Me.lstYourListbox.SetFocus
Else
' Build a comma-separated list of selected values.
For Each varItem In .ItemsSelected
strCriteria = strCriteria & "," & .ItemData(varItem)
Next varItem
' Drop leading comma.
strCriteria = Mid(strCriteria, 2)
If .ItemsSelected.Count = 1 Then
strCriteria = "YourFieldName = " & strCriteria
Else
strCriteria = "YourFieldName In (" & strCriteria & ")"
End If
DoCmd.OpenReport "rptYourReport", _
acViewPreview, _
WhereCondition:=strCriteria
End If
End With
End Sub
'------ end of code ------
In the above,
cmdReport = the name of your button
lstYourListbox = the name of your list box
rptYourReport = the name of the report
YourFieldName = the name of the field (in the report's
recordsource) that you want to
filter by
Again, if the field to be filtered by is not numeric, the code has to be
modified to surround each value with quotes. Here's the relevant amended
line:
strCriteria = strCriteria & "," & _
Chr(34) & .ItemData(varItem) & Chr(34)