Although report events aren't as plentiful as form events, the report events you can trap for allow you to control what happens as your report runs. This section discusses report events, and the section "Events Available for Report Sections, and When to Use Them" covers events specific to report sections.
The Open event is the first event that occurs in a report, before the report begins printing or displaying. In fact, it happens even before the query underlying the report is run. Listing 10.1 provides an example of using the Open event.
You can find this code in rptProjectBillingsByWorkCode in CHAP10.MDB on the sample code CD-ROM. It tries to open the frmReportDateRange form, the criteria form that supplies the parameters for the query underlying the report. The code cancels the report if it is unable to load the form. |
The Close event occurs as the report is closing, before the Deactivate event occurs. Listing 10.2 illustrates the use of the Close event.
A report's Activate event happens when the report becomes the active window. It occurs after the Open event and before the report starts printing. You will often use this event to display a custom toolbar that will be visible whenever the report is active. Listing 10.3 shows an example.
This code hides the Print Preview toolbar and shows the custom toolbar called Custom Print Preview. As you will see, this event works with the Deactivate event to show and hide the custom report toolbars when the report becomes the active window, and the user moves the focus to another window.
The Deactivate event occurs when you move to another Access window or close the report,
not when focus is moved to another application. Listing 10.4 provides an example of how you can use the Deactivate event.
This routine hides the custom toolbar displayed during the Activate event and indicates that the Print Preview toolbar should once again display where appropriate. You don't want to show the Print Preview toolbar here; instead, you just reset it to display whenever Access's default behavior would tell it to display. The acToolbarWhereApprop constant accomplishes this task.
NOTE
The sample code used in the sections on the Activate and Deactivate events illustrates one way to hide and show custom toolbars. You can also use the Toolbar property of a report to perform the same task. However, when you need to display more than one toolbar while the report is active, you must place the code to hide and show the toolbars in the Activate and Deactivate events.
If no records meet the criteria of the recordset underlying a report's RecordSource, the report prints without any data and displays #Error in the report's Detail section. To eliminate this problem, you can code the NoData event of the report, which executes when no records meet the criteria specified in the report's RecordSource (see Listing 10.5).
The Page event gives you the opportunity to do something immediately before the formatted page is sent to the printer. For example, the Page event can be used to place a border around a page, as shown in Listing 10.6.
If a Jet Engine error occurs when the report is formatting or printing, the Error event is triggered. This error usually occurs if there's no RecordSource for the report or if someone else has exclusive use over the report's RecordSource. Listing 10.7 provides an example.
NOTE
If you have Name Autocorrect turned on, the process of renaming the query will not cause the desired error to occur.
This code responds to a DataErr of 2580, which means that the report's RecordSource isn't available. A custom message is displayed to the user, and the Access error is suppressed.