Previous Page

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 typefor 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.

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

  In the Database window, select the form or report, and then click the Code button on the toolbar.

Microsoft Access opens the form or report and its module. You can also use the Code button in a form or report's Design view to open its module.

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.

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:

 Automatic statement building   When you type certain Visual Basic elements, Microsoft Access automatically tries to assist you in writing code by displaying a drop-down list of appropriate choices for the code element you've typed. For example, if you type an object variable, and follow it with a period to indicate that you intend to enter a method or property, Microsoft Access automatically displays a list of the methods and properties that apply to the object. (You use methods to perform operations on an object. You use properties to determine or change characteristics of an object.)

To complete the statement you're typing, you can either click an item in the list or continue typing your code. If you continue typing code, the list displays the closest match to what you've typed. To enter the selected item in the list at any time, press TAB. To make the list disappear, press ESC.

Note   You can press ENTER to enter the selected item in the list. However, pressing ENTER also moves the cursor to the next line, so you'll have to return to the line to enter any arguments or additional information.

To use automatic statement building, select the Auto List Members check box on the Module tab of the Options dialog box (Tools menu).

When the Auto List Members check box is selected, the statement-building lists appear automatically as you type. However, you may also want to explicitly cause Microsoft Access to display them, especially for lines of code you typed previously and want to edit. To display the list of methods and properties for an existing object, right-click the existing method or property name (to the right of the period) and then click List Properties/Methods on the shortcut menu.

Some methods take a constant as an argument. To display a list of available constants when you're entering arguments for a method, right-click in the Module window and then click List Constants on the shortcut menu.

If you've typed part of a property, method, or constant and want Microsoft Access to finish typing it for you, click Complete Word on the Edit menu.

 Quick Info   When you type a procedure or method name (followed by a space or an opening parenthesis), a tip automatically appears underneath the line of code you're writing. The tip gives syntax information about the procedure, such as the arguments you need to type to use it.

To display syntax tips, select the Auto Quick Info check box on the Module tab of the Options dialog box (Tools menu).

In addition, you can view information on any variable, constant, or procedure in your code by right-clicking the item and then clicking Quick Info or Parameter Info on the shortcut menu.

 Automatic syntax checking   As you move the insertion point off a line, Visual Basic checks the syntax of that line and displays a message if it finds an error. To enable automatic syntax checking, select the Auto Syntax Check check box on the Module tab of the Options dialog box (Tools menu).

 Drag and drop   If you want to move code you've written from one place to another in a module or between windows, you don't need to bother with the Copy and Paste commands. Just select the code you want to move, and then drag it to the new location.

To enable drag-and-drop editing, select the Drag-Drop In Editor check box on the Module tab of the Options dialog box (Tools menu).

 Undo command    Microsoft Access keeps track of the changes you make when editing code. By clicking the Undo button one or more times, you can reverse any changes you've made to a module since you opened it.

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.

 Object box   Displays the name of the selected object. Click the arrow to the right of the Object box to display a list of all objects associated with the form, then click an object in the list to display its procedures or events in the Procedure box.

 Procedure box   When (General) appears in the Object box, the Procedure box displays the name of the current procedure. When an object name appears in the Object box, the Procedure box displays the name of the event for the current event procedure. Click the arrow to the right of the Procedure box to display all the events for an object. Events that have event procedures appear in bold in the list. In the following illustration, for example, the Procedure box displays a list of all the events for the OK command button, and the Click event appears in bold.

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 Toggle 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.

© 1996 Microsoft Corporation. All rights reserved.

Next Page


Casa de Bender