Article ID: 234907 - View products that this article applies to.
This article was previously published under Q234907
This article has been archived. It is offered "as is" and will no longer be updated.
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).
Form and report class objects in Microsoft Access define a set of events that makes it possible to use event procedures to automate tasks. By using the WithEvents keyword, Visual Basic for Applications allows you to track the events occurring in a class module and to propagate them to a different class module. This is typically known as "sinking" events.
This article discusses how to sink form and report events to a custom class module.
It is possible to write event procedures in the class module of a form or a report. However, because event procedures for forms and reports are private to the object's class module, they are not accessible from other Microsoft Access objects. This makes it more difficult for developers to write reusable code. For instance, you may want to implement an error handling routine that runs when any form's Error event is triggered. If you wanted to run the same routine from every form's Error event, you could write a global subroutine or function that you call from the Error event of each form.
However, it is also possible to sink each form's Error event to a custom class module. The advantage of using this approach is that it allows a developer to implement an approach that is more object oriented than having to call a global function from the appropriate event of each form or report. If the developer's target audience consists of other Visual Basic for Applications developers, this approach allows the developer to expose the routine to them in the event-driven manner that they are already familiar with.
There are three main steps that you must perform in order to successfully sink a form or report event to a custom class module.
Step 1: Create a Custom Class Module Which Tracks EventsWhen creating your custom class module, there are several things you need to do. You must use the WithEvents keyword to define an object variable of the type of object whose events you want to track. For example, if you want to track the events of an Access form object, you must declare your object variable as follows:
This object variable enables your custom class to do two things. First, along with the use of property procedures, it provides a mechanism for identifying which particular object this instance of the class will be tracking. Second, it allows you to create event procedures in the custom class that adhere to the defined events for that object type in the Access object model.
After creating the object variable, you must create property procedures using the Property Get and Property Set statements, which allow you to set and return the same object type as the object variable. This is necessary because it is possible that you would want to call this code from multiple objects. Suppose that your custom class was tracking form events. Because you might want to track the events of the Customers form and the Employees form, your code has to identify which specific form this instance of the class will track. By using property procedures, it enables you to pass the form into the class as a property in order to identify it.
Finally, you must write the event procedures in the custom class module to automate the tasks that you want whenever the event propagates to the class module. When you use the WithEvents keyword, the event procedures for the object type that you specified are available to the class module. For instance, if you use WithEvents to declare your object variable as an Access Form object, then all event procedures defined for the Access Form object are available to write in your custom class module.
Step 2: Instantiate the Custom Class and Identify Which Object to TrackAfter you create your custom class module and add the necessary property procedures and event procedures, you must instantiate the class and pass it the object whose events you want to track. To instantiate a custom class module, you must declare an object variable as the name of your custom class. Then, you must use the Set statement to create the instance of it. Following this, you must set the property defined by the property procedures in the class module to the object that you want to track.
Step 3: Set Event Properties to [Event Procedure]In order for Microsoft Access to successfully sink events for an object, all event properties for events that you want to sink must be set to [Event Procedure] in Design view of the form or report. You do not have to add the actual code for the event procedure within the form or report module. However, the event property in Design view must be set to [Event Procedure].
NOTE: If you add the actual event procedure behind the form or report module as well as to your custom class, then both event procedures will be triggered. The event procedure defined in the form or report module will be triggered first, followed by the event procedure in your custom class.
Step-by-Step ExampleThe following steps demonstrate how to sink a form's event to a custom class module. Although it is possible to sink other form events, this example focuses on the form Error event.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.
For additional information about creating class modules, click the article number below to view the article in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/209968/EN-US/ )ACC2000: Introduction to Stand-Alone Class Module Programming
Contact us for more help
Connect with Answer Desk for expert help.