I have created a form that enables a subquery display and report generation. The outputs are based on the option of a single through multiple criteria inputs (query records based on city, zip code, date range, etc.). I was recently asked to enable the selection of multiple cities, so logically I created a multi list box that presents all the available cities without duplicates in A-Z ascending order. Everything works perfectly when my inputs were single entry qualifiers; however, incorporating the single column multi list box (with Simple multi-selection) is causing me grief. My filter code is essentially a combination of Allen Browne's (for the single value entry across multiple field options) and Baldy's for the multi list box. Any guidance on how to incorporate them all would be much appreciated. Thanks so much@
Private Sub cmdFilter_Click()
Dim strWhere As String
Dim lngLen As Long
Dim ctl As Control
Dim varItem As Variant
Const conJetDate = "\#mm\/dd\/yyyy\#"
If Not IsNull(Me.txtFilterMainName) Then
strWhere = strWhere & "([MainName] Like ""*" & Me.txtFilterMainName & "*"") AND "
End If
If Not IsNull(Me.txtfilterZip) Then
strWhere = strWhere & "([Zip] Like ""*" & Me.txtfilterZip & "*"") AND "
End If
If Not IsNull(Me.txtFilterAddKey) Then
strWhere = strWhere & "([AddressL1] Like ""*" & Me.txtFilterAddKey & "*"") AND "
End If
If Not IsNull(Me.cboFilterMinTax) Then
strWhere = strWhere & "([Tax] >= " & Me.cboFilterMinTax & ") AND "
End If
If Not IsNull(Me.txtStartDate) Then
strWhere = strWhere & "([EnteredOn] >= " & Format(Me.txtStartDate, conJetDate) & ") AND "
End If
If Not IsNull(Me.txtEndDate) Then
strWhere = strWhere & "([EnteredOn] < " & Format(Me.txtEndDate + 1, conJetDate) & ") AND "
End If
Set ctl = Me.txtFilterCity
For Each varItem In ctl.ItemsSelected
strWhere = strWhere & "'" & ctl.ItemData(varItem) & "',"
Next varItem
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
MsgBox "No criteria", vbInformation, "Nothing to do."
Else
strWhere = Left$(strWhere, lngLen)
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub
Private Sub cmdFilter_Click()
Dim strWhere As String
Dim lngLen As Long
Dim ctl As Control
Dim varItem As Variant
Const conJetDate = "\#mm\/dd\/yyyy\#"
If Not IsNull(Me.txtFilterMainName) Then
strWhere = strWhere & "([MainName] Like ""*" & Me.txtFilterMainName & "*"") AND "
End If
If Not IsNull(Me.txtfilterZip) Then
strWhere = strWhere & "([Zip] Like ""*" & Me.txtfilterZip & "*"") AND "
End If
If Not IsNull(Me.txtFilterAddKey) Then
strWhere = strWhere & "([AddressL1] Like ""*" & Me.txtFilterAddKey & "*"") AND "
End If
If Not IsNull(Me.cboFilterMinTax) Then
strWhere = strWhere & "([Tax] >= " & Me.cboFilterMinTax & ") AND "
End If
If Not IsNull(Me.txtStartDate) Then
strWhere = strWhere & "([EnteredOn] >= " & Format(Me.txtStartDate, conJetDate) & ") AND "
End If
If Not IsNull(Me.txtEndDate) Then
strWhere = strWhere & "([EnteredOn] < " & Format(Me.txtEndDate + 1, conJetDate) & ") AND "
End If
Set ctl = Me.txtFilterCity
For Each varItem In ctl.ItemsSelected
strWhere = strWhere & "'" & ctl.ItemData(varItem) & "',"
Next varItem
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
MsgBox "No criteria", vbInformation, "Nothing to do."
Else
strWhere = Left$(strWhere, lngLen)
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub