Handling Runtime Errors

No matter how thoroughly you test and debug your code, runtime errors appear eventually. Runtime errors are errors that occur when Access executes your code. Use the On Error GoTo instruction to control what happens in your application when a runtime error occurs. On Error is not a very sophisticated instruction, but it is your only choice for error processing in Access modules. You can branch to a label or ignore the error. The general syntax of On Error … follows:

						On Error GoTo
						LabelName
						On Error Resume Next
						On Error GoTo 0

On Error GoTo LabelName branches to the portion of your code with the label LabelName:. LabelName must be a label; it cannot be the name of a procedure. The code following LabelName, however, can (and often does) include a procedure call to an error-handling procedure, such as ErrorProc, as in the following:

						On Error GoTo
						ErrHandler
…
 [RepeatCode:
(Code using ErrProc to handle errors)]
…
GoTo
						SkipHandler
						ErrHandler:
						Call
						ErrorProc
[GoTo RepeatCode]
SkipHandler:
…
(Additional code)

In this example, the On Error GoTo instruction causes program flow to branch to the ErrHandler label that executes the error-handling procedure ErrorProc. Ordinarily, the error-handler code is located at the end of the procedure. If you have more than one error handler or if the error handler is in the middle of a group of instructions, you need to bypass it if the preceding code is error-free. Use the GoTo SkipHandler statement that bypasses ErrHandler: instructions. To repeat the code that generated the error after ErrorProc has completed its job, add a label such as RepeatCode: at the beginning of the repeated code, and then branch to the code in the ErrHandler: code. Alternatively, you can add the keyword Resume at the end of your code to resume processing at the line that created the error.

On Error Resume Next disregards the error and continues processing the succeeding instructions.

After an On Error GoTo statement executes, it remains in effect for all succeeding errors until another On Error GoTo instruction is encountered or error processing is explicitly turned off with the On Error GoTo 0 form of the statement.

If you do not trap errors with an On Error GoTo statement or if you have turned error trapping off with On Error GoTo 0, a dialog with the appropriate error message appears when a runtime error is encountered.

If you do not provide at least one error-handling routine in your Access VBA code for runtime applications, your application quits abruptly when the error occurs.

Detecting the Type of Error with the Err Function

The Err function (no arguments) returns an integer representing the code of the last error or returns 0 if no error occurs. This function is ordinarily used within a Select Case structure to determine the action to take in the error handler based on the type of error incurred. Use the Error$ () function to return the text name of the error number specified as its argument, as in the following example:

ErrorName$ = Error$( Err)
Select Case Err
							Case 58 To 76
        Call FileError 'procedure for handling file errors
    Case 281 To 297
        Call DDEError 'procedure for handling DDE errors
    Case 340 To 344
        Call ArrayError 'procedure for control array errors
End Select
							Err = 0

Some of the error codes returned by the Err function are listed in the Error Codes topic of the Access help file. Choose Help, Contents and Index to display the Help Topics dialog. Then type Error Codes in the text box of the Index page, and press Enter. Double-click Error Codes in the Topics Found dialog to display a help window with an abbreviated list of code numbers and their descriptions. Click the underlined description text to display a window that describes each error code in detail.

You can substitute the actual error-processing code for the Call instructions shown in the preceding example, but using individual procedures for error handling is the recommended approach. The Err statement is used to set the error code to a specific integer. This statement should be used to reset the error code to 0 after your error handler has completed its operation, as shown in the preceding example.

The Error statement is used to simulate an error so that you can test any error handlers you write. You can specify any of the valid integer error codes or create a user-defined error code by selecting an integer that is not included in the list. A user-defined error code returns User-defined error to Error$ ().

Using the Error Event in Form and Report Modules

Access includes an event, Error, that is triggered when an error occurs on a form or report. You can use an event-handling procedure in a form or report to process the error, or you can assign a generic error-handling function in an Access module to the Error event with an =ErrorHandler() entry in order to call the ErrorHandler() function.

When you invoke an error-handling function from the Error event, you need to use the Err function to detect the error that occurred and take corrective action, as described in the preceding section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.168.56