ItemAdded Event — Event Procedures
Description
To create an event procedure that runs when the ItemAdded event occurs, follow the steps included in the Remarks section of this topic.
Syntax
Private Sub evtReferences_ItemAdded(reference)
The ItemAdded event procedure has the following arguments.
Argument | Description |
|
evtReferences | An object variable representing the References collection. You must declare this object variable in a class module by using the WithEvents keyword. |
reference | The Reference object that has been added to the References collection. |
Remarks
The ItemAdded event procedure can contain code that you want to run when a reference to a type library is added to the project. You can add a reference from Visual Basic by using the AddFromFile or AddFromGUID method to add a Reference object to the References collection.
To create the ItemAdded event procedure:
- 1. Using the WithEvents keyword, declare an object variable of type References in the Declarations section of a class module. Microsoft Access uses this object variable to define the event procedure for the ItemAdded event. The following line of code shows a typical WithEvents declaration:
Public WithEvents evtReferences As References
- 2. Create an event procedure definition within the class module by selecting the name of the References object variable from the Object box in the Module window. For example, if you use the declaration shown in the preceding step, you'll see evtReferences in the Object drop-down box. Then select the ItemAdded event in the Procedure box.
- 3. Add the code that will run when the event occurs. For example, if you want to display a message box stating that a reference has been added to the project, add that code to the event procedure.
- 4. Assign the References collection to the References object variable that you declared with the WithEvents keyword. This assignment statement should be within a procedure that runs before the ItemAdded event will occur.
For example, you could include this statement within the Initialize event of the class module in which you declared the object variable. Then when you create an instance of the class, the object variable is automatically initialized. If you're using the evtReferences object variable, this event procedure would appear as follows:
Private Sub Class_Initialize()
Set evtReferences = Application.References
End Sub
Note If you've already followed the preceding steps to create an event procedure for the ItemRemoved event, you don't need to repeat them for the ItemAdded event. You can use the same References object variable for both.
In order to trigger the ItemAdded event, you must create the new reference by using the References object variable that you declared with the WithEvents keyword.
To add a reference and trigger the event:
- 1. Within a standard module, create a new instance of the class within which you declared the References object variable. For example, if the name of the class is RefEvents, you can create a new instance of the class and assign it to an object variable with the following declaration statement. This statement can exist at the module level or within a procedure.
Dim objRefEvents As New RefEvents
- 2. Within a Sub or Function procedure, create a new Reference object by using the AddFromFile or AddFromGUID method of the object variable that represents the References collection (evtReferences). Since the ItemAdded event in the class module is defined in association with a particular References object variable, you must create the new Reference object by using this variable. Otherwise, the event won't occur.
Note that you must qualify the References object variable with the name of the class in which it's declared.
Once you've completed all the preceding steps, you can add a new reference with a procedure such as the following:
Function AddReference(strFileName As String)
Dim ref As Reference
' Create new instance of RefEvents class.
Dim objRefEvents As New RefEvents
' Creates reference on References collection
' variable defined in RefEvents class.
Set ref = objRefEvents.evtReferences.AddFromFile(strFileName)
End Function
Notes
- The procedure that initializes the References object variable must be within the same scope as the object variable declaration. For example, if you declared the References object variable as Private within a class module, then you must initialize the variable in a procedure within that same class module.
- You can include the ItemAdded event procedure only in a class module.
- Once you declare a module-level variable to represent the References collection, you can use that variable to create event procedure definitions for both the ItemAdded event and the ItemRemoved event.
- If you have multiple ItemAdded event procedures within a single project, each one will run when the event occurs, but they won't run in any specified order. Therefore, it's advisable to create only one ItemAdded event procedure in a project and to include all code to be run when the event occurs in that event procedure.
- The ItemAdded event occurs after the reference has been added to the project.
See Also
ItemAdded event, ItemRemoved event.
Example
See the Initialize event — event procedures example.