Raising Errors

The Err.Raise method can be used to generate run-time errors and can be used instead of the Error statement.
Using the Err object gives more information than the ERROR statement.
If you want to use sound to indicate when something is wrong or incorrect you can use the BEEP statement.


vbObjectError constant

A lot of documentation says if you want to generate a run-time error, you should add your number to the vbObjectError constant.
link - docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/raise-method

Debug.Print VBA.vbObjectError         '-2147221504  
Debug.Print VBA.vbObjectError + 529 '-2147220975
Err.Raise VBA.vbObjectError + 529
Err.Raise VBA.vbObjectError + 529, , "BetterSolutions.com: Description"
alt text

This approach does work in some cases, it can produce some confusing results.
link - stackoverflow.com/questions/56040569/why-do-we-need-to-use-vbobjecterror-constant-when-raising-user-defined-errors-in
For example adding 515 to this constant, produces a native error.

Debug.Print VBA.vbObjectError         '-2147221504  
Debug.Print VBA.vbObjectError + 515 '-2147220989
Err.Raise VBA.vbObjectError + 515
Err.Raise VBA.vbObjectError + 515, , "BetterSolutions.com: Description"
alt text

Recommended Approach

For any user defined errors, the number should be less than 0 or between -10,000,000 and 0.
Instead of using the vbObjectError constant, use -10,000,000.
Err.Raise -10000000 + "Number", , "Description"

Debug.Print -10000000 + 515           '-9999485  

'This generates a custom error, with no description
Err.Raise -10000000 + 515

'This generates the same custom error, but this time, with a description
Err.Raise -10000000 + 515, ,"BetterSolutions.com: Description"
alt text

Err.Raise(Number [, Source] [, Description] [, HelpFile] [, HelpContext])

Number (Long)
Long, identifying the type of the error. Visual Basic errors range from 0 to 65535
The range 0 - 512 is reserved for system errors
The range 513 - 65535 is available for user defined errors
Source (String)
If this is not specified then the programming ID of the current project is used.
Description (String)
This the string that describes the error.
If the error can be mapped to a Visual Basic run-time error the string corresponding to the Number is used.
If there is no Visual Basic error corresponding to the number then "Application-defined or object-defined error" message is used. For example, "Err.Raise 1"
HelpFile
The full path of the Help file in which the error can be found
If unspecified then the fully qualified drive, path and file name of the Visual Basic help file is used.
HelpContext
The context ID that identifies the topic within the corresponding help file.
If omitted then the context ID for the error corresponding to the Number property is used if it exists.


Examples

Sub RaisingErrors() 
On Error GoTo ErrorHandler

    Err.Raise 1 'raises 1 - Application-defined or object-defined error
    Err.Raise 2 'raises 2 - Application-defined or object-defined error
    Err.Raise 3 'raises 3 - Return with GoSub
    Err.Raise 7 'raises 7 - Out of memory
    Err.Raise 513 'raises 513 - Application-defined or object-defined error
    Err.Raise 514 'raises 514 - Application-defined or object-defined error
    Err.Raise 2000 'raises 2000 - Application-defined or object-defined error
    Err.Raise 65535 'raises 65535 - Application-defined or object-defined error
    Err.Raise 655380000 '> 65535 AND < 655380000 raises "5 - Invalid Procedure Call or Argument"

    Err.Raise 0 'raises "5 - Invalid Procedure Call or Argument"
    Err.Raise -1 'raises " -1 - Automation Error"
    Err.Raise -10 'raises " -10 - Automation Error"
    Err.Raise -10000000 'raises " -10000000 - Automation Error"
    Err.Raise -2000000000 'raises "-2000000000 - Automation Error"
    Err.Raise -2147220000 'raises "-2147220000 - Automation Error"
    Err.Raise -2147220975 'raises "-2147220975 - Automation Error"

    Err.Raise -2147220977 '< 2147220977 raises a specific Automation Error"
    Err.Raise -2147220976 'raises "-2147220976 - Automation Error, The owner of the PerUser subscription"

    Err.Raise VBA.vbObjectError 'raises "-2147221504 - Automation Error, Invalid OLEVerb structure
    Err.Raise VBA.vbObjectError + 7 'raises "-2147221497 - Automation Error, Uninitialized object
    Err.Raise VBA.vbObjectError + 515 'raises "-2147220989 - Automation Error, A syntax error occurred trying"
    Err.Raise VBA.vbObjectError + 528 'raises "-2147220976 - Automation Error, The owner of the PerUser subscription"

    Err.Raise VBA.vbObjectError + 529 'raises "-2147220975 - Automation Error"
    Exit Sub

ErrorHandler:
    Call MsgBox(Err.Number & " - " & Err.Description)
End Sub

ERROR Statement

There is also an ERROR statement that can be used to generate errors.
This is only provided for backwards compatibility.


© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext