Part III: More-Advanced Access Techniques
834
If the user selects the Yes button (vbYes), the Resume statement forces processing back to the
Kill statement. The cycle repeats itself until either the Temp.txt file becomes available and is
deleted or until the user clicks the No button on the message box.
Resume Next
When your error handler corrects or works around the problem that caused the error, the Resume
Next statement may be used. Resume Next returns execution to the statement immediately fol-
lowing the line at which the error occurred.
The assumption with Resume Next is that either the error handler corrected the error condition
or that the error was relatively minor in nature and that it’s appropriate for processing to simply
continue at the statement following the error condition.
The following procedure shows how to use On Error Resume Next. As you saw earlier, this sim-
ple routine tries to delete a temporary file. If the file does not exist, an error is thrown and process-
ing drops to the error handler. Within the error handler, if the error number is 53 (“File not
found”), processing resumes at the first executable statement following Kill:
Public Sub ResumeNextDemo()
On Error GoTo HandleError
‘Statement causing error occurs here:
Kill “C:\Temp.txt”
‘Other processing goes here
ExitHere:
Exit Sub
HandleError:
‘Error 53 = “File not found”
If Err.Number = 53 Then
Resume Next
Else
‘Handle other errors here
End If
End Sub
This little demonstration illustrates how a single On Error GoTo statement, plus a little decision
logic in the error handler, can protect the procedure from all kinds of errors, yet allows the code to
simply ignore error 53 because this particular error is inconsequential to the application’s execution.
Resume Label
Resume Label is the standard method for exiting an error handler. If you need to continue execu-
tion at someplace other than the line causing the error or the line immediately after the statement
that caused the error, you should use the Resume Label statement. Resume Label directs execu-
tion to the location specified by the label argument.
The label must appear within the current procedure. You can’t resume execution at a point outside
of the currently executing procedure. You can certainly call other procedures from within an error
handler, but execution always returns to the current procedure.