I keep getting an error message stating that I have written “a subquery that can return more than one field without using the EXISTS reserved word in the main query’s FROM clause. Revise the SELECT statement of the subquery to request only one field.”
But I am needing all of the fields to appear on the report based upon the filtering criteria of all the fields in the VBA “strFilter” query below. Following is all the code for the procedure.
I have tried both a table and query as the record source for the form that has the text and combo boxes. Here is the query:
I have also tried the following but keep getting an Expected End of Statement error with the parenthesis on the last line after strSubCategory being highlighted.
If I put a semi-colon between the parenthesis and strSubCategory, I again get an Expected End of Statement error.
Thanks in advance.
But I am needing all of the fields to appear on the report based upon the filtering criteria of all the fields in the VBA “strFilter” query below. Following is all the code for the procedure.
Code:
Option Compare Database
Option Explicit
Private Sub cmd_ApplyFilter_Click()
'
'variables for holding the SQL code from the text boxes
Dim strBeginDate As String
Dim strEndDate As String
'variables for holding the SQL code from the combo boxes
Dim strLocation As String
Dim strReportedBy As String
Dim strStaffInvolved As String
Dim strCategory As String
Dim strSubCategory As String
'variable to hold the combined criteria for the filter
Dim strFilter As String
'open report code here
DoCmd.OpenReport "rpt_DetailReportBySelection", acViewPreview
'
'if a combo box is empty its value is NULL. using an IF statement to check _
whether or not the user made a choice and then create the appropriate _
SQL code
'code for empty beginning date text box
If IsNull(Me.txt_BeginDate.Value) Then
strBeginDate = "Like'*'"
Else 'Note the use of the single quote marks above and below
strBeginDate = "='" & Me.txt_BeginDate.Value & "'"
End If
'code for empty ending date text box
If IsNull(Me.txt_EndDate.Value) Then
strEndDate = "Like'*'"
Else 'Note the use of the single quote marks above and below
strEndDate = "='" & Me.txt_EndDate.Value & "'"
End If
'code for empty Location combo box
If IsNull(Me.cmb_Location.Value) Then
strLocation = "Like'*'"
Else 'Note the use of the single quote marks above and below
strLocation = "='" & Me.cmb_Location.Value & "'"
End If
'code for empty ReportBy combo box
If IsNull(Me.cmb_ReportedBy.Value) Then
strReportedBy = "Like'*'"
Else 'Note the use of the single quote marks above and below
strReportedBy = "='" & Me.cmb_ReportedBy.Value & "'"
End If
'code for empty StaffInvolved combo box
If IsNull(Me.cmb_StaffInvolved.Value) Then
strStaffInvolved = "Like'*'"
Else 'Note the use of the single quote marks above and below
strStaffInvolved = "='" & Me.cmb_StaffInvolved.Value & "'"
End If
'code for empty Category combo box
If IsNull(Me.cmb_Category.Value) Then
strCategory = "Like'*'"
Else 'Note the use of the single quote marks above and below
strCategory = "='" & Me.cmb_Category.Value & "'"
End If
'code for empty SubCategory combo box
If IsNull(Me.cmb_SubCategory.Value) Then
strSubCategory = "Like'*'"
Else 'Note the use of the single quote marks above and below
strSubCategory = "='" & Me.cmb_SubCategory.Value & "'"
End If
strFilter = " SELECT Location, ReportedBy, StaffInvolved, Category, SubCategory" & _
" FROM tbl_DisruptionDetails " & _
" WHERE ReportDate BETWEEN [BeginDate] " & strBeginDate & _
" AND [EndDate] " & strEndDate & _
" AND [ReportedBy] " & strReportedBy & _
" AND [Location] " & strLocation & _
" AND [StaffInvolved] " & strStaffInvolved & _
" AND [Category] " & strCategory & _
" AND [SubCategory] " & strSubCategory
'filter is applied to the report and switched on
With Reports![rpt_DetailReportBySelection]
.Filter = strFilter
.FilterOn = True
End With
End Sub
Code:
SELECT tbl_DisruptionDetails.*, *
FROM tbl_DisruptionDetails
WHERE (((tbl_DisruptionDetails.ReportDate) Between [Forms]![zzzfrm_prm_DetailsFiltered]![txt_BeginDate] And [Forms]![zzzfrm_prm_DetailsFiltered]![txt_EndDate]));
I have also tried the following but keep getting an Expected End of Statement error with the parenthesis on the last line after strSubCategory being highlighted.
Code:
strFilter = " SELECT Location, ReportedBy, StaffInvolved, Category, SubCategory" & _
" FROM tbl_DisruptionDetails " & _
" WHERE EXISTS (SELECT ReportDate BETWEEN [BeginDate] " & _ “strBeginDate “ & _
" AND [EndDate] " & strEndDate & _
" AND [ReportedBy] " & strReportedBy & _
" AND [Location] " & strLocation & _
" AND [StaffInvolved] " & strStaffInvolved & _
" AND [Category] " & strCategory & _
" AND [SubCategory] " & strSubCategory)
If I put a semi-colon between the parenthesis and strSubCategory, I again get an Expected End of Statement error.
Thanks in advance.