Report event procedures
Just as with forms, reports also use event procedures to respond to specific events. Access reports support events for the overall report itself and for each section in the report. Individual controls on Access reports do not raise events.
Attaching an event procedure to the report runs code whenever the report opens, closes, or prints. Each section in a report (header, footer, and so on) also includes events that run as the report is formatted or printed.
Several overall report event properties are available. Table-6 shows the Access report events. As you can see, the list of report events is much shorter than the form event list.Table-6: Report Events
|Event Property||When the Event Is Triggered|
|Open||When the report opens but before printing|
|Close||When the report closes and is removed from the screen|
|Active||When the report receives the focus and becomes the active window|
|Deactivate||When a different window becomes active|
|NoData||When no data is passed to the report as it opens|
|Page||When the report changes pages|
|Error||When a runtime error is produced in Access|
Even though users do not interact with reports as they do with forms, events still play a vital role in report design. Opening a report containing no data generally yields erroneous results. The report may display a title and no detail information. Or, it may display #error values for missing information. This situation can be a little scary for the user. Use the NoData event to inform the user that the report contains no data. NoData fires as a report opens and there is no data available in the report's RecordSource. Use the NoData event procedure to display a message box describing the situation to the user and then cancel the report's opening.
The Report_NoData event illustrated displays a message box to advise the user that the report contains no data. Then the event procedure cancels the report's opening by setting the Cancel parameter to True. Because the Cancel parameter is set to True, the report never appears on the screen and is not sent to the printer.
Many Access events are accompanied by parameters, such as the Cancel parameter you see. In this case, setting Cancel to True instructs Access to simply ignore the process that triggered the event. Because NoData was triggered as part of the report's opening process, setting Cancel to True prevents the report from being sent to the printer or being displayed on the screen.