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).
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.
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).
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.)
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:
The Errors collection is refreshed.
The routine checks to see whether an Error object is in the Errors collection.
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.
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.
18.223.106.100