Raise Method

Applies To

Err object.

Description

Generates a run-time error.

Syntax

object.Raise number, source, description, helpfile, helpcontext

The Raise method has the following object qualifier and named arguments

Argument

Description

object

Required. Always the Err object.

number

Required. Long integer that identifies the nature of the error. Visual Basic errors (both Visual Basic-defined and user-defined errors) are in the range 0 – 65535. When setting the Number property to your own error code in a class module, you add your error code number to the vbObjectError constant. For example, to generate the error number 1050, assign vbObjectError + 1050 to the Number property.

source

Optional. String expression naming the object or application that generated the error. When setting this property for an object, use the form project.class. If source is not specified, the programmatic ID of the current Visual Basic project is used.


(continued)

Argument

Description

description

Optional. String expression describing the error. If unspecified, the value in Number is examined. If it can be mapped to a Visual Basic run-time error code, the string that would be returned by the Error function is used as Description. If there is no Visual Basic error corresponding to Number, the "Application-defined or object-defined error" message is used.

helpfile

Optional. The fully qualified path to the Microsoft Windows Help file in which help on this error can be found. If unspecified, Visual Basic uses the fully qualified drive, path, and file name of the Visual Basic Help file.

helpcontext

Optional. The context ID identifying a topic within helpfile that provides help for the error. If omitted, the Visual Basic Help file context ID for the error corresponding to the Number property is used, if it exists.


Remarks

All of the arguments are optional except number. If you use Raise without specifying some arguments, and the property settings of the Err object contain values that have not been cleared, those values serve as the values for your error.

Raise is used for generating run-time errors and can be used instead of the Error statement. Raise is useful for generating errors when writing class modules, because the Err object gives richer information than is possible if you generate errors with the Error statement. For example, with the Raise method, the source that generated the error can be specified in the Source property, online Help for the error can be referenced, and so on.

See Also

Clear method, Description property, Err object, Error statement, HelpContext property, HelpContextID property ("Extensibility Object Model Language Reference"), HelpFile property, LastDLLError property, Number property, Source property.

Specifics (Microsoft Access)

The Raise method raises a specified Visual Basic error. The properties of the Err object are set as though the error had actually occurred in running code. However, this works only for Visual Basic errors, not for Microsoft Access errors or Microsoft Jet database engine errors.

If you use the Raise method with an error number reserved by Microsoft Access or by the Microsoft Jet database engine, the value of the Err object's Description property is "Application-defined or object-defined error." To determine the descriptive string for a Microsoft Access or Jet database engine error that hasn't actually occurred in running code, use the AccessError method.

The AccessError method takes a Long value that represents an error number and returns the descriptive string associated with the error.

Example

This example uses the Err object's Raise method to generate an error within an Automation object written in Visual Basic. It has the programmatic ID MyProj.MyObject.

Const MyContextID = 1010407            ' Define a constant for contextID.
Function TestName(CurrentName, NewName)
    If Instr(NewName, "bob") Then    ' Test the validity of NewName.
        ' Raise the exception.
        Err.Raise vbObjectError + 27, "MyProj.MyObject", _
            "No ""bob"" allowed in your name", "c:\MyProj\MyHelp.Hlp", _
            MyContextID
    End If
End Function