WORKING WITH THE ERRORS COLLECTION

The Errors collection stores the most recent set of multiple errors that can occur with features such as ODBC and DAO. In the DAO hierarchy, the Errors collection hangs off the DBEngine object at the top of the heap (see Figure 7.6).

Figure 7.6. The Errors collection hangs off the DBEngine object.


Note

ADO also has an Errors collection that works similarly to DAO's, with just a couple of differences in properties. The properties discussed in this section will be the same in ADO.


The Errors collection contains one method, Refresh, and one property, Count, which is the number of the Error objects the collection contains. The Errors collection is made up of Error objects, which have some of the same properties as the Err object (see Table 7.2). Error objects have no methods.

Table 7.2. The Properties of Error Objects
Property Description
Description Contains the description of the error that occurred, 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.
Number Contains the number of the error that's 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.

Like other Access collections, the Errors collection's index base starts with Error(0) and goes to Error(Error.Count-1).

Note

Because the Errors collection deals only with DAO errors, you need to check the Error(0) member to see whether it matches the error that shows up in the Err object. If it does, you can print out all the errors that make up the main error.


On the TestErrors form is another button, cmdTestErrorCollection, which intentionally causes a DAO error by looking for a database that doesn't exist. The code to cause this error is on the OnEvent event (see Listing 7.8).

Listing 7.8. VideoApp.mdb: Looking for Multiple Errors
Private Sub cmdTestErrorCollection_Click()
   Dim apRoutineError As String
   apRoutineError = "cmdTestErrorCollection_Click"

   Dim dbDummy As Database

   On Error GoTo Error_cmdTestErrorCollection_Click

   '-- Purposefully set off a DAO file not found error
   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

The ap_ErrorLog routine checks this error to see whether it's a DAO error. This routine is listed fully in the next section, but in Listing 7.9 you see the part of it that handles examining the Errors collection and creating a string of all DAO errors that have occurred.

In Listing 7.9 is a reference to a variable named errCurrent. This variable is declared an Error type variable in the code line that reads Dim errCurrent As Error. (This line is shown later, in Listing 7.14, along with the complete code listing.)

Listing 7.9. VideoApp.mdb: Logging an Error
tblErrors.AddNew

      tblErrors!Module = strModule
      tblErrors!Routine = strRoutine
      tblErrors!ErrNumber = lngErrorNo
      tblErrors!ErrMessage = strErrorMsg
      tblErrors!Form = strFormName
      tblErrors!Control = strControlName
      tblErrors!User = CurrentUser()
      tblErrors!Occured = Now

      '-- Test for DAO Errors
      Errors.Refresh

      If Errors.Count > 0 Then

           '-- Make sure the current error in the Errors Collection
           '-- matches the current error
           If Errors(0).Number = lngErrorNo Then

                '-- Concatenate the errors into a string
                strTempErrs = ""

                '--Loop through the Errors collection
                For Each errCurrent In Errors
                   strTempErrs = strTempErrs & Str(errCurrent.Number) & _
                      " - " & errCurrent.Description & vbCrLf
                Next errCurrent

                '-- Now put it in the table
                tblErrors!DAOErrors = strTempErrs

            End If
      End If
tblErrors.UPDATE

This routine uses the Err object to record most of the information for the main error, starting with the following:

'-- Test for DAO Errors
Errors.Refresh

These events take place:

  1. The Errors collection is refreshed.

  2. The routine checks to see whether an Error object is in the Errors collection.

  3. It sees whether the first error in the collection (Errors(0)) matches the error found in the Err object. If it does, the routine cycles through all the errors in the Errors collection and concatenates them in a string.

  4. The routine places the string in the DAOErrors table field.

The table in Figure 7.7 shows strings in the DAOErrors field for the error that's indeed a DAO error.

Figure 7.7. When a DAO error occurs, the Errors collection is parsed and placed in the DAOErrors field.


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

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