Using Form Events

In Chapter 1, you got a quick introduction to events, event properties, and event procedures. I walked you through an event procedure that changed the background color of a text box control placed on a form. Now is a good time to go back to the beginning of this book and review these topics. Here's a rundown of the terms you need to be familiar with:

Event — Events are things that happen to an object. Events occur when you move a mouse; press a key; make changes to data; open a form; add, modify, or delete a record; etc. An event can be triggered by the user or by the operating system.

Event property — Forms, reports, and controls have various event properties you can use to trigger desired actions. When an event occurs, Microsoft Access runs a procedure assigned to an event property. Event properties are listed in the Event tab of the object's property sheet. The name of the event property begins with the word On followed by the event's name. Therefore, the On Click event property corresponds to the Click event, and the On Got Focus event property is used for responding to the GotFocus event.

Event procedure — This is programming code you write to specify how a form, report, or control should respond to a particular event. By writing event procedures you can modify the application's built-in response to an event.

Event trapping — When you assign a program to an event property, you set an event trap. When you trap an event you interrupt the default processing that Access would normally carry out in response to the user's keypress or mouse click.

Sequence of events — Events occur in a predefined order. For example, the Click event occurs before the DoubleClick event. When you perform an action, several events occur, one after the other. For instance, the following form and control events occur when you open a form:

Open - Load - Resize - Activate - Current - Enter (control) - GotFocus (control)

Closing the form triggers the following control and form events: Exit (control) - LostFocus (control) - Unload - Deactivate - Close

To find out whether a particular event is triggered in response to a user action, you may want to place the MsgBox statement inside the event procedure for

Part IV

the event you want to test. Microsoft Access forms, reports, and controls recognize numerous events.

Events can be organized by object (form, report, control) or by cause (what caused the event to happen). This chapter is filled with numerous examples of event procedures you can write to make your forms and reports dynamic. You can also experiment with various events in the data entry/lookup application located in the downloadable AssetDataEntry.mdb file. You will find several examples from this application discussed in Chapter 26.

Microsoft Access forms can respond to a variety of events. These events allow you to manage entire records and respond to changes in the data. You can determine what happens when records are added, changed, or deleted, or when a different record becomes current. You can decide how the form appears to the user when it is first displayed on the screen and what happens when the form is closed. You can also manage problems that occur when the data is unavailable. As you design your custom forms, you will find that some form events are used more frequently than others. The following sections show numerous hands-on examples of event procedures you can write for Access forms.

0 0

Post a comment