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
Microsoft Access displays the Choose Builder dialog box.
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.
Message = "Hello, World!"
This line of code sets the Message text box to the text string, “Hello, World!”
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
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.”
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
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
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).
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:
To complete the statement you’re typing, you can either double-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, functions, and properties 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 where the constant setting would be entered 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.
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.
To enable drag-and-drop editing, select the Drag-and-Drop Text Editing check box on the Module tab of the Options dialog box (Tools menu).
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.
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.
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.