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 the Forms group in the Navigation pane 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 property sheet. In the property sheet, click the Event tab. All the events to which you can tie code appear in the property sheet.

When you click an event name in the property sheet, a Build button (look for an ellipsis) appears to the right (see Figure 6-1). To write code that is 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 opens in the VBA Editor Code window.

Figure 6-1:

Sample control, event, and Build button.

11 Fomil

Selected object

Property Sheet le.tiuti tvyt CI- mu:' . El'.'.un a myButton)

On Kev Dowri

On Key Up_

Selected event property (On Click)

Build button

The first and last lines of the procedure that will execute in response to the event are 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 lines of VBA code for that procedure look 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 then choose FileOClose and Return to Microsoft Office Access from the VBA Editor menu bar. The VBA Editor closes, and you're back in the form's Design view. There you see the words

[Event Procedure]

in the property 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 m button, you're 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 the form's Design view, the Event tab in the property 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 memorize all the events supported by all the different controls. There are too many of them, many of which you'll probably never use. Just to give you some examples of events to which you can tie code, we 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 i On Change (Change): Occurs when the contents of a TextBox or ComboBox control change, such as when the user edits the contents of a 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 Update (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 preceding list is shown with two names. The first part, outside parentheses, is the name as it appears in the property 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 property of a control named PaymentMethod, that procedure is automatically named PaymentMethod_Change().

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

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

Responses

  • Julia
    How many class modules can you have in vba access 2007?
    7 years ago

Post a comment