Datasheet
Using the Visual Basic Editor
576
Procedures in a module fall into two major categories: sub procedures and
function procedures. Both types of procedures use VBA code to perform
some task. The next sections outline some subtle differences in how and
where they’re used.
Sub procedures
A sub procedure is one or more lines of code that make Access perform a par-
ticular task. Every sub procedure starts with the word
Sub (or Private
Sub
) and ends with End Sub, using one of the following general structures:
Sub name()
...code...
End Sub
Private Sub
name()
...
code...
End Sub
name is the name of the procedure, and ...code... is any amount of VBA
code.
Text that appears to be written in plain English within a module represents
programmer comments — notes for other programmers. The computer
ignores the comments. Every comment starts with an apostrophe (
‘).
Function procedures
A function procedure is enclosed in Function...End Function state-
ments, as the following code shows:
Function name()
<...code...>
End Function
Unlike a sub procedure, which simply performs some task, a function proce-
dure performs a task and returns a value. In fact, an Access function proce-
dure is no different from any of the built-in functions you use in Access
expressions. And you can use a custom function procedure wherever you
can use a built-in procedure.
Using the Visual Basic Editor
Regardless of how you open a module, you end up in the Visual Basic Editor.
The Visual Basic Editor is where you write, edit, and test your VBA code. The
Visual Basic Editor is entirely separate from the Access program window. If
38_036494 bk08ch01.qxp 11/17/06 8:34 AM Page 576