2.3. Creating and Calling Procedures

Now that you have a sample database, I return to a discussion of modules and procedures. You already saw a procedure inside the sample module illustrations provided previously, but let's now analyze what the procedures are and how they can be used.

Procedures are the building blocks of modules. Each module contains one or more sub procedures or functions, and each sub procedure or function contains one or more VBA code statements.

2.3.1. Sub versus Function Procedure

Procedures can either be sub procedures or function procedures. A sub procedure performs an action but does not return a particular result. A function performs an action and returns a particular result. I will provide various examples to illustrate these differences. Sub procedures and functions can be called both from within other procedures and functions and when an event associated with an object is triggered (for example, button_click), as I describe in more detail in Chapter 3. Create and Call a New Sub Procedure

The syntax for declaring a new sub procedure without parameters is:

Sub ProcedureName
'code for the procedure
End Sub

The syntax for a new sub procedure that contains variables as parameters is:

Sub ProcedureName(Variables)
'code for the procedure
End Sub

Variables are containers for storing data. If a procedure must use the value stored in a particular variable, that variable can be passed as a parameter to the procedure. Let's create a new procedure to ...

Get Beginning Access™ 2007 VBA now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.