ItemRemoved Event - Event Procedures

ItemRemoved Event — Event Procedures

See Also         Example

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. 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. 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. 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. 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