Hello all,
I'm wondering if there is a more efficient way of doing the process I'm doing.
As of now, I have 2 list boxes filtering two fields on my form. They can work independently or together to narrow down even further.
I'm hoping to add a third list box to filter another field, however it seems like I would have to create several more if statements if I continue down this route.
Code:
Private Sub Command2_Click()
Dim Criteria As String
Dim Criteria2 As String
Dim i As Variant
' Build criteria string from selected items in list box.
Criteria = ""
Criteria2 = ""
For Each i In Me!
[List1].ItemsSelected
If Criteria <> "" Then
Criteria = Criteria & " OR "
End If
Criteria = Criteria & "[PlanStatusID]=" _
& Me!
[List1].ItemData(i)
Next i
For Each i In Me!
[List0].ItemsSelected
If Criteria2 <> "" Then
Criteria2 = Criteria2 & " OR "
End If
Criteria2 = Criteria2 & "[PlanNameID]=" _
& Me!
[List0].ItemData(i)
Next i
' Filter the form using selected items in the list box.
If Criteria = "" Then
Me.Filter = Criteria2
Me.FilterOn = True
End If
If Criteria2 = "" Then
Me.Filter = Criteria
Me.FilterOn = True
End If
If Criteria <> "" And Criteria2 <> "" Then
Me.Filter = Criteria & "AND" & Criteria2
Me.FilterOn = True
End If
End Sub