Using On Error Statements
On Error statements activate error handling. Each routine must contain its own On Error statement if you want that routine to do its own error handling. Otherwise, the VBA compiler cascades error handling up the call stack (the series of routines that executed to get you to this point in code). If the VBA compiler does not find any On Error statements in the call stack, it invokes its own error handling.Suppose that Func1 calls Func2, and Func2 calls Func3. Only Func1 contains error handling. An error occurs in Func3. Func3 passes control up to Func2. Func2 has no error handling, so it passes control up to Func1. Func1 handles the error. Needless to say, the error handler found in Func1 is not necessarily appropriate to handle the error that occurred in Func3.Using an On Error statement, you can cause the application to branch to error-handling code, resume code execution on the line immediately following the error, or attempt to re-execute the problematic line of code.You must decide the most appropriate response to a particular error. Sometimes it is most appropriate for your application to halt in response to an error. At other times, it is best if the routine skips the offending line entirely. By combining the use of On Error Goto, On Error Resume Next, and the Resume statement, you can handle each error appropriately.
The On Error Goto Statement
The statement On Error Goto <label > tells VBA that, from this point forward in the subroutine or function, if an error occurs, it should jump to the label specified in the statement. This is the most common form of error handling.The label specified in the On Error statement must be located in the current procedure. Listing 16.3 shows a simple example of error handling.
Listing 16.3 An Example of Error Handling Using the On Error GoTo Statement
Sub SimpleErrorHandler(iVar1 As Integer, iVar2 As Integer)
'Invoke error handling
On Error GoTo SimpleErrorHandler_Err
'Declare a variable to hold the result
Dim sngResult As Single
'Divide the first parameter received by the
'second parameter received
sngResult = iVar1 / iVar2
'Exit the subroutine if all went as planned
Exit Sub
SimpleErrorHandler_Err:
'If an error occurs, display a message and exit
'the subroutine
MsgBox "Oops!"
Exit Sub
End Sub
NOTEThis example differs from the code on the CD and will be modified in the next section.You can learn some important things from this simple routine. The routine receives two integer values. It then invokes the error handler. When an error occurs, execution continues at the label. Notice that this routine contains two Exit Sub statements. If you remove the first Exit Sub statement, the code falls through to the label regardless of whether an error occurred. The Exit Sub statement at the bottom gracefully exits the procedure, setting the error code back to 0.
Including the Error Number and Description in the Error Handler
The error-handling code in Using the Err Object." For now, take a look at the Description and Number properties to see how you can use them to enhance an error-handling routine. To display the error number and description, you must modify the error-handling code to look like this:SimpleErrorHandler_Err:
'If an error occurs, display a message and exit
'the subroutine
MsgBox "Error #" & Err.Number & ": " & Err.Description
Exit Sub
This time, instead of hard-coding the error message, you display the error number and VBA's internal error string. Figure 16.3 shows the resulting error message. You can find the SimpleErrorHandler routine and all the following examples in the basError module of the CHAP16EX.MDB database.
Figure 16.3. An error message with an error number and error string.

Using On Error Goto 0
You use On Error Goto 0 for two purposes:
- When you want Access to return to its default error handler
- When you have other error handling in a routine, but want Access to return to the calling routine when a specific condition occurs
Generally, you don't want Access to return to its default error handler. You might do this only if you are unable to handle the error, or if you are in the testing phase and not yet ready to implement your own error handler.The reason why you want Access to return the error to a higher-level routine is much clearer. You do this if you want to centralize the error handling, meaning that one routine may call several others. Instead of placing error-handling code in each routine that is called, it is appropriate in certain situations to place the error handling in the calling routine.
The On Error Resume Next Statement
On Error Resume Next continues program execution on the line immediately following the error. This construct is generally used when it is acceptable to ignore an error and continue code execution. Listing 16.4 shows an example of such a situation.
Listing 16.4 Ignoring an Error and Continuing Execution
Sub TestResumeNext()
'Instruct VBA to continue on the next line if an error
'occurs
On Error Resume Next
'Attempt to delete a file
Kill "AnyFile"
'If no error occurred, do nothing. Otherwise, display
'a message with the description of the error that occurred
If Err.Number = 0 Then
Else
MsgBox "We Didn't Die, But the Error Was: " & Err.Description
End If
End Sub
You use the Kill statement to delete a file from disk. If the specified file is not found, an error results. You delete the file only if it exists, so you are not concerned about an error. On Error Resume Next is very appropriate in this situation, because resuming execution after the offending line of code does no harm. The example illustrates that, although code execution proceeds, the properties of the error object are still set.