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.

This approach does work but in some cases, it can produce some confusing results.

For example adding 515 to this constant, produces a native error.


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"


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


ERROR Statement

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


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