To assist you with handling errors, the Visual Basic
language provides a class named Err. You don't
have to declare a variable for this class. An Err object is readily available as
soon as you you start working on VBA code and you can directly access its members.
As mentioned already, there are various types of errors that
can occur to your program. To assist you with identifying them, the Err
object is equipped with a property named Number. This property holds a specific
number to most errors that can occur to your program. When your program runs and
encounters a problem, it may stop and display the number of the error. Here is
an example:
As you can see, this is error number 13. Because there are
many types of errors, there are also many numbers, so much that we cannot review
all of them. We can only mention some of them when we encounter them.
When a program runs, to find out what type of error
occurred, you can question the Number property of the Err object to
find out whether the error that has just occurred holds this or that number. To
do this, you can use an If...Then conditional statement to check the
number. You can then display the necessary message to the
user. Here is an example:
Private Sub cmdCalculate_Click()
On Error GoTo WrongValue
Dim HourlySalary As Double, WeeklyTime As Double
Dim WeeklySalary As Double
' One of these two lines could produce an error, such as
' if the user types an invalid number
HourlySalary = CDbl(txtHourlySalary)
WeeklyTime = CDbl(txtWeeklyTime)
' If there was an error, the flow would jump to the label
WeeklySalary = HourlySalary * WeeklyTime
txtWeeklySalary = FormatNumber(WeeklySalary)
Exit Sub
WrongValue:
If Err.Number = 13 Then
MsgBox "You typed an invalid value"
HourlySalary = 0
WeeklyTime = 0
Resume Next
End If
End Sub
As mentioned already, there are many errors and therefore
many numbers held by the Number property of the Err object. As a result, just
knowing an error number can be vague. To further assist you with decrypting an
error, the Err object provides a
property named Description. This property holds a (usually short) message
about the error number. This property works along with the Number
property holding the message corresponding to the Number property.
To get the error description, after inquiring about the
error number, you can get the equivalent Description value. Here is an example:
Private Sub cmdCalculate_Click()
On Error GoTo WrongValue
Dim HourlySalary As Double, WeeklyTime As Double
Dim WeeklySalary As Double
' One of these two lines could produce an error, such as
' if the user types an invalid number
HourlySalary = CDbl(txtHourlySalary)
WeeklyTime = CDbl(txtWeeklyTime)
' If there was an error, the flow would jump to the label
WeeklySalary = HourlySalary * WeeklyTime
txtWeeklySalary = FormatNumber(WeeklySalary)
Exit Sub
WrongValue:
If Err.Number = 13 Then
MsgBox Err.Description
HourlySalary = 0
WeeklyTime = 0
Resume Next
End If
End Sub
In some cases, the error message
will not be explicit enough, especially if a user simply reads it to you over
the phone. The alternative is to create your own message in the language you
easily understand, as we did earlier. If you want, you can also display a
message that combines both the error description and your own message. Here is an example:
Private Sub cmdCalculate_Click()
On Error GoTo WrongValue
Dim HourlySalary As Double, WeeklyTime As Double
Dim WeeklySalary As Double
' One of these two lines could produce an error, such as
' if the user types an invalid number
HourlySalary = CDbl(txtHourlySalary)
WeeklyTime = CDbl(txtWeeklyTime)
' If there was an error, the flow would jump to the label
WeeklySalary = HourlySalary * WeeklyTime
txtWeeklySalary = FormatNumber(WeeklySalary)
Exit Sub
WrongValue:
If Err.Number = 13 Then
MsgBox Err.Description & ": The value you typed cannot be accepted."
HourlySalary = 0
WeeklyTime = 0
Resume Next
End If
End Sub
Most of the time, you will know what caused an error,
since you will have created the application. The project that causes an
error is known as the source of error. In some cases, you may not be able
to easily identify the source of error. To assist you with this, the Err
object is equipped with a property named Source.
To identify the application that caused an error, you can inquire
about the value of this property.
Debugging consists of examining and testing portions
of your code or parts of your application to identify problems that may
occur when somebody is using your database. Microsoft Visual Basic
provides as many tools as possible to assist you with this task.
The Immediate window is an object you can use to test
functions and expressions. To display the
Immediate window, on the main menu of Microsoft Visual Basic, you can
click View -> Immediate Window. It's a habit to keep the Immediate
window in the bottom section of the Code Editor but you can move it from
there by dragging its title bar:
Probably the simplest action you can perform in the
Immediate window consists of testing an expression. For example, you can
write an arithmetic operation and examine its result. To do this, in the
Immediate window, type the question mark "?" followed by the
expression and press Enter. Here is an example that tests the result of 275.85 + 88.26:
One of the most basic actions you can perform in the
Immediate window consists of testing a built-in function. To do this, type
? followed by the name of the function and its arguments, if any. For
example, to test the UCase$ function, in the
Immediate window, you could type:
? UCase("République d'Afrique du Sud")
After typing the function and pressing Enter, the result would
display in the next line:
The
Immediate window is recognized in code as the Debug object. To
programmatically display something, such as a string, in the Immediate window, the
Debug object provides the Print method. The simplest way to use it
consist of passing it a string. For example, imagine you create a button
on a form, you name it cmdTestFullName and initialize it with a string.
Here is an example of how you can display that string in the Immediate
window:
Private Sub cmdTestFullName_Click()
Dim strFullName$
strFullName$ = "Daniel Ambassa"
Debug.Print strFullName$
End Sub
When you click the button, the Immediate window would
display the passed string:
In the same way, you can create a more elaborate
expression and test its value in the Immediate window.
You can also pass a value, such as a date, that can
easily be converted to a string.
|
|
||||||||||||||||||||
|
|
Error Handling
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment