9.5. More on Absorbing Expected Errors

As stated earlier in this chapter, sometimes a normal activity in your application results in Access encountering an error. For example, if the code behind a report cancels the On Open event, Access displays an error message. Because this is a normal event, your user shouldn't see an error message. Your application should continue as though nothing happened.

The code in the Open event of the report looks 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:
  DisplayUnexpectedError Err.Number, Err.Description

  Resume Exit_Procedure
  Resume

End Sub

An open selection criteria form is shown in Figure 9-8.

Figure 9.8. Figure 9-8

If the user clicks OK, the form is hidden and the report's On Open code continues. It adds the selection criteria to the report's RecordSource property and displays the report. However, if the user clicks Cancel, the form sets a hidden Continue text box to no before it ...

Get Access™ 2007 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.