Special Edition Using Microsoft Office Access 2003
Class modules are containers for VBA code that relate to a particular class of objects. Access 2003 defines two classes (collections) Forms and Reports that contain VBA code for a particular instance of the class: a Form or Report object. In object-oriented programming terms, class modules encapsulate VBA code within a Form or Report object. Code encapsulation lets you create reusable objects. For example, when you copy a form from one Access database to another, the copy you make includes the code in the form's class module. Access's Form and Report class modules differ from conventional modules in that a Form or Report object is integral to the code and contributes the object's visible properties (appearance). Conventional modules, such as Northwind.mdb's Utility Functions, appear in the Modules page of the Database window. Your event-handling code creates a custom set of methods (behavior) that are applicable to the object. When you open a form or report, you create the default instance of the corresponding Form or Report object. The default instance of the object appears in the Forms or Reports page of the Database window. VBA 6.0 also lets you create additional temporary, nondefault instances of Form and Report objects with the New reserved word. You don't need to add an explicit reference to the associated form or report in your code, although it's good programming practice to use the Me self-reference to specify the current instance of the Form or Report object. Note Access 9x called the window that displays class module code the Class Module window. The Class Module window was integrated with other Access windows. Access 2000+ uses the VBA Integrated Design Environment (IDE), which this book calls the VBA editor.
The Main Switchboard Class Module
The Northwind Traders Main Switchboard form is a good starting point for gaining an understanding of class modules and simple event-handling code. The Main Switchboard form contains two event-handling subprocedures: one each for the Display Database Window and Exit Microsoft Access command buttons, plus a single OpenForms function that services the Categories, Suppliers, Products, Orders, and Print Sales Reports command buttons. Figure 28.1 illustrates the relationships between the command buttons and the function or subprocedure that executes when you click the button. Figure 28.1. This diagram illustrates relationships between command buttons and event-handling code for the Main Switchboard form.
To view the event-handling code in the Main Switchboard form, follow these steps:
Tip All VBA procedures and functions in Access class modules have Public scope unless you specify otherwise; procedures that you declare with Sub, Function, Public Sub, or Public Function are visible to all other class modules and conventional code modules. For example, you can execute the Main Switchboard form's ExitMicrosoftAccess_Click procedure from the Utility Functions module with a [Form_Main Switchboard].ExitMicrosoftAccess_Click instruction. When you create a procedure stub in a class module, Access adds the Private modifier. Private subprocedures and functions have slightly less overhead than Public subprocedures and functions and improve performance in large Access applications. The function and subprocedures of the Form_Main Switchboard class module are declared without the default Private modifier, which is not a good programming practice for event handlers.
Event-Handling Code in the Main Switchboard Form
Listing 28.1 shows all the code contained in the Form_Main Switchboard class module. Each of the procedures includes standard error-handling code consisting of On Error GoTo Err_Lable... Resume Exit_ Label...Exit{ Function| Sub} statements. Adding error handling to every procedure you write is a good VBA programming practice.
Listing 28.1 Event-Handling Code of the Form_Main Switchboard Class Module
Option Compare Database 'Use database order for string comparisons. Option Explicit 'Requires variables to be declared before they are used. Function OpenForms(strFormName As String) As Integer 'This function is used in the Click event of command buttons that 'open forms on the Main Switchboard. Using a function is more efficient 'than repeating the same code in multiple event procedures. On Error GoTo Err_OpenForms 'Open specified form. DoCmd.OpenForm strFormName Exit_OpenForms: Exit Function Err_OpenForms: MsgBox Err.Description Resume Exit_OpenForms End Function Sub ExitMicrosoftAccess_Click() 'This code created by Command Button Wizard. On Error GoTo Err_ExitMicrosoftAccess_Click 'Exit Microsoft Access. DoCmd.Quit Exit_ExitMicrosoftAccess_Click: Exit Sub Err_ExitMicrosoftAccess_Click: MsgBox Err.Description Resume Exit_ExitMicrosoftAccess_Click End Sub Sub DisplayDatabaseWindow_Click() 'This code created in part by Command Button Wizard. On Error GoTo Err_DisplayDatabaseWindow_Click Dim strDocName As String strDocName ="Categories" 'Close Main Switchboard form. DoCmd.Close 'Give focus to Database window; select Categories table (first 'form in list). DoCmd.SelectObject acTable, strDocName, True Exit_DisplayDatabaseWindow_Click: Exit Sub Err_DisplayDatabaseWindow_Click: MsgBox Err.Description Resume Exit_DisplayDatabaseWindow_Click End Sub Tip The Default to Full Module view and Procedure Separator settings on the Editor page of the Options properties sheet make reading VBA code easier. By default, all the procedures in the class module appear after the Declarations section of the class module in the alphabetical order of the procedure name, separated by a horizontal gray line. With Full Module view specified, you can use the scroll bars to view all the procedures within a module.
Access 2003's DoCmd object is the key to manipulating Access application objects with VBA. DoCmd lets a VBA statement execute the equivalent of an Access macro, such as OpenForm or Quit. Application-specific reserved words, such as DoCmd, preclude a common set of VBA objects for all members of Office; thus, DoCmd is an Access-specific object, not a reserved word.
|