Microsoft Access 2010 Bible

(Rick Simeone) #1

Chapter 11: Mastering VBA Data Types and Procedures


437


Note
A module is a container for multiple subprocedures and functions.


Calling VBA procedures
VBA procedures are called in a variety of ways and from a variety of places. They can be called
from events behind forms and reports, or they can be placed in module objects and called by sim-
ply using their name or by using the Call statement. Here are some examples:

SomeSubRoutineName

Call SomeSubRoutineName

Somevalue = SomeFunctionName

Only functions return values that may be assigned to variables. Subprocedures are simply called,
do their work, and end. Although functions return a single value, both subprocedures and func-
tions can place values in tables, in form controls, or even in public variables available to any part of
your program. You can see several examples of different ways to use subprocedures and functions
throughout this chapter.

The syntax used for calling subprocedures with parameters is variable. For example, when using
the Call keyword to call a subprocedure that includes arguments, the arguments must be
enclosed in parentheses:

Call SomeSubRoutineName(arg1, arg2)

However, when the same call without the Call keyword requires no parentheses:

SomeSubRoutineName arg1, arg2

Also, using the Call keyword with a function tells Access your code is not capturing the func-
tion’s return value:

Call SomeFunctionName

or, when arguments are required:

Call SomeFunctionName(arg1, arg2)

In this case, the function is treated as if it is a subroutine.

Creating subs
Conceptually, subroutines are easy to understand. A subroutine (usually called a sub and sometimes
called a subprocedure) is a set of programming statements that is executed as a unit by the VBA
engine. VBA procedures can become complex, so this elementary description of subroutines is
quickly overwhelmed by the actual subroutines you’ll compose in your Access applications.
Free download pdf