Skip to main content

Err object Source property

Table of contents
  1. Remarks
  2. Examples

Returns or sets a string expression specifying the name of the object or application that originally generated the error. Read/write.

Remarks

The Source property specifies a string expression representing the object that generated the error; the expression is usually the object's class name or programmatic ID.

Use Source to provide 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, Excel sets Err.Number to its error code for that error and sets Source to Excel.Application.

When generating an error from code, Source is your application's programmatic ID. For class modules, Source should contain a name having the form project.class.

When an unexpected error occurs in your code, the Source property is automatically filled in. For errors in a standard module, Source contains the project name. For errors in a class module, Source contains a name with the project.class form.

Examples

This example assigns the Programmatic ID of an Automation object created in Visual Basic to the variable MyObjectID, and 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) programmatically. The only valid use of properties other than Number is for displaying rich information to an end user in cases where you can't handle an error. The example assumes that App and MyClass are valid references.

Dim MyClass, MyObjectID, MyHelpFile, MyHelpContext
' 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.
MyObjectID = App.Title & "." & MyClass.Name
Err.Raise Number := vbObjectError + 894, Source := MyObjectID, _
          Description := "Was not able to complete your task", _
          HelpFile := MyHelpFile, HelpContext := MyHelpContext

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>