Word Document Properties


Chapter 6: Trapping Errors and Squashing Bugs



Download 8,82 Mb.
Pdf ko'rish
bet128/207
Sana16.03.2022
Hajmi8,82 Mb.
#496217
1   ...   124   125   126   127   128   129   130   131   ...   207
Bog'liq
VBA For Dummies 5th Ed.pdf ( PDFDrive )

Chapter 6: Trapping Errors and Squashing Bugs
11_046500 ch06.qxp 12/5/06 5:35 PM Page 143


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

Download 8,82 Mb.

Do'stlaringiz bilan baham:
1   ...   124   125   126   127   128   129   130   131   ...   207




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish