Microsoft Office Tutorials and References
In Depth Information
Writing Your Own VBA Procedures
Public Sub name ()
name is the name you typed in the Add Procedure dialog box.
Creating a procedure through the Add Procedure dialog box isn’t really
necessary. You can just type the Function statement or Sub statement
in the Code window, and VBA automatically adds the corresponding End
Function or End Sub statement.
The statements that the Add Procedure dialog box adds to the module use
only the bare-minimum number of optional arguments supported by the
Function and Sub statements. Depending on what the procedure does, you
may need to define some additional arguments, as discussed in “Passing
arguments to procedures,” later in this chapter.
Creating a new event procedure
Recall that an event procedure is already tied to some event, such as clicking
a button on a form. If you want to create a new event procedure for a control
on a report or form, follow these steps:
1. In Design view, open the form or report that contains the control for
which you want to create a new procedure.
In the Navigation Pane, right-click the object’s name and choose Design
View from the contextual menu.
2. Select the control for which you want to create a procedure.
3. Click the Event tab in the property sheet.
If the property sheet isn’t open, right-click the selected control, choose
the Properties option from the contextual menu, and then click the
4. Click the event that should trigger the procedure into action and then
click the Build button that appears.
If the selected control is a button, for example, and you want a user to
click that button to trigger the procedure, click the On Click event and
then click its Build button.
The Choose Builder dialog box opens.
5. Select Code Builder from the list and then click OK.
The class module for the form or report opens in Visual Basic Editor,
with the cursor resting in a new procedure.