Access VBA Macro Programming

(Joao Candeias) #1
If Err.Number = 71 Then
MsgBox "The D drive is not ready"
Else
MsgBox "An error occurred"

End If
End Sub


You saw from the first example that the number for “Drive not ready” came up in the error
message box as 71. The program looks atErr(a system variable that holds the last error number)
and checks to see if it is 71. If it is, it displays the message box, “The D drive is not ready”; if
it is not, it displays the message box, “An error occurred.”


The Resume Statement


TheResumestatement can be added to make the code execution branch back to the statement
where the error occurred. This gives the opportunity for the user to intervene—for example,
to put a CD into drive D and for the code to then reinterrogate drive D. You can include this
in your error-handling routine so the line that created the error will be tried again following
user intervention:


Sub Test_Error()
On Error GoTo err_handler
temp = Dir("d:*.*")
Exit Sub
err_handler:
If Err.Number = 71 Then
MsgBox "The D drive is not ready"
Else
MsgBox "An error occurred"


End If
Resume
End Sub


You can also add the statementNexttoResume. This will skip over the statement that
created the error and thus ignore it.


Sub Test_Error()


On Error Resume Next

temp = Dir("d:\*.*")

End Sub


Chapter 8: Errors and the Error Function 93

Free download pdf