You are currently offline, waiting for your internet to reconnect

ACC2000: How to Sink Form and Report Events to Custom Class Modules

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

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 Events

When 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:
Private WithEvents frm As Access.Form				
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 Track

After 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.
'Declarations section of moduleOption Compare DatabaseOption Explicit'This code assumes you have created a custom class named cFormErrorHandler'that exposes a property named Form. You use the Form property of the'custom class to pass it the form whose events you want to sink. See the'Step by Step example below for a complete example.Private clsFormError As cFormErrorHandler      Private Sub Form_Open(Cancel As Integer)    Set clsFormError = New cFormErrorHandler    Set clsFormError.Form = MeEnd Sub				

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 Example

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

  1. Open the sample database Northwind.mdb.
  2. On the Insert menu, click Class Module to start the Visual Basic Environment (VBE) and to create a new custom class module.
  3. Add the following code to the class module:
    Option Compare DatabaseOption Explicit'Define the object variable of the object type whose events you want to'track. See Step 1 in the "More Information" section.Private WithEvents frm As Access.Form 'Define property procedures so that you can identify which particular form'each instance of the class will be using. See Step 1 in the "More'Information" section.Public Property Get Form() As Access.Form    Set Form = frmEnd PropertyPublic Property Set Form(frmIn As Access.Form)    Set frm = frmInEnd Property'Define the event procedure that you want to sink. Note that it uses the'same definition as the Error event procedure in a form's module.      Private Sub frm_Error(DataErr As Integer, Response As Integer)    MsgBox "My custom error message!"    Response = acDataErrContinueEnd Sub					
  4. Save the class module as cFormErrorHandler.
  5. Open the Employees form in Design view.
  6. Add the following code to the class module of the form:
    'Declarations section of module.Option Compare DatabaseOption Explicit      Private clsFormError As cFormErrorHandler      Private Sub Form_Open(Cancel As Integer)    Set clsFormError = New cFormErrorHandler    Set clsFormError.Form = MeEnd Sub					
  7. In Design view of the form, set the OnError property of the form to [Event Procedure]. Note that it is not necessary to actually add the OnError event procedure to the form's module. Just simply set the OnError property to [Event Procedure].

    NOTE: If you add event procedure code to the form itself, then the code defined in the form will run, followed by the code in the custom class.
  8. Save the form and close it.
  9. Open the form in Form view.
  10. On the Edit menu, click Delete Record. This causes an error to occur because the employee record that you are attempting to delete has related records in the Orders table.
Note that you received the custom error message that you defined in the Form_Error event procedure within the custom class module. This indicates that the form's Error event was propagated to the custom class module, even though you did not define a Form_Error event procedure directly in the form.
For additional information about creating class modules, click the article number below to view the article in the Microsoft Knowledge Base:
209968 ACC2000: Introduction to Stand-Alone Class Module Programming

Article ID: 234907 - Last Review: 10/25/2013 22:28:00 - Revision: 4.0

Microsoft Access 2000 Standard Edition

  • kbnosurvey kbarchive kbhowto KB234907