Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources]

Alison Balter

نسخه متنی -صفحه : 544/ 169
نمايش فراداده

Events Available for Reports, and When to Use Them

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

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.

Listing 10.1 The Open Event
Private Sub Report_Open(Cancel As Integer) 'Ignore an error if it occurs On Error Resume Next 'Open the report criteria form DoCmd.OpenForm "frmReportDateRange", _ WindowMode:=acDialog, _ OpenArgs:="rptProjectBillingsbyWorkCode" 'If the criteria form is not loaded, display an error 'message and cancel the printing of the report '(the form will not be loaded if the user clicks Cancel) If Not IsLoaded("frmReportDateRange") Then MsgBox "Criteria Form Not Successfully Loaded, " & _ "Canceling Report" Cancel = True End If End Sub

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

The Close event occurs as the report is closing, before the Deactivate event occurs. Listing 10.2 illustrates the use of the Close event.

Listing 10.2 The Close Event
Private Sub Report_Close() 'Close criteria form as report is closing DoCmd.Close acForm, "frmReportDateRange" End Sub

You can find this code in the rptProjectBillingsByWorkCode report in CHAP10.MDB on the sample code CD-ROM. It closes the criteria form frmReportDateRange when the report is closing, in case the form is still open.

The Activate 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.

Listing 10.3 Using the Activate Event to Display Custom Toolbars
Private Sub Report_Activate() 'Hide built-in Print Preview toolbar. 'Show Custom Print Preview toolbar. DoCmd.ShowToolbar "Print Preview", acToolbarNo DoCmd.ShowToolbar "Custom Print Preview", acToolbarYes End Sub

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

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.

Listing 10.4 Using the Deactivate Event to Display Custom Toolbars
Private Sub Report_Deactivate() 'Hide Custom Print Preview toolbar. 'Show built-in Print Preview toolbar. DoCmd.ShowToolbar "Custom Print Preview", acToolbarNo DoCmd.ShowToolbar "Print Preview", acToolbarWhereApprop End Sub

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.

The NoData Event

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).

Listing 10.5 The NoData Event
Private Sub Report_NoData(Cancel As Integer) 'Display a message and cancel processing MsgBox "There is no data for this report. Canceling report..." Cancel = True End Sub

You can find this code in the NoData event of rptProjectBillingsByWorkCode in CHAP10.MDB on the sample code CD-ROM. In case no data is returned by the report's underlying recordset, a message is displayed to the user, and Cancel is set equal to True. This exits the report without running it.

The Page Event

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.

Listing 10.6 The Page Event
Private Sub Report_Page() 'Draw a red line starting in the upper left-hand corner 'and going to the lower right-hand corner Me.Line (0, 0)-(Me.ScaleWidth - 30, Me.ScaleHeight - 30), _ RGB(255, 0, 0), B End Sub

You will find this code in the rptTimeSheet report, in CHAP10.MDB. It draws a red line on the report, starting in the upper-left corner and going to the lower-right corner. It uses the ScaleWidth and ScaleHeight properties to determine where the lower-right corner of the report's printable area is. The B in the third parameter creates a rectangle by using the coordinates as opposite corners of the rectangle.

The Error Event

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.

Listing 10.7 The Error Event
Private Sub Report_Error(DataErr As Integer, Response As Integer) 'If Data Source Not Found error occurs, display message 'To test this, rename qryTimeSheet If DataErr = 2580 Then MsgBox "Record Source Not Available for this Report" Response = acDataErrContinue End If End Sub

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.