Hi,
I am trying to set up a listbox with an option to select multiple items (I have done this and tested it with debug.print and it seems to work). I am then building a filter statement with VBA. I want to then use a button to add this statement to the filter in a subform with (a datasheet design), and then requery it.
My code below seems to be working in part. But I am getting all the items at times. Seems to work consistenly when selecting one item only, but I can't see anything wrong with my 'OR' statements when I debug.print.
Any ideas?
-----------------------------------
Private Sub Command176_Click()
Dim i As Integer
Dim strFilter As String
Dim blnFirst As Boolean
i = 0
If Me.List163.ItemsSelected.Count = 0 Then
MsgBox ("select an item")
Else
strFilter = "Area = "
blnFirst = True
For i = 0 To Me.List163.ListCount - 1
If Me.List163.Selected(i) = True Then
'Debug.Print List163.ItemData(i)
If blnFirst = True Then
strFilter = strFilter & "'" & List163.ItemData(i) & "'"
blnFirst = False
Else
strFilter = strFilter & " or " & "'" & List163.ItemData(i) & "'"
End If
End If
Next i
Me.sbfrmItems.Form.FilterOn = True
Me.sbfrmItems.Form.Filter = strFilter
Me.sbfrmItems.Form.Requery
End If
End Sub
I am trying to set up a listbox with an option to select multiple items (I have done this and tested it with debug.print and it seems to work). I am then building a filter statement with VBA. I want to then use a button to add this statement to the filter in a subform with (a datasheet design), and then requery it.
My code below seems to be working in part. But I am getting all the items at times. Seems to work consistenly when selecting one item only, but I can't see anything wrong with my 'OR' statements when I debug.print.
Any ideas?
-----------------------------------
Private Sub Command176_Click()
Dim i As Integer
Dim strFilter As String
Dim blnFirst As Boolean
i = 0
If Me.List163.ItemsSelected.Count = 0 Then
MsgBox ("select an item")
Else
strFilter = "Area = "
blnFirst = True
For i = 0 To Me.List163.ListCount - 1
If Me.List163.Selected(i) = True Then
'Debug.Print List163.ItemData(i)
If blnFirst = True Then
strFilter = strFilter & "'" & List163.ItemData(i) & "'"
blnFirst = False
Else
strFilter = strFilter & " or " & "'" & List163.ItemData(i) & "'"
End If
End If
Next i
Me.sbfrmItems.Form.FilterOn = True
Me.sbfrmItems.Form.Filter = strFilter
Me.sbfrmItems.Form.Requery
End If
End Sub