I am trying to create a form with multiple list boxes to allow the user to filter the results. Here is my code for the button called ‘Query’:
Private Sub Query_Click()
Dim strWhere As String
Dim lngLen As Long
Dim strDelim As String
Dim strQuery As String
Const conJetDate = "#mm/dd/yyyy#"
strDelim = "'"
If Me.txtPlanTypes.ItemsSelected.Count > 0 Then
With Me.txtPlanTypes
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strWhere = strWhere & "([Plan_Type] = " & strDelim & .ItemData(varItem) & strDelim & ") OR "
End If
Next
End With
lngLen = Len(strWhere) - 3
strWhere = Left$(strWhere, lngLen)
strWhere = "(" & strWhere & ") AND "
End If
If Me.txtPlanStatus.ItemsSelected.Count > 0 Then
With Me.txtPlanStatus
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strWhere = strWhere & "([CurrentStatus] = " & strDelim & .ItemData(varItem) & strDelim & ") OR "
End If
Next
End With
lngLen = Len(strWhere) - 3
strWhere = Left$(strWhere, lngLen)
strWhere = "(" & strWhere & ") AND "
End If
If Not IsNull(Me.txtStartDate) Then
strWhere = strWhere & "([tbl_groups].[PYE] >= " & Format(Me.txtStartDate, conJetDate) & ") AND "
End If
If Not IsNull(Me.txtEndDate) Then
strWhere = strWhere & "([tbl_groups].[PYE] < " & Format(Me.txtEndDate + 1, conJetDate) & ") AND "
End If
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then 'Nah: there was nothing in the string.
MsgBox "No criteria", vbInformation, "Nothing to do."
Else 'Yep: there is something there, so remove the " AND " at the end.
strWhere = Left$(strWhere, lngLen)
End If
strQuery = "SELECT tbl_groups.GA_Number, tbl_groups.Plan_Name, tbl_groups.PYE, tbl_groups.Platform, tbl_groups.Custodian, tbl_groups.Plan_Type, tbl_groups.CurrentStatus, tbl_groups.Primary_Administrator, qryrptMaxForceOutDate.MaxOfForceOutsUploadDate AS [Last Force-Outs Run], tbl_groups.ThreeSixteenPlan, tblSystem.SystemGrp, tblCompliance.ComplianceSystem, tbl_groups.InvoluntaryCashOut, tbl_groups.VestingUploadedDate, IIf(IsNull([MaxOfForceOutsUploadDate])=True,'Not Complete','Complete') AS Complete, tblCompliance.DateBillNot INTO tblForceOutReport FROM ((tbl_groups LEFT JOIN qryrptMaxForceOutDate ON (tbl_groups.GA_Number = qryrptMaxForceOutDate.GA_Number) AND (tbl_groups.PYE = qryrptMaxForceOutDate.PYE)) INNER JOIN tblCompliance ON tbl_groups.GA_Record_ID = tblCompliance.GA_Record_ID) INNER JOIN tblSystem ON tbl_groups.Platform = tblSystem.SystemType "
strQuery = strQuery & " WHERE " & strWhere & ";"
DoCmd.RunSQL strQuery
End Sub
It creates the query and it’s almost correct. I took the SQL it created and put it in a query design view. This is what it looks like:
In the [Plan_Type] field I need it to have ‘401(k) or 403(b)’ on both lines, not just the first.
Here is the SQL the VBA produces:
SELECT tbl_groups.GA_Number, tbl_groups.Plan_Name, tbl_groups.PYE, tbl_groups.Platform, tbl_groups.Custodian, tbl_groups.Plan_Type, tbl_groups.CurrentStatus, tbl_groups.Primary_Administrator, qryrptMaxForceOutDate.MaxOfForceOutsUploadDate AS [Last Force-Outs Run], tbl_groups.ThreeSixteenPlan, tblSystem.SystemGrp, tblCompliance.ComplianceSystem, tbl_groups.InvoluntaryCashOut, tbl_groups.VestingUploadedDate, IIf(IsNull([MaxOfForceOutsUploadDate])=True,’Not Complete’,’Complete’) AS Complete, tblCompliance.DateBillNot INTO tblForceOutReport FROM ((tbl_groups LEFT JOIN qryrptMaxForceOutDate ON (tbl_groups.GA_Number = qryrptMaxForceOutDate.GA_Number) AND (tbl_groups.PYE = qryrptMaxForceOutDate.PYE)) INNER JOIN tblCompliance ON tbl_groups.GA_Record_ID = tblCompliance.GA_Record_ID) INNER JOIN tblSystem ON tbl_groups.Platform = tblSystem.SystemType WHERE ((([Plan_Type] = ‘401(k)’) OR ([Plan_Type] = ‘403(b)’) ) AND ([CurrentStatus] = ‘1st – In Conversion – Startup’) OR ([CurrentStatus] = ‘1st – In Conversion – Takeover’) ) AND ([tbl_groups].[PYE] >= #12/31/2023#) AND ([tbl_groups].[PYE] < #01/01/2025#);
What parentheses or AND/OR toggle am I missing to accomplish that?
Any help would be appreciated.