|[ Team LiB ]|
Recipe 3.9 Suppress Printing a Report if There Are No Records to Print
You have a report that prints records you select from a criteria form. Sometimes there aren't any records that match the criteria and the report opens with #Error in the detail section, which is unattractive and confusing. Is there any way you can prevent the report from printing when it has no records to print?
Access includes an event, OnNoData, that fires when no records are present in the report's underlying recordset. This solution shows you how to use this new event to suppress printing of the report when no records match the specified criteria.
To create a report that suppresses printing when there are no records, follow these steps:
The following example demonstrates this solution. Load the 03-09.MDB database. Open the frmCriteria1 pop-up criteria form. This form allows you to enter criteria for the rptSelect1 report (see Figure 3-19).
When you press the traffic-light button, a simple event procedure will execute that opens the report in print preview mode. The rptSelect1 report is based on the qryCriteria1 parameter query, which derives its parameter values from the controls on the frmCriteria1 form. If you accept the default values, the parameter query will return a recordset with no records. This will produce the report shown in Figure 3-20.
Now open the frmcriteria2 pop-up criteria form. This form is identical to the first, except that the event procedure attached to its command button runs the rptSelect2 report instead. If you accept the default values, the rptSelect2 report will attempt to run, again with no records. But this version of the report has an event procedure attached to its OnNoData event that suppresses printing and instead displays the message box shown in Figure 3-21.
The OnNoData event is triggered whenever a report attempts to print with no records. If you attach an event procedure to the OnNoData event, your code will run whenever the report prints without any records. While the MsgBox statement informs the user what has happened, the key line of code is:
Cancel = True
This line tells Access to cancel printing of the report (by setting the passed Cancel argument to True).
If you use VBA code to open a report that has no data and allow the report's OnNoData event to cancel the report, you will get an error in the code that attempted to open the report. So, in this solution, you'll find error-handling code in the button-click event that opens the report in frmCriteria2. When an error occurs, the code checks whether it's the expected error, which has a number of 2501. If so, it ignores the error. Here's the code behind the cmdPrint button:
Private Sub cmdPrint_Click( ) On Error GoTo HandleErr Me.Visible = False DoCmd.OpenReport "rptSelect2", acPreview ExitHere: DoCmd.Close acForm, Me.Name Exit Sub HandleErr: Select Case Err.Number Case 2501 ' The OpenReport action was canceled. ' There were no rows. So do nothing. Case Else MsgBox Err.Number & ": " & Err.Description End Select Resume ExitHere End Sub
The report header contains controls to display the selection criteria, which are picked up from the criteria form, using expressions like this one:
The form disappears from view when the report opens in print preview mode because the event procedure attached to the traffic-light button sets the form's Visible property to False before opening the report. Making the form invisible (rather than closing it) ensures that the selection criteria are still available for the report's data source.
3.9.4 See Also
For more information on printing query criteria on reports, see the Solution in Recipe 3.2.
|[ Team LiB ]|