Events Event Properties and Event Procedures

In order to customize your database applications or to deliver products that fit your users' specific needs, you'll be doing quite a bit of event-driven programming. Access 2003 is an event-driven application. This means that whatever happens in an Access application is the result of an event that Access has detected. Events are things that happen to objects and can be triggered by the user or by the system, such as clicking a mouse button, pressing a key, selecting an item from a list, or changing a list of items available in a list box. As a programmer, you will often want to modify the application's built-in response to a particular event. Before the application processes the user's mouse clicks and keypresses in the usual way, you can tell the application how to react to the activity. For example, if a user clicks a Delete button on your form, you can display a custom delete confirmation message to ensure that the user selected the intended record for deletion.

For each event defined for a form, form control, or report, there is a corresponding event property. If you open any Microsoft Access form in Design view and choose View | Properties, and then click the Event tab of the property sheet, you will see a long list of events your form can respond to (see Figure 1-4).

By selecting a specific control on the form and clicking the Event tab of the property sheet, you can see the events that can happen to the selected control while the form is running (see Figure 1-5). Forms, reports, and the controls that appear on them have various event properties you can use to trigger desired actions. For example, you can open or close a form when a user clicks a command button, or you can enable or disable controls when the form loads.

SÉT Text Box: TexlO

x]

TextO

Format Data | Event other Ali

Before Update

After Update

On Drty

On Undo

On Change

On Enter

Cn Exit

On Got Focus

On Lost Focus

On Cick

On DM CSck

On Mouse Down

On Meuse Move

On Mouse Up

On Key Down

On Key Up

On Key Press

1 1

Figure 1-5: Event properties for a text box control placed on a form.

É" Form ffc]

|Form

Format Data | Event | other All

Oo Current

A

öfter insert

Before Update

After Update

On Dirty

On undo

Co Delete

Before Del Confirm

After Del Confirm

On Load

Co Lhload -

Co Activate

Co Deactivate

Oo Got Focus

Co Lost Focus

Co Dfel Click

Oo Mouse Down

Co Mouse Move

Co Mouse Whed

Oo Key Down

CnKeyUfci

Key Preview

No

Co Filter

Co Apply Filter

Timer Interval

0

Before Screen rp

Co Cmd Enabled

Oo cmd Checked

Co Cmd Before Execute Oo Cmd Execute

Co Data Chonffe

Co Data Set Change

Co PrvotTable Change

Co Selection Change

Co View Changs

Co Disconnect

Before Query

Oo Query

After Layout

Before Render

After final Render . .

Figure 1-4: Event properties for a form.

Figure 1-4: Event properties for a form.

Figure 1-5: Event properties for a text box control placed on a form.

Introduction to Access 2003 VBA Programming

To specify how a form, report, or control should respond to events, you write event procedures. In your programming code, you may need to describe what should happen if a user clicks on a particular command button or makes a selection from a combo box. When you design a custom form, for example, you should anticipate and program events that can occur at run time (while the form is being used). The most popular event is the Click event. Every time a command button is clicked, it triggers the appropriate event procedure to respond to the Click event for that button. When you assign a program to an event property, you set an event trap. Trapping gives you considerable control in handling events. When you trap an event, you basically interrupt the default processing that Access would normally carry out in response to the user's keypress or mouse click. If a user clicks a command button to save a form, whatever programming code you've written in the Click event of that command button will happen. Keep in mind that code entered as an event procedure cannot be used as a standalone procedure. The event programming code is stored as a part of a form, report, or control and is triggered only when user interaction with a form or report generates a specific event.

0 0

Post a comment