The constant is designed to keep your error codes away from those already assigned in VBA.
If you really want to show the use an error code of 1 or 2 etc then you need to handle that.
Const ERR_ANDY_PROBLEM_1_CODE As Long = vbObjectError + 1
Const ERR_ANDY_PROBLEM_2_CODE As Long = vbObjectError + 2
Const ERR_ANDY_PROBLEM_1_DESC = "Error Problem 1"
Const ERR_ANDY_PROBLEM_2_DESC = "Error Problem 2"
Sub x()
On Error GoTo ErrTest
Err.Raise ERR_ANDY_PROBLEM_1_CODE, "Routine X", ERR_ANDY_PROBLEM_1_DESC
Err.Raise ERR_ANDY_PROBLEM_2_CODE, "Routine X", ERR_ANDY_PROBLEM_2_DESC
Exit Sub
ErrTest:
Select Case Err.Number
Case ERR_ANDY_PROBLEM_1_CODE
MsgBox "Err: " & ERR_ANDY_PROBLEM_1_CODE - vbObjectError & vbLf & Err.Description, vbExclamation, Err.Number
Case ERR_ANDY_PROBLEM_2_CODE
MsgBox "Err: " & ERR_ANDY_PROBLEM_2_CODE - vbObjectError & vbLf & Err.Description, vbExclamation, Err.Number
Case Else
MsgBox "Err:" & Err.Number & vbLf & Err.Description
End Select
Resume Next
End Sub
Although you may argue it is easier for a user to report they encounter error number 1 rather than -2147221503 the really important thing is you/your code knows what the value means.
Bookmarks