Working with Class Procedures

Every form in a database has a class module in which you can store code that's used only by that form. To get to a form's class module, you first have to click Forms in the database window and then open an existing form in Design view or create a new form. Typically, you want to tie your code to an object and event.

For example, a button on a form is an object. Every button has an On Click event that occurs whenever a user clicks the button in Form view. If you want to write code that runs every time someone clicks that button, you want to tie the code to that button's On Click event.

To see which events an object on a form offers, first select the object. The name of the object appears in the Properties sheet. In the Properties sheet, click the Events tab. All the events to which you can tie code appear in the Properties sheet.

When you click an event name in the Properties sheet, a Build button (look for an ellipsis) appears to the right (see Figure 6-1). To write code that will be executed each time the event occurs, click that Build button. The first time you do, you see a Choose Builder dialog box. Choose Code Builder and then click OK. The form's class module will open in the VBA editor Code window.

Figure 6-1:

Sample control, event, and Build button.

Selected object (MyButton)

Selected object (MyButton)

Figure 6-1:

Sample control, event, and Build button.

Selected event (OnClick) Build button

The first and last line of the procedure that will execute in response to the event is already typed into the class module for you. The name of the procedure is a combination of the object and event name, followed by a pair of parentheses. For example, the procedure that executes whenever someone clicks a button named myButton is myButton_OnClick(). The first and last line of VBA code for that procedure looks like this in the class module:

Private Sub myButton_Click() End Sub

Any VBA code that the event is to execute needs to be typed between those two lines of code. After you write your code, choose FileOSave and Return to Microsoft Access from the VBA editor menu bar. The VBA editor closes, and you'll be back to the form's Design screen. There you'll see these words:

[Event Procedure]

m in the Properties sheet, next to the name of the property for which you wrote the code. In the future, whenever you click that property and click the Build button, you are taken straight to the form's class module, with the cursor already placed inside the procedure.

Every type of control has a unique combination of events to which you can tie code. When you click a control in forms Design, the Event tab in the Properties sheet shows you all the events the control exposes. Some controls offer quite a few more events than the button control shown in Figure 6-1.

You don't need to learn all the events supported by all the different controls right now. There are too many of them, many of which you'll probably never use. But just to give you some examples of events to which you can tie code, I offer the following quick list.

1 On Click (Click): Occurs when the user clicks the control (points to the control and clicks the left mouse button)

1 On Mouse Down (MouseDown): Occurs when the user points to the control and then clicks either the left or right mouse button

1 On Change (Change): Occurs when the contents of a TextBox or ComboBox control changes, such as when the user edits the contents of field

1 Before Update (BeforeUpdate): Occurs after the user makes a change to data in the control but before the new data is inspected and before the underlying record is updated

1 After Focus (AfterUpdate): Occurs after the user changes the contents of the control, the new data has passed any data validation rules, and the underlying record has been updated

Here's the reason each item in the list above is shown with two names. The first part outside parentheses is the name as it appears in the Properties sheet. The name in parentheses (like Click) is the official VBA name and also the name used in any VBA procedure that you tie to the event. For example, if you tie a procedure to the On Change event of a control named PaymentMethod, that procedure is automatically named PaymentMethod_Change().

So that, in a nutshell, is how you work with class procedures. Examples always help, so your first forays into programming Access forms will all use class procedures to illustrate their techniques.

Remember, a module is a container that contains VBA code, where that code is organized into chunks called procedures. A class module is a module that contains class procedures. The module and the procedures within it belong to the form (or report) to which the class module is attached.

0 0

Post a comment