WOW. What an experience. Ok. This is solved and I hope this will help others out:
Incase anyone needs a solution for multiple multi-listboxes on a form they are using to filter a query that is not attached to the multi-listboxes but only the form and they want end-users to have the ability to filter and see the results, MajP at Tek Tips helped me solve the problem I posted on here. Here is the information to help people out.
1) Use continuous form.
2) You multi-listboxes need to be in the header selection so after selection your results are in the details.
2) Have a command buton to see results and a command button for clearing what an end-user selected.
3) Use functions to build what you need for the filters.
4) Put your labels you use to search your query in the header section as well and then the textboxes to display results in the details section. This way you labels will not continuously appear. Only the data will.
Code:
Private Sub cmdReset_Click()
Dim ctrl As Access.Control
Dim itm As Variant
For Each ctrl In Me.Controls
If ctrl.ControlType = acListBox Then
If ctrl.MultiSelect = 0 Then
ctrl = Null
Else
For Each itm In ctrl.ItemsSelected
ctrl.Selected(itm) = False
Next
End If
End If
Next ctrl
Me.Filter = ""
Me.FilterOn = False
End Sub
Private Sub cmdResults_Click()
Dim FormFilter As String
FormFilter = GetFilterFromListBoxes
Debug.Print FormFilter
Me.FilterOn = False
Me.Filter = FormFilter
Me.FilterOn = True
End Sub
Public Function GetFilterFromListBoxes() As String
Dim lst As Access.ListBox
Dim ctrl As Access.Control
Dim fieldName As String
Dim fieldType As String
Dim TotalFilter As String
Dim ListFilter As String
Dim itm As Variant
'Each listbox needs a tag property with the field name and the field type
'Seperate these with a ;
'The types are Text, Numeric, or Date
For Each ctrl In Me.Controls
If ctrl.ControlType = acListBox Then
fieldName = Split(ctrl.tag, ";")(0)
fieldType = Split(ctrl.tag, ";")(1)
For Each itm In ctrl.ItemsSelected
If ListFilter = "" Then
ListFilter = GetProperType(ctrl.ItemData(itm), fieldType)
Else
ListFilter = ListFilter & "," & GetProperType(ctrl.ItemData(itm), fieldType)
End If
Next itm
If Not ListFilter = "" Then
ListFilter = fieldName & " IN (" & ListFilter & ")"
End If
If TotalFilter = "" And ListFilter <> "" Then
TotalFilter = ListFilter
ElseIf TotalFilter <> "" And ListFilter <> "" Then
TotalFilter = TotalFilter & " AND " & ListFilter
End If
ListFilter = ""
End If
Next ctrl
GetFilterFromListBoxes = TotalFilter
End Function
Public Function GetProperType(varItem As Variant, fieldType As String) As Variant
If fieldType = "Text" Then
GetProperType = sqlTxt(varItem)
ElseIf fieldType = "Date" Then
GetProperType = SQLDate(varItem)
Else
GetProperType = varItem
End If
End Function
Public Function sqlTxt(varItem As Variant) As Variant
If Not IsNull(varItem) Then
varItem = Replace(varItem, "'", "''")
sqlTxt = "'" & varItem & "'"
End If
End Function
Function SQLDate(varDate As Variant) As Variant
If IsDate(varDate) Then
If DateValue(varDate) = varDate Then
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
Else
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
End If
End If
End Function
My form is linked to my query.
My multi-listboxes are linked to the tables within my database that house all the information. Such as State would house all the states so end-users would be able to see all the states. This does not mean the end-user will see all the states in the results. They will only see what is in my query. My query is querying fields from my main table I want to show on my results based on what they filter for. Hope this makes sense.
I hope this helps those out there that have requests for multiple multi-listboxes.............Was a 2wk challenge for me trying to figure this out and I thank MajP so assisting in the solution.