• Run-time errors are trappable. That is, Visual
Basic recognizes an error has occurred and enables you to trap it and take
corrective action. If an error occurs and is not trapped, your program will usually
end in a rather unceremonious manner.
• Error trapping is enabled with the On Error statement:
On Error GoTo errlabel
Yes, this uses the dreaded GoTo statement! Any time a run-time error occurs
following this line, program control is transferred to the line labeled errlabel.
Recall a labeled line is simply a line with the label followed by a colon (:).
• The best way to explain how to use error trapping is to look at an
outline of an example procedure with error trapping.
[Declare variables, ...]
On Error GoTo HandleErrors
Error handling code]
Once you have set up the variable declarations, constant definitions, and
any other procedure preliminaries, the On Error statement is executed to enable
error trapping. Your normal procedure code follows this statement. The error handling
code goes at the end of the procedure, following the HandleErrors statement label.
This is the code that is executed if an error is encountered anywhere in the Sub
procedure. Note you must exit (with Exit Sub) from the code before reaching the
HandleErrors line to avoid inadvertent execution of the error handling code.
• Since the error handling code is in the same procedure where an error
occurs, all variables in that procedure are available for possible corrective
action. If at some time in your procedure, you want to turn off error trapping,
that is done with the following statement:
On Error GoTo 0
• Once a run-time error occurs, we would like to know what the error
is and attempt to fix it. This is done in the error handling code.
Basic offers help in identifying run-time errors. The Err object returns,
in its Number property (Err.Number), the number associated with the current error
condition. (The Err function has other useful properties that we won’t cover
here - consult on-line help for further information.) The Error() function takes
this error number as its argument and returns a string description of the error.
Consult on-line help for Visual Basic run-time error numbers and their descriptions.
• Once an error has been trapped and some action taken, control must
be returned to your application. That control is returned via the Resume statement.
There are three options:
Resume Lets you retry the operation that caused the error. That is, control
is returned to the line where the error occurred. This could be dangerous in that,
if the error has not been corrected (via code or by the user), an infinite loop
between the error handler and the procedure code may result.
Resume Next Program control is returned to the line immediately following
the line where the error occurred.
Resume label Program control is returned to the line labeled label.
• Be careful with the Resume statement. When executing the error handling
portion of the code and the end of the procedure is encountered before a Resume,
an error occurs. Likewise, if a Resume is encountered outside of the error handling
portion of the code, an error occurs.