Flylib.com

Books Software

 
 
 

Chapter 7 -- Class, Form, and Report Modules

Chapter 7

Class, Form, and Report Modules

To program Microsoft Access efficiently , you must manage your programmatic resources so that they are easy to use and reuse. The value of code grows in proportion to how much use you can get from it.

Class modules package code for easy reuse. The class module acts as a container that exposes the code and selected variables inside it in a way that is familiar to Microsoft Visual Basic for Applications (VBA) developers. Basically, you invoke class procedures and assign values to variables with the same syntax used for the properties and methods of built-in Access objects. To use the code in a class module, you do not have to know anything about how it works. Also, since class modules expose properties, methods , and events like other objects do, even beginning VBA developers can use them.

This chapter first introduces standalone class modules and form and report class modules. Then it demonstrates simple ways to build classes into your Access applications and to develop custom properties and methods. Next comes a case study that uses three forms, a few custom Property Get and Property Let functions, and a couple of techniques based on ActiveX Data Objects (ADO) to start building an application. The section after the case study shows the syntax for programming events into your custom classes and introduces the WithEvents keyword.

The focus then shifts to the containers for class, form, and report modules as we look at the All collections that are new to Access 2000. Just as there are AllForms and AllReports collections, there is an AllModules collection. (In fact, there are ten All collections altogether.) The chapter wraps up by explaining how to combine the AllModules collection with the Modules collection to manage code in an application.

Module Types

There are three basic kinds of modules:

  • Standard modules. These hold subprocedures and function procedures that you want to make available throughout a database file. Standard modules can also contain variables defined with a Public declaration that you want to make available to procedures in other modules.
  • Standalone class modules. These let you create custom objects. You can define properties, methods , and events for these objects, and you can use the New keyword to create instances of the form objects.
  • Class modules for forms and reports (often called form and report modules). Forms and reports by default all have modules behind them (their HasModule property is set to True by default). You can use the Me keyword when referring to the modules behind forms and reports.

Class Modules

Standalone class modules differ from form and report class modules in several ways.

First, standalone class modules do not have a built-in user interface, as form and report class modules do. This makes standalone class modules more suited to tasks that do not require an interface, such as performing calculations, looking up data, or modifying a database. When form or report modules require computationally intensive tasks, they can call a standalone class module.

Second, standalone class modules offer Initialize and Terminate events that enable operations that need to take place at the opening and closing of a class instance. Report and form modules do not have these events, but you can perform similar functions with the Load and Close events.

Third, you must use the New keyword to create instances of standalone class modules. Report and form class modules also let you create instances with the DoCmd OpenForm and OpenReport methods as well as by referencing the report or form class module's properties or methods . For example, Form_MyForm.SetFocus opens the MyForm form.

You can create a standalone class module from the Insert menu in VBE. (The same menu offers commands for building a standard module or a procedure.) After creating a class module shell, you can populate it with procedures and declarations, which equip it with custom properties and methods.

Custom Property Functions and Custom Methods

{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}

Special property functions make it easier to develop any combination of read-only, write-only, and read/write properties for your classes. If your application permits , you can define properties by simply declaring a public variable. When a class module defines a property with a public variable, it is always a read/write property. The ability to declare custom properties lets you extend the basic Access functionality for forms and reports . In addition, these property functions allow you to create powerful standalone classes.

Your applications can also build custom methods into classes. You can use subprocedures or function procedures to accomplish this. By selectively exposing variables and procedures with the Public keyword, you can narrowly define what methods and properties they expose. This lets your applications define interfaces to your class objects that perform in very specific ways.

Instantiating Classes

The public methods and procedures support programmatic access by procedures outside the class. You must first instantiate the class in a host procedure within another module, using the New keyword. (You use the same keyword to instantiate objects from other classes, such as ADO Connection and Recordset objects. In fact, your applications can instantiate multiple copies of a custom class at the same time—just like the ADO classes.) After instantiating a class, the code in your host procedure manipulates the instance of the class, not the class itself. You can change a property in one instance of a form, but when you instantiate a second instance of the form it appears with the default property setting.

Custom Classes and Events

Although VBA in Access lets you build custom classes with their own properties and methods, you cannot build custom events within those classes. You can, however, design a class that hooks onto a built-in class or type library that you attach. For example, you can build a class module that launches VBA code in response to the ItemAdded and ItemRemoved events of the References collection. This collection tracks links to external type libraries and ActiveX controls. After referencing a library, such as the Microsoft ActiveX Data Objects 2.1 Library, you can build custom events around the ADO events for the Connection and Recordset objects. These events can enable asynchronous data access that lets your application respond to users even while it remains ready to respond to a completed connection or the availability of a fetched set of records.

You use the WithEvents keyword within a Public declaration to point to an object reference that monitors and reports events within an ActiveX control. This keyword is valid only in class modules. You can define multiple variables within a module with the WithEvents keyword, but you cannot create arrays with it. Also, a declaration cannot contain both the WithEvents and New keywords.

Similar products