Source Property (Visual Basic)

Applies To

Err Object.

Description

Returns or sets the name of the object or application that originally generated the error.

Syntax

object.Source [= stringexpression]

The Source property syntax has these parts:

Part

Description

object

Always the Err object.

stringexpression

A string expression representing the application that generated the error.


Remarks

The Source property specifies a string expression representing the object that originally generated the error; the expression is usually the object’s class name or programmatic ID. Use Source to provide your users with information when your code is unable to handle an error generated in an accessed object. For example, if you access Microsoft Excel and it generates a Division by zero error, Microsoft Excel sets Err.Number to its error code for that error and sets Source to Excel.Application. Note that if the error is generated in another object called by Microsoft Excel, Microsoft Excel intercepts the error and sets Err.Number to its own code for Division by zero. However, it leaves the other Err object properties (including Source) as set by the object that generated the error. Source always contains the name of the object that originally generated the error — your code can try to handle the error according to the error documentation of the object you accessed. If your error handler fails, you can use the Err object information to describe the error to your user, using Source and the other Err properties to inform the user which object originally caused the error, its description of the error, and so forth.

When generating an error from code, Source is your application’s programmatic ID. For OLE Automation class modules, Source should contain a name having the form project.class. Not all Visual Basic host applications can create OLE Automation objects. See your host application’s documentation to determine whether it can create classes and OLE Automation objects.

Note The On Error Resume Next construct may be preferable to On Error GoTo when dealing with errors generated during access to other objects. Checking Err after each interaction with an object removes ambiguity about which object your code was accessing when the error occurred. Thus, you can be sure which object placed the error code in Err.Number, as well as which object originally generated the error (the one specified in Err.Source).

See Also

Description Property, Err Object, GetObject Function, HelpContext Property, HelpFile Property, LastDLLError Property, Number Property, On Error Statement.

Example

This example assigns the Programmatic ID of an OLE Automation object created in Visual Basic to the variable MyObjectID, then assigns that to the Source property of the Err object when it generates an error with the Raise method. When handling errors, you should not use the Source property (or any Err properties other than Number) programatically. The only valid use of properties other than Number is for displaying rich information to an end user in cases where you cannot handle an error.


' An object of type MyClass generates an error and fills all Err object
' properties, including Source, which receives MyObjectID, which is a
' combination of the Title property of the App object and the Name
' property of the MyClass object.= App.Title & "." & MyClass.Name.Raise Number := vbObjectError + 894, Source := MyObjectID, _
    Description := "Was not able to complete your task", _
    HelpFile := MyHelpFile, HelpContext := MyHelpContext