>
Error Object
Description
The Error object contains details about data access errors, each of which pertains to a single operation involving data access objects (DAO).
Remarks
Any operation involving data access objects can generate one or more errors. As each error occurs, one or more Error objects are placed in the Errors collection of the DBEngine object.
When another DAO operation generates an error, the Errors collection is cleared, and the new Error object is placed in the Errors collection. DAO operations that don't generate an error have no effect on the Errors collection.
Elements of the Errors collection aren't appended as they typically are with other collections. The set of Error objects in the Errors collection describes one error. The first Error object is the lowest level error, the second the next higher level, and so forth. For example, if an ODBC error occurs while trying to open a Recordset object, the last Error object contains the DAO error indicating that the object couldn't be opened. The first error object contains the lowest level ODBC error; subsequent errors contain the ODBC errors returned by the various layers of ODBC. In this case, the driver manager, and possibly the driver itself, return separate Error objects.
Note
If you use the New keyword to create an object that subsequently causes an error before that object has been appended to a collection, the DBEngine Errors collection will not contain an entry for that object's error (because it might still contain information from a previous error).
To determine if the error information in the Errors collection is valid, compare the Number property of the first element of the Errors collection (DBEngine.Errors(0)) with the value of the Visual Basic Err object.
Error handling code should examine the Errors collection whenever you anticipate a data access error. If you are writing a centralized error handler, test the Visual Basic Err object. If it matches the Errors(0) object, you can then use a series of Select Case statements to identify the particular data access error or errors that occurred.
Properties
Description Property; HelpContext, HelpFile Properties; Number Property; Source Property.
See Also
Appendix, "Data Access Object Hierarchy."
Example
See the Description property example.
Example (Microsoft Access)
The following example generates an error by attempting to open a Recordset object on a non-existent Students table. Information about the error is stored in both the data access Error object and the Visual Basic Err object. The procedure prints the value of the Description, Source, and Number properties of the Error object. Then it prints the values of the corresponding properties of the Err object.
Note that the first Error object in the Errors collection, Errors(0), should always refer to the same error as the Err object. If it doesn't, information in the Errors collection may be outdated. Use the Refresh method to ensure that the Errors collection includes the most recent error information.
Sub CheckError()
Dim dbs As Database, tdf As TableDef, rst As Recordset
Dim prp As Property
' Declare Error variable for enumeration of Errors collection.
Dim errX As Error
' Ignore errors.
On Error Resume Next
' Clear error in Err object.
Err.Clear
' Refresh Errors collection.
Errors.Refresh
' Return Database variable pointing to current database.
Set dbs = CurrentDb
' Attempt to open Recordset object on nonexistent table.
Set rst = dbs.OpenRecordset("Students")
Debug.Print "DAO Error Object:"
' Print number of errors in Errors collection.
Debug.Print ">>>Number of errors: "; Errors.Count
' Enumerate Errors collection and key properties.
For Each errX In DBEngine.Errors
Debug.Print errX.Description
Debug.Print errX.Source
Debug.Print errX.Number
Next errX
Debug.Print
Debug.Print "VBA Err Object:"
' Display corresponding properties of Err object.
Debug.Print Err.Description
Debug.Print Err.Source
Debug.Print Err.Number
End Sub