Professional Documents
Culture Documents
12.1 Introduction: Learning the simple programs we are writing here, these
basics of programming terms are interchangeable.
Programming can be an enormously complex and 12.1.1 Interacting with the interpreter
difficult activity. Or it can be quite straightforward. In
Access provides two ways of interacting with the
either case, the basic programming concepts remain
VBA language. The most useful of these is through
the same. This tutorial is an introduction to a handful
saved modules that contain VBA procedures. These
of programming constructs that apply to any “third
procedures (subroutines and functions) can be run to
generation” language, not only Visual Basic for
do interesting things like process transactions
Applications (VBA).
against master tables, provide sophisticated error
Strictly speaking, the language that is checking, and so on.
included with Access is not Visual Basic—it is The second way to interact with VBA is directly
a subset of the full, stand-alone Visual Basic through the interpreter. Interpreted languages are
language (which Microsoft sells separately). easier to experiment with since you can invoke the
In Access version 2.0, the subset is called interpreter at any time, type in a command, and
“Access Basic”. In version 7.0, it is slightly watch it execute. In the first part of this tutorial, you
enlarged subset called “Visual Basic for Appli- are going to invoke Access’ VBA interpreter and exe-
cations” (VBA). However, in the context of the cute some very simple statements.
In the second part of the tutorial, you are going to 12.3 Tutorial exercises
create a couple of VBA modules to explore looping,
conditional branching, and parameter passing. 12.3.1 Invoking the interpreter
• Click on the module tab in the database window
12.2 Learning objectives and press New.
What is the debug/immediate window? How This opens the module window which we will use in
do I invoke it? Section 12.3.3. You have to have a module window
What are statements, variables, the open in order for the debug window to be available
assignment operator, and predefined from the menu.
functions? • Select View > Debug Window from the main
menu. Note that Control-G can be used in ver-
How do I create a module containing VBA
sion 7.0 and above as a shortcut to bring up the
code?
debug window.
What are looping and conditional branching?
What language constructs can I use to In version 2.0, the “debug” window is called
implement them? the “immediate” window. As such, you have to
How do I use the debugger in Access? use View > Immediate Window. The term
debug window will be used throughout this
What is the difference between an interpreted
tutorial.
and compiled programming language?
When the second statement is executed, VBA recog- is a function that is provided as part of the program-
nizes that s is a variable, not a string (since it is not ming environment.
in quotations marks). The interpreter replaces s with For example, cos(x) is a predefined function in
its value (Hello) before executing the Print com- many computer languages—it takes some number x
mand. In the final statement, s is in quotation marks as an argument, does some processing to find its
so it is interpreted as a literal string. cosine, and returns the answer. Note that since this
function is predefined, you do not have to know any-
Within the debug window, any string of char-
thing about the algorithm used to find the cosine, you
acters in quotations marks (e.g., “COMM”) is
just have to know the following:
interpreted as a literal string. Any string with-
out quotation marks (e.g., COMM) is interpreted 1. what to supply as inputs (e.g., a valid numeric
as a variable (or a field name, if appropriate). expression representing an angle in radians),
Note, however, that this convention is not uni- 2. what to expect as output (e.g., a real number
versally true within different parts of Access. between -1.0 and 1.0).
12.3.2.3 Predefined functions The on-line help system provides these two
pieces of information (plus a usage example
In computer programming, a function is a small pro-
and some additional remarks) for all VBA pre-
gram that takes one or more arguments (or param-
defined functions.
eters) as input, does some processing, and returns
a value as output. A predefined (or built-in) function
A module contains a declaration page and one or 12.3.4 Creating subroutines with looping
more pages containing subroutines or user-defined and branching
functions. The primary difference between subrou- In this section, you will explore two of the most pow-
tines and functions is that subroutines simply exe-
erful constructs in computer programming: looping
cute whereas functions are expected to return a and conditional branching.
value (e.g., cos()). Since only one subroutine or
• Create a new subroutine by typing the following
function shows in the window at a time, you must anywhere on the declarations page:
use the Page Up and Page Down keys to navigate Sub LoopingTest()↵
the module.
Notice that Access creates a new page in the mod-
The VBA editor in version 8.0 has a number of ule for the subroutine, as shown in Figure 12.3.
enhancements over earlier version, including 12.3.4.1 Declaring variables
the capability of showing multiple functions
When you declare a variable, you tell the program-
and subroutines on the same page.
ming environment to reserve some space in memory
for the variable. Since the amount of space that is
required is completely dependent on the type of data
the variable is going to contain (e.g., string, integer,
Boolean, double-precision floating-point, etc.), you
• Copy the highlighted code to the clipboard (Con- 12.3.7 Creating the Min() function
trol-Insert), switch to ParameterTest, and In this section, you are going to create a user-
paste the code (Shift-Insert) into the Parame- defined function that returns the minimum of two
terTest procedure. numbers. Although most languages supply such a
To incorporate the parameters into ParameterT- function, Access does not (the Min() and Max()
est, you will have to make the following modifica- function in Access are for use within SQL statements
tions to the pasted code: only).
• Replace i = 1 with i = intStart. • Create a new module called basUtilities.
• Replace i > 10 with i > intStop. • Type the following to create a new function:
• Call the subroutine from the debug window by Function MinValue(n1 as Single, n2
typing: as Single) as Single↵
ParameterTest 4, 12↵ This defines a function called MinValue that returns
a single-precision number. The function requires two
If you prefer enclosing parameters in brack-
single-precision numbers as parameters.
ets, you have to use the Call <sub
name>(parameter1, ..., parametern) Since a function returns a value, the data type
syntax. For example: of the return value should be specified in the
Call ParameterTest(4,12)↵ function declaration. As such, the basic syn-
tax of a function declaration is:
function
Implement the MinValue()
using conditional branching.
various
Test the function by passing it
parameter values.