Setting an Error Trap

Visual Basic enables an error trap whenever it encounters the On Error statement, which specifies an error handler (the routine within a procedure that handles the error). The error trap remains enabled while the procedure that contains it is active — that is, until an Exit Sub, Exit Function, End Sub, or End Function statement is run in that procedure.

Although only one error trap can be enabled at any one time in any given procedure, you can create several alternative error traps and enable different ones at different times. You can also disable an error trap by using a special form of the On Error statement — On Error GoTo 0. For more information about disabling error handling, see "Turning Off Error Handling" later in this chapter.