I have a cross tab query that has an expression in a value field. The expression includes a Dcount calculation from a different query. The main query returns records based on a range of dates so there are date parameters in the main query. The entire cross tab query works correctly when I run it. What I would like to do however, is filter the results in the cross tab query AND in the query that lies inside the expression based on a parameter that will limit the results to either persons that are senior citizens or persons that are veterans. In the cross tab query, I have added row headings for senior citizens and veterans. I have VBA code in a procedure that calls the report that will filter on one of these two parameters. What I don't know how to do is to put the same filter on the query that is inside the expression of the cross tab query. My goal here is to use the same report container, and the same query container for both sets of reports. I know that I could set up separate queries for all of this but I was trying to be economical with my code as well as my queries. Is this even possible?
The following is the procedure that calls the report. It has multiple case statements but I removed most of them since they don't apply to my question. Case 9 returns a report of all individuals. Case 11 and Case 12 return reports that are filtered to return only veterans or senior citizens. The same report is used for all 3 Case statements. The On load event of the report declares which query is used to populate the report. That On load event is not listed here as it works correctly.
Code:
Private Sub CmdReport_Click()
On Error GoTo Err_CmdReport_Click
Dim startdate As Date
Dim enddate As Date
Dim DateRange As String
Dim rptlabel As String
If GetPrinter() = False Then
Msgbox " Printers must be set up in the print settings menu before printing reports.", vbOKOnly, "Need Printer setup"
Exit Sub
ElseIf IsNull(Me.Text2.Value) Then
Msgbox " Please enter the range of dates for the report", vbOKOnly, "Missing dates"
Me.Text2.SetFocus
Exit Sub
ElseIf IsNull(Me.Text4.Value) Then
Msgbox " Please enter the range of dates for the report", vbOKOnly, "Missing dates"
Me.Text4.SetFocus
Exit Sub
End If
If Frame89.Value = 7 Then
DoCmd.OpenForm "Client_List_Reports", acNormal, , , acFormEdit, acWindowNormal, DateRange
End If
startdate = Me.Text2.Value
enddate = Me.Text4.Value
DateRange = "tblServiceInstance.Indate >= #" & startdate & "# AND tblServiceInstance.Indate <= #" & enddate & "#"
Select Case Me.Frame89.Value
Case 9
rptname = "IndividualsAssistedbyCounty_Report"
prnttype = "Document"
qryname = "qryIndividualsAssisted_Crosstab"
rptlabel = "Individuals"
If RecordCount(qryname) = 0 Then
Msgbox "There was no activity for this range of dates", vbOKOnly
Exit Sub
End If
DoCmd.OpenReport rptname, acViewReport, , , acWindowNormal, rptlabel
Set rpt = Reports(rptname)
Set rpt.Printer = Application.Printers(Selprnt(prnttype))
rpt.Printer.Orientation = acPRORPortrait
Case 11
rptname = "IndividualsAssistedbyCounty_Report"
prnttype = "Document"
qryname = "qryIndividualsSpecialAssisted_Crosstab"
rptlabel = "Veteran"
If RecordCount(qryname) = 0 Then
Msgbox "There was no activity for this range of dates", vbOKOnly
Exit Sub
End If
DoCmd.OpenReport rptname, acViewReport, , "Cl_Veteran = True ", acWindowNormal, rptlabel
Set rpt = Reports(rptname)
Set rpt.Printer = Application.Printers(Selprnt(prnttype))
rpt.Printer.Orientation = acPRORPortrait
Case 12
rptname = "IndividualsAssistedbyCounty_Report"
prnttype = "Document"
qryname = "qryIndividualsSpecialAssisted_Crosstab"
rptlabel = "Senior Citizen"
If RecordCount(qryname) = 0 Then
Msgbox "There was no activity for this range of dates", vbOKOnly
Exit Sub
End If
DoCmd.OpenReport rptname, acViewReport, , "Cl_ageType= 'Senior Citizen' ", acWindowNormal, rptlabel
Set rpt = Reports(rptname)
Set rpt.Printer = Application.Printers(Selprnt(prnttype))
rpt.Printer.Orientation = acPRORPortrait
Case Else 'no option buttons have been selected
Msgbox "You must select at least one option to generate a report"
End Select
Set rpt = Nothing
Application.Printer = Nothing
Exit_Err_CmdReport_Click:
Exit Sub
Here is the edit view of the query used to filter by senior citizens only. In the expression field, I have spelled out that the underlying query "qryFurnitureOnHoldSuummary" filter on the [CL_agetype] field. That I am trying to do is replace that explicit criteria with a parameter that I can declare in the VBA code. I highlighted the criteria that I want to replace with a parameter. My question is: Is this possible? How do I do it?
On a related note. I have noticed that when I open a query in design view one table is highlighted in yellow. All my queries are opening this way. What is the significance of the yellow box. I never noticed that before.
