O'Reilly logo

Access 2003 VBA Programmer's Reference by Armen Stein, Graham Seach, Teresa Hennig, Patricia Cardoza

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required