9.6. More on Absorbing an Expected Error: Example

As stated earlier in this chapter, sometimes a normal activity in your application will result in Access encountering an error. For example, if the code behind a report cancels the On Open event, Access will display an error message. This is common when you display a form to gather selection criteria during the On Open event of a report. If the user clicks Cancel on the dialog box, the Cancel parameter is set to True and the Open event of the report is cancelled. This technique is described in Chapter 14.

Since this is a normal event, your user shouldn't see an error message. Your application should just continue as though nothing happened. The code in the Open event of the report will look something like this:

Private Sub Report_Open(Cancel As Integer)
On Error GoTo Error_Handler
Me.Caption = "My Application" DoCmd.OpenForm FormName:="frmReportSelector_MemberList", _ WindowMode:=acDialog 'Cancel the report if "cancel" was selected on the dialog form. If Forms!frmReportSelector_MemberList!txtContinue = "no" Then Cancel = True GoTo Exit_Procedure End If Me.RecordSource = ReplaceWhereClause(Me.RecordSource, Forms!frmReportSelector_MemberList!txtWhereClause) Exit_Procedure: Exit Sub Error_Handler: MsgBox "An error has occurred in this application. " _ & "Please contact your technical support person and " _ & "tell them this information:" _ & vbCrLf & vbCrLf & "Error Number " & Err.Number & ", " _ & Err.Description, _ Buttons:=vbCritical, ...

Get Access 2003 VBA Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.