The information in this article applies to:
Advanced: Requires expert coding, interoperability, and multiuser skills. This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp). SUMMARY
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. MORE INFORMATION
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. 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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web: http://www.microsoft.com/mcsp/For more information about the support options available from Microsoft, please see the following page on the World Wide Web: http://www.microsoft.com/support/supportnet/overview/overview.aspCAUTION: Following the steps in this example will modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and perform these steps on a copy of the database.
REFERENCESAdditional query words: inf
Keywords : kbdta |
Last Reviewed: November 1, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |