Creating a Procedure

There are two ways to create a new procedure (that is, a subroutine or a function) within a code module. First, after selecting the correct project in the Project Explorer, we can select the Procedure option from the Insert menu. This will produce the dialog box shown in Figure 9.1. Just type in the name of the procedure and select Sub or Function (the Property choice is used with custom objects in a class module). We will discuss the issue of Public versus Private procedures and static variables later in this chapter.

The Add Procedure dialog box

Figure 9-1. The Add Procedure dialog box

A simpler alternative is to begin typing:

Sub SubName

or:

Function FunctionName

in any code window (following the current End Sub or End Function statement, or in the general declarations section). As soon as the Enter key is struck, Access will move the line of code to a new location and thereby create a new subroutine. (It will even add the appropriate ending — End Sub or End Function.)

Get Access Database Design and Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.