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, title:="My Application"

Resume Exit_Procedure Resume

End Sub

An open selection criteria form is shown in 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 is hidden. If the report sees a "no" in this text box, it cancels itself by setting Cancel = True.

If you set the Cancel parameter to True in a report's On Open procedure, an error will be returned out to the calling code, and if it isn't handled you'll see an error, as shown in Figure 9-9.

An error lias occurred in this application. Please contact your technical support person and tell them this information:

Error Number 2501, The OpenReport action was canceled.

Figure 9-9

Now this is one ugly error message. For Access to continue without inflicting it on your poor user, you must check for this particular error (in this case, 2501) and absorb it by doing nothing but exiting the procedure. The following code shows how to absorb this error:

Private Sub cmdPreview_Click() On Error GoTo Error_Handler

If Me!lstReport.Column(3) & "" <> "" Then

DoCmd.OpenReport ReportName:=Me!lstReport.Column(3), _ View:=acViewPreview End If

'Update the Last Run Date of the report DoCmd.Hourglass True DoCmd.SetWarnings False DoCmd.RunSQL _

"UPDATE tsysReport SET tsysReport.DtLastRan = Date() " & _ "WHERE tsysReport.RptKey = " & Me.lstReport DoCmd.SetWarnings True DoCmd.Hourglass False

Exit_Procedure: Exit Sub

Error_Handler:

If Err.Number = 2 501 Then Resume Exit_Procedure

Else

On Error Resume Next DoCmd.SetWarnings True DoCmd.Hourglass False

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, title:="My Application"

Resume Exit_Procedure

Resume

End If

End Sub

In the above code, you are telling Access that if error 2501 is encountered, it should be ignored. Access will not display an error message, and will instead exit the procedure immediately. However, if any errors other than 2501 occur, the code will continue through to the Else statement and use your normal error handling logic.

If you have several expected error codes that you want to quietly absorb, you can either add them to the If statement using Or, like this:

If Err.Number = 2501 Or Err.Number = 245 0 Then

Or, if you want to take different actions for each error, you can use a Select Case statement, like this:

Select Case Err.Number

Case 2501 'report was cancelled

Resume Exit_Procedure Case 2450 'form is no longer loaded

Resume Next Case Else

...normal error handling End Select

In this example, when the report is cancelled (error 2501) Access will jump directly to Exit_Procedure, but if it encounters a form that is not loaded (error 2450), it will use Resume Next to ignore the error and continue with the next line of code.

While you are getting familiar with including error handling in every procedure, or if you aren't sure which error numbers need special handling, just include the basic error handling with the extra Resume. As specific expected errors pop up during your development and testing, you can add the code to handle and absorb them.

0 0

Post a comment