Class Error (DAO VBA)

Error object contains details about data access errors, each of which pertains to a single operation involving DAO. To use a Error class variable it first needs to be instantiated, for example


Dim err as Error
Set err = Errors(Item:=1)

Description

Returns a descriptive string associated with an error. This is the default property for the Error object.

The Description property comprises a short description of the error. Use this property to alert the user about an error that you cannot or do not want to handle.


Sub DescriptionX() 
 
 Dim dbsTest As Database 
 
 On Error GoTo ErrorHandler 
 
 ' Intentionally trigger an error. 
 Set dbsTest = OpenDatabase("NoDatabase") 
 
 Exit Sub 
 
ErrorHandler: 
 Dim strError As String 
 Dim errLoop As Error 
 
 ' Enumerate Errors collection and display properties of 
 ' each Error object. 
 For Each errLoop In Errors 
 With errLoop 
 strError = _ 
 "Error #" & .Number & vbCr 
 strError = strError & _ 
 " " & .Description & vbCr 
 strError = strError & _ 
 " (Source: " & .Source & ")" & vbCr 
 strError = strError & _ 
 "Press F1 to see topic " & .HelpContext & vbCr 
 strError = strError & _ 
 " in the file " & .HelpFile & "." 
 End With 
 MsgBox strError 
 Next 
 
 Resume Next 
 
End Sub 
 

HelpContext


Dim err As DAO.Error: Set err = 
err.HelpContext

HelpFile


Dim err As DAO.Error: Set err = 
err.HelpFile

Number

Returns a numeric value specifying an error.

Use the Number property to determine the error that occurred. The value of the property corresponds to a unique trap number that corresponds to an error condition.


Sub DescriptionX() 
 
 Dim dbsTest As Database 
 
 On Error GoTo ErrorHandler 
 
 ' Intentionally trigger an error. 
 Set dbsTest = OpenDatabase("NoDatabase") 
 
 Exit Sub 
 
ErrorHandler: 
 Dim strError As String 
 Dim errLoop As Error 
 
 ' Enumerate Errors collection and display properties of 
 ' each Error object. 
 For Each errLoop In Errors 
 With errLoop 
 strError = _ 
 "Error #" & .Number & vbCr 
 strError = strError & _ 
 " " & .Description & vbCr 
 strError = strError & _ 
 " (Source: " & .Source & ")" & vbCr 
 strError = strError & _ 
 "Press F1 to see topic " & .HelpContext & vbCr 
 strError = strError & _ 
 " in the file " & .HelpFile & "." 
 End With 
 MsgBox strError 
 Next 
 
 Resume Next 
 
End Sub 
 

Source

Returns the name of the object or application that originally generated the error.

The Source property value is usually the object's class name or programmatic ID. Use the Source property to provide your users with information when your code is unable to handle an error generated in an object in another application. For example, if you access Microsoft Excel and it generates a "Division by zero" error, Microsoft Excel sets Error.Number to the Microsoft Excel code for that error and sets the Source property to Excel.Application. Note that if the error is generated in another object called by Microsoft Excel, Microsoft Excel intercepts the error and still sets Error.Number to the Microsoft Excel code. However, the other Error object properties (including Source) will retain the values as set by the object that generated the error. The Source property always contains the name of the object that originally generated the error. Based on all of the error documentation, you can write code that will handle the error appropriately. If your error handler fails, you can use the Error object information to describe the error to your user, using the Source property and the other Error properties to give the user information about which object originally caused the error, the description of the error, and so forth.


    Sub DescriptionX() 
     
     Dim dbsTest As Database 
     
     On Error GoTo ErrorHandler 
     
     ' Intentionally trigger an error. 
     Set dbsTest = OpenDatabase("NoDatabase") 
     
     Exit Sub 
     
    ErrorHandler: 
     Dim strError As String 
     Dim errLoop As Error 
     
     ' Enumerate Errors collection and display properties of 
     ' each Error object. 
     For Each errLoop In Errors 
     With errLoop 
     strError = _ 
     "Error #" & .Number & vbCr 
     strError = strError & _ 
     " " & .Description & vbCr 
     strError = strError & _ 
     " (Source: " & .Source & ")" & vbCr 
     strError = strError & _ 
     "Press F1 to see topic " & .HelpContext & vbCr 
     strError = strError & _ 
     " in the file " & .HelpFile & "." 
     End With 
     MsgBox strError 
     Next 
     
     Resume Next 
     
    End Sub