ItemRemoved Event — Event Procedures

Description

To create an event procedure that runs when the ItemRemoved event occurs, follow the steps included in the Remarks section of this topic.

Syntax

Private Sub evtReferences_ItemRemoved(reference)

The ItemRemoved 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 removed from the References collection.


Remarks   The ItemRemoved event procedure can contain code that you want to run when a reference to a type library is removed from the project. You can remove a reference from Visual Basic by using the Remove method to remove a Reference object from the References collection.

To create the ItemRemoved 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 ItemRemoved event. The following line of code shows a typical WithEvents declaration:
    Public WithEvents evtReferences As References
  2. 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 ItemRemoved event in the Procedure box.
  3. 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 removed from the project, add that code to the event procedure.
  4. 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 ItemRemoved 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 ItemAdded event, you don't need to repeat them for the ItemRemoved event. You can use the same References object variable for both.

In order to trigger the ItemRemoved event, you must remove the existing reference by using the References object variable that you declared with the WithEvents keyword.

To remove a reference and trigger the event:

  1. 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 Remove method of the object variable that represents the References collection (evtReferences). Since the ItemRemoved 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 remove an existing reference with a procedure such as the following:

Function RemoveReference(strRefName 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(strRefName)
    objRefEvents.evtReferences.Remove ref
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 ItemRemoved 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 ItemRemoved event and the ItemAdded event.
  • If you have multiple ItemRemoved 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 ItemRemoved event procedure in a project and to include all code to run when the event occurs in that event procedure.
  • The ItemRemoved event occurs after the reference has been removed from the project.
See Also

ItemAdded event, ItemRemoved event.

Example

See the Initialize event — event procedures example.