MS-Access / Getting Started

Control event procedures

Controls also raise events. Control events are often used to manipulate the control's appearance or to validate data as the user makes changes to the control's contents. Control events also influence how the mouse and keyboard behave while the user works with the control. A control's BeforeUpdate event fires as soon as focus leaves the control (more precisely, BeforeUpdate fires just before data is transferred from the control to the recordset underlying the form, enabling you to cancel the event if data validation fails), whereas a form's BeforeUpdate does not fire until you move the form to another record. (The form's BeforeUpdate commits the entire record to the form's data source.)

This means that a control's BeforeUpdate is good for validating a single control while the form's BeforeUpdate is good for validating multiple controls on the form. The form's BeforeUpdate would be a good place to validate that values in two different controls are in agreement with each other (such as a zip code in one text box, and the city in another text box), instead of relying on the BeforeUpdate in each of the controls.

You create event procedures for control events in exactly the same way you create procedures for form events. You select [Event Procedure] in the Property Sheet for the event, and then add VBA code to the event procedure attached to the event. Table-5 shows each control event property, the event it recognizes, and how it works. As you review the information in Table-5, keep in mind that not every control supports every type of event.

Table-5 Control Events
EventWhen the Event Is Triggered
BeforeUpdateBefore changed data in the control is updated to the underlying recordset
AfterUpdateAfter changed data is transferred to the form's recordset
DirtyWhen the contents of a control change
UndoWhen the form is returned to a clean state
ChangeWhen the contents of a text box change or a combo box's text changes
UpdatedWhen an ActiveX object's data has been modified
NotInListWhen a value that isn't in the list is entered into a combo box
EnterBefore a control receives the focus from another control
ExitJust before the control loses focus to another control
GotFocusWhen a nonactive or enabled control receives the focus
LostFocusWhen a control loses the focus
ClickWhen the left mouse button is pressed and released (clicked) on a control
DblClickWhen the left mouse button is pressed and released (clicked) twice on a control or label
MouseDownWhen a mouse button is pressed while the pointer is on a control
MouseMoveWhen the mouse pointer is moved over a control
MouseUpWhen a pressed mouse button is released while the pointer is on a control
KeyDownWhen any key on the keyboard is pressed when a control has the focus or when a SendKeys macro action is used
KeyPressWhen a key is pressed and released on a control that has the focus or when a SendKeys macro action is used
KeyUpWhen a pressed key is released or immediately after a SendKeys macro is used
[Previous] [Contents] [Next]