Private Sub cmdFilter_Click()
Dim varItem As Variant, strWhere As String
' Initialise to zero length string
strWhere = ""
With SearchPostcode
If .ItemsSelected.Count <> 0 Then
For Each varItem In .ItemsSelected
strWhere = strWhere & "Services.[ser_postcode].value IN ('" & .ItemData(varItem) & "') AND "
Next
End If
End With
With SearchContainer
If .ItemsSelected.Count <> 0 Then
For Each varItem In .ItemsSelected
strWhere = strWhere & "Services.[ser_cont].value IN ('" & .ItemData(varItem) & "') AND "
Next
End If
End With
With SearchWasteType
If .ItemsSelected.Count <> 0 Then
For Each varItem In .ItemsSelected
strWhere = strWhere & "Services.[ser_wastetype].value IN ('" & .ItemData(varItem) & "') AND "
Next
End If
End With
' Strip off the trailing " And " text string
If Len(strWhere) > 0 Then strWhere = Left(strWhere, Len(strWhere) - _
Len(" AND "))
Me.Filter = strWhere
Me.FilterOn = True
End Sub
What I gave you was concatenating the IN() items whereas yours is concatenating a bunch of IN()s. Before the Me.Filter line put Msgbox strWhere and you will see what I mean. Simply follow what was given.
Two more things:
1. Avoid "open-ended" or one-line IF statements, close them properly with an END IF. If you want one-line IFs then use the IIF statement, however, the IIF will evaluate both arguments.
2. There's no point turning on the form's filter if a filter was not chosen. So enclose those two lines in your Len(strWhere) IF block.
I see what you're getting at. In your case, I think you should do a string of INs for each listbox because they are multivalued. Then concatenate the INs with ANDs. So:
IN('AB', 'BB', 'BC') AND IN(container list) AND IN(waste type list)
Will that not work for you?
Good morning! If you that's what you truly want then just get rid of the INs and concatenate everything using ANDs.
You could create a dummy record with that data and display it that way or just have a label at the top that displays a count of how many records are returned after a search.
Me.txtSearchResults.ControlSource = "=Count([ser_sup])"
Me.txtSearchResults.ControlSource = ""="&"Your Search returned"&"Count([ser_sup])"&"results""
="your search returned " & Count([ser_sup]) & " results"