Err Object
The Err object contains information about your run-time errors.
This object has global scope which means that it can be referenced/used from anywhere.
However the value/error information is only available inside the current procedure, when you leave that procedure the object is reset.
This object is automatically reset when execution moves to a different subroutine or function.
This object is automatically reset when any of the following statements are executed: "On Error", "Resume" or "Exit".
Property | Description |
Number | The error number. If this is zero then no error has occurred. |
Description | A short description of the error |
Source | The project name from the Properties dialog box. |
HelpContext | The context ID for a particular error in a help file. |
HelpFile | A folder location and filename of the help file. |
LastDllError | A system error code produced by a call to a DLL or external library (i.e. an API call) |
Err.Clear Method
The clear method can be used explicitly to reset the properties, which is equivalent to "Err.Number = 0".
Err.Clear
This method only needs to be used in conjunction with "On Error Resume Next" when you are performing more than one error check inside the same subroutine.
Public Sub MultipleRuntimeErrors_Resume()
Dim iInteger As Integer
On Error Resume Next
iInteger = 40000
If (Err.Number <> 0) Then
Debug.Print "Overflow"
Err.Clear
End If
Debug.Print 1 / 0
If (Err.Number <> 0) Then
Debug.Print "Division by Zero"
Err.Clear
End If
End Sub
Using "On Error Resume Next" is not recommended because there is a risk of catching the previously handled error.
Passing an ErrObject
It is possible to pass in an ErrObject into a subroutine.
Public Sub MySubroutine()
On Error GoTo ErrorHandler
Debug.Print 1 / 0
ErrorHandler:
Call Error_Handle("MySubroutine", Err)
End Sub
Public Sub Error_Handle(ByVal sProcedureName As String, _
ByVal oErr As ErrObject)
Call MsgBox("Number: " & Err.Number & vbNewLine & _
"Description: " & Err.Description & vbNewLine & _
"Source: " & Err.Source)
End Sub
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext