The host application doesn’t fail (and cause data loss) because of an
error in the program.
Instead of using default error handling, the program provides robust
error handling that leaves the operating system in a stable state.
Writing your own error-handling code
Adding an error handler to your program is a two-part process. First, you
must tell VBA that you’ve included an error handler. Otherwise,
VBA uses the
default error handler, even when error-handler code appears in the program.
Second, you must provide the error-handling code.
Listing 6-2 shows an
example of a
Sub
that uses error handling. (You can find the source code for
this example on the Dummies.com site at
http://www.dummies.com/go
/vbafd5e
.)
Listing 6-2
Defining a Custom Error Handler
Public Sub ErrorHandle()
‘ The variable that receives the input.
Dim InNumber As Byte
‘ Tell VBA about the error handler.
On Error GoTo MyHandler
‘ Ask the user for some input.
InNumber = InputBox(“Type a number between 1 and “ + _
“10.”, “Numeric Input”, “1”)
‘ Determine whether the input is correct.
If (InNumber < 1) Or (InNumber > 10) Then
‘ If the input is incorrect, then raise an error.
Err.Raise vbObjectError + 1, _
“ErrorCheck.ErrorCondition.ErrorHandle”,
_
“Incorrect Numeric Input. The number “ +
_
“must be between 1 and 10.”
Else
‘ Otherwise, display the result.
MsgBox “The Number You Typed: “ + CStr(InNumber),
_
vbOKOnly Or vbInformation, _
“Successful Input”
End If
‘ Exit the Sub.
144
Part II: Learning the Ropes
11_046500 ch06.qxp 12/5/06 5:35 PM Page 144
Exit Sub
‘ The start of the error handler.
MyHandler:
‘ Display an error message box.
MsgBox “The program experienced an error.” + vbCrLf +
_
“Error Number: “ + CStr(Err.Number) + vbCrLf +
_
“Description: “ + Err.Description + vbCrLf + _
“Source: “ + Err.Source, _
vbOKOnly Or vbExclamation, _
“Program Error”
‘ Always clear the error after you process it.
Err.Clear
End Sub
This example introduces a few new VBA features that you haven’t seen in pre-
vious examples. Notice the use of the
GoTo
statement. You can use a number
of
forms of
On Error
, but the
GoTo
form shown here is the most common.
Another common form is
On Error Resume Next
, which tells VBA to
ignore the line of code with a problem and continue with the next line. The
problem with this second form is that you haven’t really handled the error. It
remains unreported and could cause a severe crash later.
Never use the
On Error GoTo 0
statement. This
statement turns off error
handling, which means that errors go unhandled and VBA doesn’t even tell
you about them. This setting can cause a range of problems, including appli-
cation failure and data loss.
The
InputBox
function is an interesting way to get hold of one piece of infor-
mation. This example uses the
InputBox
function
for testing, which is the
most common way that I’ve seen it used. In general, you never use the
InputBox
function in a program because you normally need more than one
input. The first argument
prompts for information, the second contains a
default value, and the third provides a title for the dialog box. You can also
include arguments for placement of the dialog box onscreen and provide a
help context.
Notice the
InNumber
data type. Using this data type ensures that you can’t
input a letter or special character without generating an error. If you try to
input
the wrong information, VBA detects the error and generates an error
number 13 (type mismatch). The
Byte
data type also limits the acceptable
input range. If you overflow the buffer by using a number that’s too large,
VBA generates error 6 (overflow). You can detect these errors in your error
handler and take appropriate action.
145
Do'stlaringiz bilan baham: