I have a form in which I have a multiselect list box that contains a list of customer names. I am want to be able to select multiple customers and press a command button to run the query and pass the list box items along with it.
I found this bit of code and tried to make it work but I cannot get it to work. Anyone have any ideas on how to get this to work?
I found this bit of code and tried to make it work but I cannot get it to work. Anyone have any ideas on how to get this to work?
Code:
Private Sub Command2_Click()
Dim sWhere As String ' Where condition
Dim lst As ListBox ' multiselect list box
Dim vItem As Variant ' items in listbox
Dim iLen As Integer ' length of string.
'Set lst = Me!lstSource
Set lst = Me!List0
'loop through all items in listbox
For Each vItem In lst.ItemsSelected
If Not IsNull(vItem) Then
sWhere = sWhere & "(custNAME = """ & lst.ItemData(vItem) & """) OR"
End If
Next
iLen = Len(sWhere) - 4 ' Without trailing " OR ".
If iLen = 0 Then
sWhere = "(" & Left$(sWhere, iLen) & ")"
End If
'DoCmd.OpenReport "MyReport", acViewPreview, , sWhere
DoCmd.OpenQuery "qry_GetRevenue (2/2)"
Set lst = Nothing
End Sub