Home

Error Handling

 

The Err Object

 

Introduction

To support error handling, Visual Basic provides a global variable named Err. This allows you to identify the error and its description. Because an error depends on what caused it and why, the values of the Err variable also depend and are not always the same.

The Error Number

When developing Visual Basic for Applications (VBA), those who worked on the project also tried to anticipate as many problems as possible. This allowed them to assist you with the types of errors that you code or your applications may encounter. To make it easier, they assigned a specific number to each type of error. Consequently, when a run-time error occurs in your database, a dialog box would come up and display a number that corresponds to the error. You may have seen these types of numbers in previous lessons or exercises. Here is an example:

The Err object is equipped with a Number property that allows you to identify an error by its number. The Number property is a constant integer. Most of the times, when a run-time error occurs, the above dialog box would show you the error number that occurred. Consider the following example:
Private Sub cmdCalculate_Click()
    Dim Number#
    Dim Twice#
    
    Number = [txtNumber]
    Twice = Number * 2
    [txtResult] = Twice
End Sub

Imagine that the user enters an inappropriate value in the requesting text box:

After clicking the button, an error would produced as follows:

If the user clicks Debug, the line that caused the error would be highlighted:

Notice that, in the previous screenshot, we get the error number. 

The Error Message

Obviously an error number doesn't mean much. To indicate what each error number refers to, the Err object is equipped with a Description property, which is a string. To display this message, you can create an On Error GoTo expression and indicate where to jump if an error occurs. Here is an example:

Private Sub cmdCalculate_Click()
    On Error GoTo cmbCalculate_Error
    Dim Number#
    Dim Twice#
    
    Number = [txtNumber]
    Twice = Number * 2
    [txtResult] = Twice
    
    Exit Sub
cmbCalculate_Error:
    If Err.Number = 13 Then
        MsgBox Err.Description
    End If
End Sub

This time, if the type of error you are anticipating occurs, you can rightfully display the description;

Once again, notice that the type of message of the Err.Description string may not mean much to a regular user. For this reason, you should make it a habit to anticipate as many types of errors that may occur in your application and display more meaningful messages. You can do this in the section where the code would jump when an error occurs.

 

The Source of the Error 

It is assumed that an error would be caused when using your application. In fact, the database on which the user is working when the error occurred is considered as the source of the error. This information can be valuable at times. The application that caused an error is recognized as the Source property of the Err object. Most of the time, you will know this. Still, if you want to get this information, you can access the Source property of the Err object and get this as a string.

 

The Immediate Window

 

Introduction

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

The Immediate window is an object you can use to test functions and expressions. It is available only in Visual Basic (and not in the Microsoft Access side). To ("physically") use 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 an 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 ? following by the name of the function and its arguments, if any. For example, to test the UCase$ function we reviewed in Lesson 5, in the Immediate window, you could type:

? UCase("République d'Afrique du Sud")

After type the function, press Enter. The result would display in the next line:

The Immediate window is recognized in code as the Debug object. To programmatically 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.

 

 

Previous Copyright © 2005-2010 FunctionX, Inc. Next