In order to handle the events of a COM component object instance, you must
declare it using the WithEvents keyword. For example, you might declare an instance of an
Access Report in a form's General Declarations with the line:
Private WithEvents RPT As Access.Report
In the Code Window for the form, you'd see RPT listed as one of the form's
objects (see Figure 10.5). If you chose the RPT object from the left side drop-down list,
then you'd see the list of procedures for the events supported by this object (see
Figure 10.6).

FIGURE 10.5. The object you declared using WithEvents is visible in the list of objects for the module
where you declared it.

FIGURE 10.6. The object's event procedures are available to you when you declare the object
using WithEvents.
There are several important limitations on programming using the WithEvents keyword:
-
You can't use WithEvents keyword in a Standard Module (a .BAS file).
-
You can only declare an Object variable using WithEvents in the General section
of a module.
-
You can't use the As New keyword in a declaration that uses WithEvents.
-
Not all ActiveX component applications support the WithEvents keyword. In particular,
only components whose object classes are registered in the Windows Registry will
support WithEvents and then not even all of those components. Obviously, you can't
handle events for those components that don't support WithEvents.
For a more detailed discussion of how to program with COM components' events,
see the section "Handling a Class
Event" in Chapter 12, "Creating
a COM Component that Implements Business Rules or Logic."