Creating Your First Event Procedure

You write Visual Basic code in units called procedures. A procedure contains a series of Visual Basic statements that perform an operation or calculate a value. An event procedure is a procedure that runs in response to an event. This section shows you how to create a simple event procedure that makes a command button and a text box work together on a form. The following illustration provides an example of how this interface may appear to the user.

Note   This example assumes that you have control wizards turned off in the form’s Design view. To do this, make sure the Control Wizards tool in the toolbox is not pressed in.

Because an event procedure is part of the design of the form or report that runs it, the first step is to create the form and add the controls. In this case, you create a form that isn’t based on a table or query and add a text box and a command button. Your code will refer to these controls by name, so it’s a good idea to set the Name property of each control on your form to something more descriptive than the default settings that Microsoft Access gives them. For example, Microsoft Access names the text box Text0 and the command button Command1. To be more descriptive, you could name the text box Message and the command button OK.

Tip   When you name the tables, fields, and other objects in your database, keep in mind that you’ll use these names to refer to the objects elsewhere in your application. Although descriptive names for objects with spaces are easier to recognize than more compact names, they can be difficult to use in expressions, SQL statements, and Visual Basic code. Consider using short, consistent names that don’t contain spaces and are easy to remember and type—for example, field names such as LastName and Phone.

After you’ve created your form and its controls and set their properties, you’re ready to write your first event procedure in the Module window.

Û To write the event procedure for the OK command button

  1. In Design view, right-click the object (form, report, section, or control) for which you want to write an event procedure, in this case, the OK command button. On the shortcut menu, click Build Event.

    Microsoft Access displays the Choose Builder dialog box.

  2. In the list box, click Code Builder, and then click OK.

    Microsoft Access opens the Module window and creates a template for the default event procedure of the object you selected, in this case, the Click event procedure. (The default event procedure is the one for which Microsoft Access thinks you’re most likely to add code.) The template for the OK command button’s Click event procedure is shown in the following illustration.

  3. Enter the code for the event procedure between the Sub and End Sub statements. For the OK command button’s event procedure, enter the following code:
    Message = "Hello, World!"
    

    This line of code sets the Message text box to the text string, “Hello, World!”

  4. Save and close the module.

    When you save the module, Microsoft Access sets the command button’s OnClick event property to [Event Procedure], as shown in the following illustration.

Now that you’ve written the event procedure, you’re ready to run it. To do this, you make the event happen on the form.

Û To run the OK_Click event procedure

  1. Click the Form View button on the toolbar to switch to Form view.
  2. Click OK.

    The event procedure runs and the text “Hello, World!” appears in the text box.

See Also   For more information on events and event procedures, see Chapter 6, “Responding to Events.”

Working with a Form or Report Module

The “Hello, World!” example in the previous section shows you how to create a new event procedure by using the Build Event command on an object’s shortcut menu. In addition to this method, Microsoft Access provides a variety of other ways to open a form or report module and create or modify its event procedures. Note that a form or report doesn't have a module by default. A form or report without a module is called a lightweight object, and typically loads and displays faster than a form or report with a module. However, using the Build Event command or any of the following procedures automatically creates a form or report module.

Û To open a form or report and its module at the same time

In the previous section, you learned how to open the default event procedure. You can create or open any event procedure directly from the property sheet.

Û To create or open any event procedure

  1. Open the form or report in Design view.
  2. Display the property sheet by right-clicking the form, report, section, or control, and then clicking Properties on the shortcut menu.
  3. In the property sheet, click the Event tab.
  4. Select the property box for the event procedure you want to open.

  5. Click the Build button to the right of the property box.

    If the event property already has an event procedure associated with it, Microsoft Access opens the Module window and displays the event procedure.

    If the event property is empty, Microsoft Access displays the Choose Builder dialog box. In the list box, click Code Builder, and then click OK. Microsoft Access opens the Module window and creates a template for the event procedure. If you don't want to see the Choose Builder dialog box, and always want to open the Module window by using an empty event property's Build button, you can select the Always Use Event Procedures check box on the Forms/Reports tab of the Options dialog box (Tools menu).

Writing and Editing Code

Editing the code in a module is much like editing text with any text editor. The blinking vertical line, or insertion point, marks the place on screen where you insert typed or pasted text.

As you begin using the Module window, you can take advantage of the following features designed to help you write Visual Basic code efficiently:

See Also   For more information on these features, or for a list of keyboard shortcuts in the Module window, search the Help index for “Module window.”

Note   When several people are using the same database, they use separate versions of the forms, reports, and modules in the database. If one person changes code, the others must close and reopen the database in order to see those changes. More than one person can edit the same form, report, or module at the same time. If you attempt to save a form, report, or module that has already been changed by someone else, Microsoft Access warns you that the module has changed since you opened the database.

Navigating Between Procedures

When you’re editing code in the Module window, you can move between procedures in the module by pressing CTRL+PAGE DOWN and CTRL+PAGE UP. In addition, by selecting objects and their procedures or events in the Object and Procedure boxes, you can go directly to a procedure or create a new procedure.

Although the Module window normally displays one procedure at a time, you can also view all the procedures in a module at once. To switch between Procedure view and Full Module view, click the Procedure View and Full Module View buttons in the lower-left corner of the Module window, as shown in the following illustration.

Using Bookmarks

When working with a large application, it’s easy to lose your place as you move between modules and procedures. To keep track of portions of code that you’re working on, you can set a bookmark to mark your place. To set a bookmark on the current line of code, point to Bookmarks on the Edit menu, and then click Bookmark. A blue square in the left margin of the Module window indicates that your bookmark is set.

To return to bookmarks in a module, point to Bookmarks on the Edit menu, and then click Next Bookmark or Previous Bookmark. To clear all bookmarks in all modules, click Clear All Bookmarks on the Bookmarks submenu.