Calling a Procedure from an Event

At this stage of the game, your database contains a standard module that contains a Sub procedure named SkipLabels(). Because you haven't yet tied the SkipLabels procedure to any event, nothing is in the database yet to take advantage of SkipLabels().

Recall that earlier in this chapter, we show a form with a control named LabelsToSkip (it stores the number of labels to be skipped over) as well as a Cancel button and a Print button (refer to Figure 8-6). If the user clicks Cancel, you just want SkipLabelsForm to close without doing anything. If the user clicks the Print button, you want the form to call SkipLabels with the appropriate label report name and number of labels.

When you want an event procedure on a form to call a standard procedure, use the syntax

Call procedureName (arguments)

where procedureName is the name of the procedure to call, and arguments are values for whatever required arguments the procedure is expecting. SkipLabels() requires at least two arguments: the name of the labels report and the number of labels to skip. Here's how you could get the Print button in the SkipLabels form to call SkipLabels () when clicked:

1. Open SkipLabelsForm (or whatever form you created) in Design view and click the button that will call SkipLabels.

2. On the Event tab of the Properties sheet, click the On Click event property for the Print button.

3. Click the Build button and choose Code Builder. You see the VBA Editor with the cursor inside the event procedure.

4. Type the following line into the procedure:

Call SkipLabels("[ YourReportName]", [LabelsToSkipj.Value)

and substitute YourReportName with the name of the report in your database that prints labels.

For example, if your database contains a report named Avery 8462 Labels, you type Call SkipLabels("Avery 8462 Labels", [LabelsToSkip]. Value), as shown in the second procedure — PrintBttn_Click() in Figure 8-9.

Figure 8-9:

The Print Bttn_ Click() procedure called the Skip Labels Sub.

Figure 8-9:

The Print Bttn_ Click() procedure called the Skip Labels Sub.

The first procedure in that figure — CancelBttn_Click() — in that class module just closes SkipLabelsForm without doing anything and is tied to the On Click event of the form's Cancel button.

The syntax for calling a custom VBA function from an Event procedure is =functionName( arguments), which is clearly different from calling a Sub procedure with Call procedureName (arguments). We talk more about custom functions in Chapter 11.

5. Choose FileOSave and Return to Microsoft Office Access from the VBA Editor's menu bar.

The button's On Click event property shows Event Procedure, as usual. Now you can close and save the form and then reopen it in Form view to try it out.

You can do some fancier things with SkipLabelsForm in later chapters. For example, you can allow the user to choose any one of several label formats, or you can let the user specify a filter condition by using simple options on a form. For now, if you got this far, you did great. You created a Sub procedure named SkipLabels() that you can easily drop into just about any database you create.

Was this article helpful?

0 0

Post a comment