I haven't added code for the non-multi-select listbox yet but was waiting to see if there was a way to deselect. Here is my code as it stands right now (with help from PHV). I've got 4 multi-selects and 5 text boxes (3 with 'OR' between them and 2 with "AND' between them) which all write to the query. It's working great now but I have to add in two more multi-selects and two non-multi-select listboxes.
I'll probably go with the 'Extended' listboxes because those give the appearance of being either-or and you can deselect by using CTRL+Click. the user can select both though if they use the CTRL key.
Private Sub cmdApplyChoices_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strSQLCriteria As String
Dim strSQL As String
Dim strSQLProj As String
Dim strSQLPhase As String
Dim strSQLSkill As String
Dim strSQLMod As String
Dim strSQLKW As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("IT_MTL Query")
'============================================
'Below we set all the initial strings to null
'============================================
strSQLCriteria = ""
strSQL = ""
strSQLProj = ""
strSQLPhase = ""
strSQLSkill = ""
strSQLMod = ""
strSQLKW = ""
'=======================================================
'Below we'll get items selected from the Project listbox
'=======================================================
If Me!ProjList.ItemsSelected.Count = 0 Then
'Do Nothing
Else
For Each varItem In Me!ProjList.ItemsSelected
strSQLProj = strSQLProj & "IT_MTL.[Task ID] Like '*" & Me.ProjList.ItemData(varItem) & "*' OR "
Next varItem
strSQLProj = Left(strSQLProj, Len(strSQLProj) - 4)
strSQLCriteria = " AND (" & strSQLProj & ")"
End If
'=====================================================
'Below we'll get items selected from the Phase listbox
'=====================================================
If Me!PhaseList.ItemsSelected.Count = 0 Then
'Do Nothing
Else
For Each varItem In Me!PhaseList.ItemsSelected
strSQLPhase = strSQLPhase & "IT_MTL.[Task ID] Like '*" & Me.PhaseList.ItemData(varItem) & "*' OR "
Next varItem
strSQLPhase = Left(strSQLPhase, Len(strSQLPhase) - 4)
strSQLCriteria = strSQLCriteria & " AND (" & strSQLPhase & ")"
End If
'========================================================
'Below we'll get items selected from the Skillset listbox
'========================================================
If Me!SkillList.ItemsSelected.Count = 0 Then
'Do Nothing
Else
For Each varItem In Me!SkillList.ItemsSelected
strSQLSkill = strSQLSkill & "IT_MTL.[Task ID] Like '*" & Me.SkillList.ItemData(varItem) & "*' OR "
Next varItem
strSQLSkill = Left(strSQLSkill, Len(strSQLSkill) - 4)
strSQLCriteria = strSQLCriteria & " AND (" & strSQLSkill & ")"
End If
'===================================================
'Below we'll get items selected from the Mod listbox
'===================================================
If Me!ModList.ItemsSelected.Count = 0 Then
'Do Nothing
Else
For Each varItem In Me!ModList.ItemsSelected
strSQLMod = strSQLMod & "IT_MTL.[Mod] Like '*" & Me.ModList.ItemData(varItem) & "*' OR "
Next varItem
strSQLMod = Left(strSQLMod, Len(strSQLMod) - 4)
strSQLCriteria = strSQLCriteria & " AND (" & strSQLMod & ")"
End If
'===========================================================
'Below we'll get items selected from the Key Word Text boxes
'===========================================================
If IsNull(Me!KeyWord1) Then
'Do nothing
Else
strSQLKW = " OR IT_MTL.[Task] Like '*" & Me!KeyWord1 & "*'"
End If
If IsNull(Me!KeyWord2) Then
'Do nothing
Else
strSQLKW = strSQLKW & " OR IT_MTL.[Task] Like '*" & Me!KeyWord2 & "*'"
End If
If IsNull(Me!KeyWord3) Then
'Do nothing
Else
strSQLKW = strSQLKW & " OR IT_MTL.[Task] Like '*" & Me!KeyWord3 & "*'"
End If
If IsNull(Me!KeyWord4) Then
'Do nothing
Else
strSQLKW = strSQLKW & " OR IT_MTL.[Task] Like '*" & Me!KeyWord4 & "*'"
End If
If IsNull(Me!KeyWord5) Then
'Do nothing
Else
strSQLKW = strSQLKW & " AND IT_MTL.[Task] Like '*" & Me!KeyWord5 & "*'"
End If
If strSQLKW = "" Then
'Do nothing
Else
strSQLKW = Right(strSQLKW, Len(strSQLKW) - 4)
strSQLCriteria = strSQLCriteria & " AND (" & strSQLKW & ")"
End If
If strSQLCriteria = "" Then
strSQL = "SELECT IT_MTL.[Task ID], IT_MTL.Task, IT_MTL.ProposedHours, IT_MTL.NegHours, IT_MTL.[Mod] FROM IT_MTL "
Else
strSQLCriteria = Right(strSQLCriteria, Len(strSQLCriteria) - 5)
'===================================================
'Below we'll combine all the separate criteria into
'one string called "strSQL"
'===================================================
strSQL = "SELECT IT_MTL.[Task ID], IT_MTL.Task, IT_MTL.ProposedHours, IT_MTL.NegHours, IT_MTL.[Mod] FROM IT_MTL " & _
"WHERE (" & strSQLCriteria & ");"
End If
qdf.SQL = strSQL
DoCmd.OpenQuery "IT_MTL Query"
Set db = Nothing
Set qdf = Nothing
End Sub