WORKING WITH THE ERR AND ERROR OBJECTS

There are different ways to use the Err object (used by Access) and Error objects (used by Jet), as well as the Error statement, which can make them confusing. It's best to examine them in the order that you'll use them.

In Access 97, the Err object contains information about the most recent error that has occurred. The Err object takes the place of the Err statement and Error$ in versions before Access 95. The Err object contains properties and methods, as do other Access objects. Table 7.1 shows the properties belonging to the Err object.

Table 7.1. Properties Used with the Err Object
Property Description
Description Contains the description of the error if a description exists.
HelpContext Stores the context ID for the VB help file.
HelpFile Stores the path and filename of the VB help file.
LastDLLError Returns the last error in a 32-bit DLL call. For more information on dynamic link libraries (DLLs), see Chapter 15, “Using API Calls to Extend the Power of Access.”
Number Contains the number of the error that has been set in the Error object. This is the default property for the Err object.
Source Reflects the system in which this error occurred. Most Access errors set the Source property to MSAccess. When Automation is used, if another application (such as Excel) caused the error, the Source property is set to that application.

The Err object has two methods: Clear and Raise.

The Err Object's Clear Method

The Clear method allows you to clear the last error that's now in the Err object. With this method, you can retry an action in a loop that might cause an error. After the error occurs, you might want to start at the top of the loop and check the error again. So, at the bottom of the loop, you perform an Err.Clear to set the Err.Number object back to 0.

Note

Whenever you have Access perform an Exit Sub|Function to exit a routine, perform one of the Resume statements, or use another On Error statement, the Err object is cleared automatically.


The Err Object's Raise Method

The Raise method allows you to generate a runtime error on purpose. This is useful when debugging an application or creating a custom error. Properties for this method can include the same properties used for the Err object itself: Description, HelpContext, HelpFile, Number, and Source.

Note

For compatibility purposes, you can still use the Error statement, but the Err.Raise method is the preferred method in VBA.


Listing 7.6 shows the Raise method used to test the centralized error handler discussed later in the section “Creating a Centralized Error-Handling Routine.” You can find this code on the TestErrors form in the VideoApp.mdb database, which is found on the accompanying CD-ROM in the Examples folder. The cmdTestRaiseError_Click() procedure is attached to the cmdTestCustomError command button's OnEvent event.

Listing 7.6. VideoApp.mdb: Raising an Error on Purpose
Private Sub cmdTestRaiseError_Click()

   Dim apRoutineError As String
   apRoutineError = "cmdTestRaiseError_Click"

   On Error GoTo Error_cmdTestRaiseError_Click
   Err.Raise 6 ' Division By Zero Error

   Exit Sub

Error_cmdTestRaiseError_Click:

   apCurrErrNo = Err.Number
   apCurrErrMsg = Err.Description

   ap_ErrorLog apModuleError, apRoutineError, apCurrErrNo, apCurrErrMsg
   Select Case ap_ErrorHandler(apCurrErrNo, apCurrErrMsg)
       Case apTryAgain
           Resume
       Case apExitRoutine
           Exit Sub
       Case apResumeNext
           Resume Next
   End Select

End Sub
   Set dbDummy = DBEngine(0).OpenDatabase("NoDB")

   Exit Sub

Error_cmdTestErrorCollection_Click:

   apCurrErrNo = Err.Number
   apCurrErrMsg = Err.Description

   ap_ErrorLog apModuleError, apRoutineError, apCurrErrNo, apCurrErrMsg
   Select Case ap_ErrorHandler(apCurrErrNo, apCurrErrMsg)
       Case apTryAgain
           Resume
       Case apExitRoutine
           Exit Sub
       Case apResumeNext
           Resume Next
   End Select

End Sub

Private Sub cmdTestRaiseError_Click()

   Dim apRoutineError As String
   apRoutineError = "cmdTestRaiseError_Click"
   On Error GoTo Error_cmdTestRaiseError_Click

   Err.Raise 6 ' Division By Zero Error

   Exit Sub

Error_cmdTestRaiseError_Click:

   apCurrErrNo = Err.Number
   apCurrErrMsg = Err.Description

   ap_ErrorLog apModuleError, apRoutineError, apCurrErrNo, apCurrErrMsg
   Select Case ap_ErrorHandler(apCurrErrNo, apCurrErrMsg)
       Case apTryAgain
           Resume
       Case apExitRoutine
           Exit Sub
       Case apResumeNext
           Resume Next
   End Select

End Sub

Private Sub cmdTestRaiseError_Click()

   Dim apRoutineError As String
   apRoutineError = "cmdTestRaiseError_Click"

   On Error GoTo Error_cmdTestRaiseError_Click

   Err.Raise 6 ' Division By Zero Error

   Exit Sub

Error_cmdTestRaiseError_Click:

   apCurrErrNo = Err.Number
   apCurrErrMsg = Err.Description

   ap_ErrorLog apModuleError, apRoutineError, apCurrErrNo, apCurrErrMsg
   Select Case ap_ErrorHandler(apCurrErrNo, apCurrErrMsg)
       Case apTryAgain
           Resume
       Case apExitRoutine
           Exit Sub
       Case apResumeNext
           Resume Next
   End Select

End Sub

The Raise method also generates user-defined errors. Use Raise when you want to have errors sent through standard error handlers with your own information. You can set up Automation errors in this way.

Tip

You can use the Raise method to test the error handlers you create. Look for this method in action later in the section “Tracking Errors by Using a Custom Error Log.”


To use the Err.Raise method, you need to supply the error information via the properties for the Err object mentioned earlier in Table 7.1. The following code shows constants used for a new error, located in the declarations section of the TestErrors form:

Const apErrNoDatabaseNotFound = 32000
Const apErrMsgDatabaseNotFound = "No Database found in this Directory"

Next, this routine asks users to supply a folder where the database can be found. If the database isn't found in this folder, the routine uses the new error associated with apErrNoDatabaseNotFound. The code in Listing 7.7 is also found on the TestErrors form, attached to the OnEvent event of the TestUserDefinedError command button.

Listing 7.7. VideoApp.mdb: Using Custom Error Messages
Private Sub cmdTestUserDefinedError_Click()
   Dim strDirToLook As String
   Dim strMDBFound As String

   On Error GoTo Error_cmdTestUserDefinedError_Click

   '-- Looking for the first MDB found
   strDirToLook = InputBox("Please enter a directory:")
   strMDBFound = Dir(strDirToLook & "*.mdb")

   '-- if no mdb found call the standard error handler with our error
   If Len(strMDBFound) = 0 Then
      Err.Raise apErrNoDatabaseNotFound, strDirToLook, _
         apErrMsgDatabaseNotFound
   Else
      MsgBox strMDBFound
   End If

   '-- Other code if desired

   Exit Sub

Error_cmdTestUserDefinedError_Click:

   MsgBox Err.Description, vbCritical, Err.Source
   Exit Sub

End Sub

Figure 7.5 shows the result of running this routine.

Figure 7.5. Creating your own error codes is useful for customizing your application.


Tip

You can use another set of functions for creating custom error messages: CVError() and IsError(). For more information on these functions, see “Creating User-Defined Errors” later in this chapter.


That's it for the properties and methods of the Err object. Access now has a collection for errors that occur while working with Data Access Objects (DAOs) and ActiveX Data Objects (ADO). For more information on DAO and collections in general, see Chapter 5, “Working with Data Access Objects.”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.167.114