Previous Page

CHAPTER  6

Responding to Events

Rather than running entire programs line by line, Microsoft Access applications run macros and event procedures in response to specific events for particular objects, such as a change to data in a field, or a mouse click on a command button. Understanding the events Microsoft Access recognizes can help you create powerful, flexible applications. This chapter describes the Microsoft Access event model and shows you how to manage events in your applications.

Working with Events

Microsoft Access applications are event-driven; this means that the way they behave depends on how objects are designed to respond to events. Objects in Microsoft Access respond to the following types of events:

 Mouse clicks

 Changes in data

 Keystrokes a user types

 Objects receiving or losing the focus

 Forms being opened, closed, or resized

 Reports being printed or formatted

 Run-time errors

The focus is the application's ability to receive input or respond to a user's mouse or keyboard actions. In Microsoft Windows, only one item at a time can have the focus. For example, when a user types, characters appear in a text box only if the text box has the focus. Which object or control receives the focus is determined by a user's actions, such as clicking in a text box or pressing TAB to move to a control. Before a user acts, settings made at design time determine which control has the focus. For example, when a user first opens or switches to a form, the control that has the focus is the one with the lowest TabIndex property setting. You can also explicitly set the focus in code by using the SetFocus method.

See Also   For more information on the TabIndex property or the SetFocus method, search the Help index for "TabIndex property" or "SetFocus method."

You can see the events that are generated in Microsoft Access by opening the ShowEvents form in the Orders sample application. The ShowEvents form, a special version of the Orders form, records each event as it occurs. An accompanying EventHistory form lists the name of the event and the type or name of the object on which the event occurred, using the format object_event. For example, if a Click event occurs on the ShowEvents subform, the line "[Subform]Form_Click" is added to the Events list on the EventHistory form.

The EventHistory form lists events in reverse order, with the most recent event at the top of the list. The form lists all events except MouseMove events, which occur each time you move the mouse pointer, and would quickly fill up the list if they were included.

By default, Microsoft Access automatically responds to events with built-in behaviors defined for each object. For example, when a user enters or changes data in a text box, Microsoft Access automatically checks to make sure the data is of the right type.

In addition, each object in Microsoft Access has a set of event properties that correspond to each event to which the object can respond. For example, the following table lists some of the event properties and corresponding events for a check box.

Event property

Event

OnGotFocus

GotFocus

OnKeyDown

KeyDown

OnKeyPress

KeyPress

OnKeyUp

KeyUp

OnLostFocus

LostFocus

OnMouseDown

MouseDown

OnMouseMove

MouseMove

OnMouseUp

MouseUp

See Also   For a complete list of events and event properties, as well as information on when each of these events occur, search the Help index for "events, listed alphabetically."

You can specify a further response to an event by setting the object's corresponding event property. When an event occurs that an object can respond to, Microsoft Access uses the setting of the object's corresponding event property to determine how to respond:

 If the event property is blank, Microsoft Access responds to the event only with its built-in behavior.

 If the event property is set to the name of a macro, and the event can't be canceled, Microsoft Access first performs the built-in behavior, and then runs the macro. If the event property is set to the name of a macro and the event can be canceled, Microsoft Access first runs the macro, and then performs the built-in behavior.

 If the event property is set to [Event Procedure], and the event can't be canceled, Microsoft Access first performs the built-in behavior, and then calls the appropriate event procedure. If the event property is set to [Event Procedure], and the event can be canceled, Microsoft Access first runs the macro, and then performs the built-in behavior.

See Also   For information on which events can be canceled, see "Canceling Events" later in this chapter.

For example, when you click a command button whose OnClick event property is set to a macro, Microsoft Access:

1.  Makes the button appear pressed in momentarilythe built-in behavior when a Click event occurs on a command button. Note that the Click event can't be canceled.

2.  Runs the macro.

   

When the event property is set to [Event Procedure], Microsoft Access responds to the event by running the appropriate event procedure in addition to performing its built-in behavior. Event procedures are named for the event and the object for which they occur, in the format object_event. For example, if a user clicks the Products command button, Microsoft Access:

1.  Makes the Products command button appear pressed in momentarilyits built-in behavior.

2.  Runs the Products_Click event procedure.

   

Note   When you create an event procedure, Microsoft Access automatically sets the appropriate event property to [Event Procedure] if the property doesn't already have a setting. As an alternative, you can set the property to [Event Procedure], and then create the event procedure separately. For more information on creating an event procedure, see Chapter 2, "Introducing Visual Basic," or search the Help index for "event procedures, creating."

You can also have Microsoft Access call a function in response to an event. To do so, add to the appropriate event procedure an expression that calls the function, or type an equal sign (=) followed by the function name as the event property setting in the property sheet.

For example, to call the CheckValues function when a form opens, you can type the following OnOpen property setting in the form's property sheet:

=CheckValues()

Note   Using an expression that calls a function as an event property setting for a form or control is useful when you want to use code and the form's HasModule property is set to No so that it loads more quickly. For more information, see "Optimizing Form Loading and Paging" in Chapter 13, "Optimizing Your Application."

The macros and Visual Basic code that Microsoft Access runs in response to events control how the objects in your application work together. By managing events, and the macros and Visual Basic code that Microsoft Access runs in response to events, you can create powerful, flexible database applications.

© 1996 Microsoft Corporation. All rights reserved.

Next Page


Casa de Bender