Microsoft Access 2010 Bible

(Rick Simeone) #1

Chapter 23: Handling Errors and Exceptions


829


Obviously, the Select Case construct is not the only way to handle multiple error conditions.
You could, for instance, use nested If...Then...Else and If...Then...ElseIf state-
ments. However, you’ll find that the If statement is not easily extensible and the logical flow
through nested If...Then...Else statements can be difficult to follow.


In the “Resume Label” section, later in this chapter, you’ll read about the special Resume statement
you use to redirect program flow out of the error handler.


VBA error-handling statements


You’ve already seen several examples of the basic VBA statements for handling errors:


l On Error

l (^) Resume
There are a number of forms of the On Error statement:
l (^) On Error GoTo Label
l On Error GoTo 0
l (^) On Error Resume Next
As you’ll see in the section “VBA Resume statements” later in this chapter, there are also a number
of forms of the Resume statement:
l Resume
l (^) Resume Next
l Resume Label
An error handler is a section of code that is executed when some kind of an error occurs. The exact
error can be specified or it can be general. Essentially, when an error is detected by an error trap,
then the action defined by the error handler is taken.
There are numerous ways to deal with errors within forms, reports, and code. Each form and
report, as well as each function and subroutine, can (and probably should) have an error-handling
routine. It isn’t unusual to see a good part of the development effort devoted to error handling.
Probably the most common routine is the following:
Function SampleCode
‘Dim statements here
On Error GoTo HandleError
‘Insert functional code here
ExitHere:
‘Cleanup code goes here
Exit Function
HandleError:
‘Error handler code here

Free download pdf