Microsoft Access 2010 Bible

(Rick Simeone) #1

Part II: Programming Microsoft Access


390


Standard modules
Standard modules are independent from forms and reports. Standard modules store code that is
used from anywhere within your application. By default, these procedures are often called global or
public because they’re accessible to all elements of your Access application.

Use public procedures throughout your application in expressions, macros, event procedures, and
other VBA code. To use a public procedure, you simply reference it from VBA code in event proce-
dures or any other procedure in your application.

Tip
Procedures run; modules contain. Procedures are executed and perform actions. Modules, on the other hand,
are simple containers, grouping procedures and declarations together. A module can’t be run; instead, you run
the procedures contained within the module.


Standard modules are stored in the Module section of the Navigation Pane. Form and report mod-
ules (see the next section) are attached to their hosts and are accessed through the Form Property
Sheet or Report Property Sheet.

Tip
Generally speaking, you should group related procedures into modules, such as putting all of an application’s
data conversion routines into a single module. Logically grouping procedures make maintenance much easier
because there is a single place in the application for all the procedures supporting a particular activity. Plus,
most modules contain procedures that are related in some way.


Form and report modules
All forms and reports support events. The procedures associated with form and report events can
be macros or VBA code. Every form or report you add to your database contains a VBA code mod-
ule (unless its Has Module property is set to No). This form or report module is an integral part of
the form or report; it’s used as a container for the event procedures you create for the form or
report. The module attached to each form or report is a convenient way to place all the object’s
event procedures in a single location.

Cross-Reference
For more on events, turn to Chapter 12.


Adding VBA event procedures to a form module is very powerful and efficient. Because the module
is an integral and permanent part of the form or report, all the event procedures travel with the
object when it’s exported to another Access database.

Modifying a control’s event procedure is easy: Simply click the builder button (with the ellipsis:.. .)
in the Property Sheet next to the event property name, to open the form’s code module. Figure
10.7 illustrates accessing the Click event procedure of the Delete button on the Contacts form.
Free download pdf