Module Types

As mentioned earlier, procedures are created and stored in modules. In Access 2003 there are two types of modules: standard and class.

■ Standard modules

These modules are listed in the Microsoft Office Access Database window by selecting the Modules object in the left pane (see Figure 1-1). Standard modules are used to hold subprocedures and function procedures that are not associated with any particular form or report and, therefore, can be run from anywhere in the application. You can create a standard module by clicking Modules from the Objects list in the Database window and clicking the New button, or you can create them from the Visual Basic Editor window by choosing Insert | Module.

Figure 1-1: To list standard and standalone class modules that exist in a Microsoft Office Access database, click the Modules object in the left pane of the Database window.

■ Class modules

Class modules come in three varieties: standalone class modules, form modules, and report modules.

• Standalone class modules — These modules are used to create your own custom objects with their own properties and methods. You create a standalone class module by choosing Insert | Class Module in the Visual Basic Editor window. Access will create a default class module named Classl and will list it in the Class Modules folder in the Project Explorer window (see Figure 1-3). You will work with standalone class modules in Chapter 8.

• Form modules and report modules — Each Access form can contain a form module, and each report can contain a report module. These

Introduction to Access 2003 VBA Programming modules are special types of class modules that are saved automatically whenever you save the form or report.

To allow quick access to them, forms and reports don't have modules associated with them when they're first created. All newly created forms and reports are lightweight by design. They load and display faster than the forms and reports with modules. These lightweight forms and reports have their Has Module property set to No (see Figure 1-2).

When you open a form or report in Design view and choose View | Code or click the Code button in the toolbar, Access creates a form or report module. The Has Module property of a form or report is automatically set to Yes to indicate that the form or report now has a module associated with it. Note that this happens even if you have not written a single line of VBA code. Access opens a module window and assigns a name to the module that consists of three parts: the name of the object (e.g., form or report), an underscore character, and the name of the form or report. For example, a newly created form that has not been saved is named Form_Form1, a form module in the Customers form is named Form_Customers, and the report module in the Customers report is named Report_Customers.

Figure 1-2: When you begin designing a new form in the Microsoft Office Access user interface, the form does not have a module associated with it. Notice that the Has Module property on the Form properties sheet is set to No.

As with report modules, form modules store event procedures for events recognized by the form and its controls, as well as general function procedures and subprocedures. You can also write Property Get, Property Let, and Property Set procedures to create custom properties for the form or report. The procedures stored in their class modules are available only while you are using that particular form or report.

Figure 1-3: Microsoft Office Access organizes database modules in folders. Form and report modules are listed in the Microsoft Office Access Class Objects folder. Standard modules can be found under the Modules folder. The Class Modules folder organizes standalone class modules.

Figure 1-2: When you begin designing a new form in the Microsoft Office Access user interface, the form does not have a module associated with it. Notice that the Has Module property on the Form properties sheet is set to No.

PrD|ect - Acc2QD3_ChapUl

E M00=0ft office Access C&ess Objects EH Form_Customers ¡3 Repart_Cu5tomers S® Modules

Part I

0 0

Post a comment