I have been tackling the exact problem myself and finally got it to work - a
form with 4 list boxes that filters the data based on my selections. I
created a Function that builds a Where String and then passed that to the
query of the form with a Command Button that filters the records when pressed.
Private Function WhereString() As String
Dim strWhere As String
Dim strWhere1 As String
Dim strWhere2 As String
Dim strWhere3 As String
Dim varItem As Variant
On Error Resume Next
' ... build "Make" criterion expression
If Me.lstMake.ItemsSelected.Count > 0 Then
strWhere = strWhere & "Make IN ("
For Each varItem In Me.lstMake.ItemsSelected
strWhere = strWhere & "'" & _
Me.lstMake.ItemData(varItem) & "', "
Next varItem
strWhere = Left(strWhere, Len(strWhere) - 2) & ") AND "
End If
If Me.lstModel.ItemsSelected.Count > 0 Then
strWhere1 = strWhere1 & "Model IN ("
For Each varItem In Me.lstModel.ItemsSelected
strWhere1 = strWhere1 & "'" & _
Me.lstModel.ItemData(varItem) & "', "
Next varItem
strWhere1 = Left(strWhere1, Len(strWhere1) - 2) & ") AND "
End If
If Me.lstGroup.ItemsSelected.Count > 0 Then
strWhere2 = strWhere2 & "SDescription IN ("
For Each varItem In Me.lstGroup.ItemsSelected
strWhere2 = strWhere2 & "'" & _
Me.lstGroup.ItemData(varItem) & "', "
Next varItem
strWhere2 = Left(strWhere2, Len(strWhere2) - 2) & ") AND "
End If
If Me.lstBrand.ItemsSelected.Count > 0 Then
strWhere3 = strWhere3 & "Brand IN ("
For Each varItem In Me.lstBrand.ItemsSelected
strWhere3 = strWhere3 & "'" & _
Me.lstBrand.ItemData(varItem) & "', "
Next varItem
strWhere3 = Left(strWhere3, Len(strWhere3) - 2) & ") AND "
End If
WhereString = strWhere & strWhere1 & strWhere2 & strWhere3
If Len(WhereString) > 0 Then
WhereString = " WHERE " & Left(WhereString, Len(WhereString) - 5)
End If
End Function
Private Sub cmdSearch_Click()
Dim strSQL As String
Dim strRecordSource As String
On Error Resume Next
strRecordSource = "qryModelSearch"
' move focus to clear button
Me.cmdClear.SetFocus
' build sql string for form's RecordSource
strSQL = "SELECT * FROM " & strRecordSource & _
WhereString()
Me.RecordSource = strSQL
End Sub
gorebeccago said:
I can't get the code to work. I don't know how to define it, and the more I
try, the dumber I seem to become.
Is there an example somewhere that I can just download or model?
ruralguy via AccessMonster.com said:
Are you looking for something like:
Dim ctl As Control
For Each ctl in Me.Controls
If ctl.ControlType = acListBox Then
'-- What do you want to do with this ListBox???
End If
Next ctl
That link doesn't help because it's just for one list box. I have an array of
list boxes, all with multiple select conditions. I need the on_click( )
command to cycle through all of the list boxes, but don't know/can't find the
syntax.
Is there an example of this code anywhere?
Maybe this link will help:
http://www.fontstuff.com/access/acctut11.htm
[quoted text clipped - 14 lines]
Help!