Databases Reference
In-Depth Information
one error.) Note that if no errors occur, the Errors collection remains as it was before the
operation.
Example 14-9, which deliberately produces an error, illustrates the use of the Errors
collection. It also demonstrates the use of three Error object properties: Number (the
VBA error number), Description (a description in words of the error), and Source (the
object or application that generated the error).
Example 14-9. An Errors collection example
Sub exaErrorsCollection( )
' Note declaration of object variable of type Error
Dim dbsTest As DATABASE
Dim txtError As String
Dim errObj As Error
On Error GoTo ehTest
' A statement that produces an error
Set dbsTest = _
DBEngine.Workspaces(0).OpenDatabase("NoSuchDatabase")
Exit Sub
ehTest:
txtError = ""
' Loop through the Errors collection,
' to get the Number, Description and Source
' for each error object
For Each errObj In DBEngine.Errors
txtError = txtError & Format$(errObj.Number)
txtError = txtError & ": " & errObj.Description
txtError = txtError & " (" & errObj.Source & ")"
txtError = txtError & vbCrLf
Next
MsgBox txtError
Exit Sub
End Sub
Running this code produces the window in Figure 14-12.
Figure 14-12. Error message from executing exaErrorsCollection
Search WWH ::




Custom Search