G
Guest
I have zero knowledge of visual basic and am having a very difficult time
trying to get my project to work. I need help in layman's terms...
The following code works great. It allows me to select multiple items from
a listbox and run a query that is filtered by the selected items. Now for
the part I have been struggling with. I want to be able to add multiple list
boxes to the form so that I can filter on item 1 AND item2 AND item3.
For example, this code lets me select employee names. The second List Box
(called lstLOB) contains the Line of Business. Since employees handle
multiple lines, I want to be able to run the query on Employee Name AND Line
of Business.
For someone that doesnt understand coding, how would you add/modify this?
Please...someone throw me a bone...
Peter
Private Sub cmdFilter_Click()
Dim varItem As Variant
Dim strWhere As String
Dim strSQL As String
'build criteria for the selected employees
For Each varItem In Me.lstEmp.ItemsSelected
strWhere = strWhere & Chr(34) & Me.lstEmp.ItemData(varItem) & Chr(34) &
", "
Next varItem
'make sure at least 1 employee was selected
If Len(strWhere) > 0 Then
strSQL = "[Employee Name] IN (" & Left$(strWhere, Len(strWhere) - 2) & ")"
End If
'this changes the actual SQL of your query based on the items selected in
the listboxes
'and is the very last line of code to execute
CurrentDb.QueryDefs("zzzHelpMeQuery").SQL = "SELECT * FROM Joined WHERE " &
strSQL
End Sub
trying to get my project to work. I need help in layman's terms...
The following code works great. It allows me to select multiple items from
a listbox and run a query that is filtered by the selected items. Now for
the part I have been struggling with. I want to be able to add multiple list
boxes to the form so that I can filter on item 1 AND item2 AND item3.
For example, this code lets me select employee names. The second List Box
(called lstLOB) contains the Line of Business. Since employees handle
multiple lines, I want to be able to run the query on Employee Name AND Line
of Business.
For someone that doesnt understand coding, how would you add/modify this?
Please...someone throw me a bone...
Peter
Private Sub cmdFilter_Click()
Dim varItem As Variant
Dim strWhere As String
Dim strSQL As String
'build criteria for the selected employees
For Each varItem In Me.lstEmp.ItemsSelected
strWhere = strWhere & Chr(34) & Me.lstEmp.ItemData(varItem) & Chr(34) &
", "
Next varItem
'make sure at least 1 employee was selected
If Len(strWhere) > 0 Then
strSQL = "[Employee Name] IN (" & Left$(strWhere, Len(strWhere) - 2) & ")"
End If
'this changes the actual SQL of your query based on the items selected in
the listboxes
'and is the very last line of code to execute
CurrentDb.QueryDefs("zzzHelpMeQuery").SQL = "SELECT * FROM Joined WHERE " &
strSQL
End Sub