Using the Error Object

The Errors collection contains Error objects that represent the Microsoft Jet errors that have occurred. You can use these objects to determine if errors have occurred, and then to work around them. The following code attempts to open a database that doesn’t exist, and then displays the errors that result:

Dim dbs As Database
Dim errX As Error

' Disable error handling.
On Error Resume Next

' Try to open a nonexistent database.
Set dbs = OpenDatabase("XYZ123.456")

' Look at the errors generated.
For Each errX In DBEngine.Errors
	Debug.Print errX.Description
	Debug.Print errX.Number
	Debug.Print errX.Source
Next errX

The Error object is unlike the error variables and functions in Visual Basic in that more than one error can be generated by a single operation. Also, objects in the Errors collection are appended in a manner different from the other DAO collections. The most detailed errors are placed at the end of the collection, and the most general errors are placed at the beginning.

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-highest level, and so on. 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 can’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.