Chapter 23: Handling Errors and Exceptions
831
here — HandleError and ExitHere — were chosen simply because of the obvious purposes
they serve. Using the same labels in all your VBA code makes it very easy to simply copy and paste
the basic error-handling statements into all your procedures.
On Error GoTo 0
The On Error GoTo 0 statement disables error handling and returns Access to its default error-
notification behavior. This statement also resets the properties of the Err object and defeats what-
ever error trap has been set in a procedure. The following procedure shows an example of using
GoTo 0. After processing has bypassed the Delete method, the On Error GoTo 0 statement
disables further error traps. This means any errors that occur after this statement will be handled
by the default VBA error mechanism:
Sub DeleteTableDef()
Dim db As DAO.Database
Set db = CurrentDb()
‘Resume Next is described in the next section:
On Error Resume Next
‘The following statement throws an
‘error if tblTemp does not exist:
db.TableDefs.Delete “tblTemp”
On Error GoTo 0
‘More code here
End Sub
Although in most cases it is not desirable to let VBA handle errors, one situation where you may
choose to use On Error GoTo 0 is during the development process. Assume you’re working on a
complex procedure that has a number of different failure modes. You’re never really sure you’re
trapping for all possible errors, so you may want to disable error handling temporarily so that
you’ll be sure to see all errors that occur past the error trap you’ve prepared. Later, when you’re
sure that you’ve captured all the possible errors thrown by the procedure, remove the On Error
GoTo 0 statement and allow your error trap to handle errors within the procedure.
On Error Resume Next
The On Error Resume Next statement instructs Access to ignore errors in the code following the
statement. Any errors that occur in the code following On Error Resume Next are simply
ignored. No error-handling routine is called. The On Error Resume Next statement is useful if
your code can safely ignore errors in the code immediately following the statement. In the two pro-
cedures that follow, should any error occur while attempting to delete the error log file, the rest of
the routine continue to execute:
Sub DeleteTempFile()
On Error Resume Next
Kill “C:\Temp.txt”
End Sub