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.
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 your 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 of a run-time error number 2489: 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 be 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.
Obviously an error number does not 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. 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.
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. 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 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 we reviewed in Lesson 5, 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 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.
Here is an example: Private Sub cmdModifyPersons_Click() On Error GoTo cmdModifyPersons_Error Dim curDatabase As DAO.Database Dim tblPersons As DAO.TableDef ' Get a reference to the current database Set curDatabase = CurrentDb ' Get a reference to a table named Customers Set tblPersons = curDatabase.TableDefs("Persons") tblPersons.Fields.Delete "FullName" Exit Sub cmdModifyPersons_Error: If Err.Number = 3265 Then MsgBox "The column you are trying to delete doesn't exist on the table" ElseIf Err.Number = 3211 Then MsgBox "Before deleting the column, please close the table first " & _ "and make sure nobody is using it" End If Resume Next End Sub
|
|
|||||||||||||||||
|