Technology & Information Management Instructor: Michael Kremer, Ph.D. Database Program: Microsoft Access Series DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6) Section 5
AGENDA 8. Events in MS Access
Events in MS Access 8.
8.1 OVERVIEW OF EVENTS 137 MS Access is event driven, better yet, the user s action case events to trigger. Access comes already structured with a couple of hundreds event procedures, its arguments and whether they are cancelable or not. Certain events are not triggered when caused programmatically. User actions: Data updates, keyboard, mouse, certain computer and network states. One action may cause a whole sequence of events.
8.1 OVERVIEW OF EVENTS 138 Unless you write code and attach it to those events, nothing is going to happen. When code or macro is attached to an event, it is executed automatically when the event takes place. The most difficult part is to figure out which event to use for which situation.
8.1 OVERVIEW OF EVENTS Event Category Applies to Events Mouse events: When a mouse is moved or the left button is clicked. Forms, Form Sections, Controls Click, DoubleClick, MouseDown, MouseUp, MouseMove Keyboard events When keys on the keyboard are pressed or when the Sendkeys action or method is used. Forms, Controls KeyDown, KeyUp, KeyPress 139 Window events When a form or report is opened, closed, or resized. Focus events When an object becomes active or loses focus. Data events When data is added, deleted, or changed. Filter events When a filter is created or applied. Print/Format events When a report is formatted for printing or printed. Error events When an error occurs. Timing events When a specified period of time is elapsed. Reference events When a reference is added or removed. Forms, Reports Forms, Reports, Controls Forms, Controls Forms Reports, Report Sections Forms, Reports Forms Application Open, Load, Unload, Close, Resize Enter, GotFocus, Exit, LostFocus, Activate, Deactivate Current, BeforeInsert, AfterInsert, Delete, Dirty, BeforeDeleteConfirm, AfterDeleteConfirm, BeforeUpdate, AfterUpdate, Change, Updated, NotInList Filter, ApplyFilter Format, Print, Retreat, NoData, Page Error Timer ItemAdded, ItemRemoved
8.2 SEQUENCE OF EVENTS 140
8.2 SEQUENCE OF EVENTS 141 Besides the obvious event, many other events are triggered. Event naming convention: Form level events Section-level events Control-level events After form is opened, first control based on tab order receives focus. Form_ FormHeader_, Detail_, FormFooter_ controlname_
8.3 RESPONDING OF EVENTS Respond to an event in the following way: First, choose the right event Place a reference in the event property. Event Property 142 Event Property Value Embedded Macro (New in Access 2007) Standalone Macro Function (not recommended) Event Procedure
8.3 RESPONDING OF EVENTS 143 Most event properties start with the preposition ON. That means, exactly when the event takes place the event handler is executed. Some other event handlers execute Before or After an event. Event Handlers: Macro Event procedure Function call (not recommended, only function, not sub) Embedded Macro (New in Access 2007)
8.3 RESPONDING OF EVENTS 144 When an event procedure is chosen as a handler, MS Access automatically provides the framework for the procedure including arguments. Event procedure name: Objectname_eventname(arguments) Do not change the name of the procedure or it s arguments!! Before creating event procedures, make sure to name your objects according to naming convention! Why? If you change the name of an object after creating the event procedure, you break the link.
8.3 RESPONDING OF EVENTS 145 Event procedures carry additional arguments, such as Cancel. This means, that the event is cancelable. Other events carry other parameters about system states.
8.3 RESPONDING OF EVENTS 146 Respond to an event in the following ways: Select standalone macro: Set to Event Procedure: Clicking inside an empty event property displays Choose Builder dialog box: Macro Builder Embedded Macro Expression Builder Select function Code Builder Create event procedure Build button
8.3 RESPONDING OF EVENTS 147 Certain action cannot be trapped: Select a menu/ribbon command Scrolling Clicking the right/middle button Selecting a toolbar button Record locking Create customized record locking procedure using timer event. Use macros sparingly, if at all: Error handling, no looping, difficult to debug, no transactions, passing parameters.
8.4 FORM EVENTS 148 Opening a form: Open Load Activate Open Event: Make changes to data source, such as filtering/selecting records using criteria Open another form, move focus to a specific control, or ask for a criteria from the user. Load Event: Calculate data, change data in controls, or open another form based on linked values. If form contains controls: Enter GotFocus If no controls, then the form receives focus. If another form was active before: Deactivate
8.4 FORM EVENTS Events on a form occur at three levels: Form level: Higher level events (opening, closing) Section level: Clicking or moving mouse Control level: Entering, Exiting, Focus, Data changes Control events vary by control type. 149
8.4 FORM EVENTS 150 Opening and closing a form seems like a trivial process. Opening a form Opening a form having a control without controls
8.4 FORM EVENTS If form contains a sub form, then sub form events are fired first. 151 When the form containing a sub form is closed, the sub form events are fired last.
8.4 FORM EVENTS Closing a form having pending changes 152
8.4 FORM EVENTS 153
8.4 FORM EVENTS 154
8.4 FORM EVENTS 156 Minimize, Maximize, Restore trigger the resize event. Dragging the border to size a form triggers the resize event multiple times. If multiple forms are open, one form is minimized. Minimizing Maximizing
8.4 FORM EVENTS 157 Events between controls on different forms. Form is deactivated, control loses focus, but is not exited. Opening form 2 Closing form 2
8.5 DATA RELATED EVENTS 157 Record and Control buffer at form level. When data is bound to a form, first the entire record is loaded into the record buffer. Active control value is loaded into control buffer.
8.5 DATA RELATED EVENTS 158 These two buffers represent the two different undo levels. Press [Esc] once to undo the current field, press [Esc] again to undo the entire record. Whenever there are pending changes, form is set to dirty. Use dirty event to check whether data changes have been made. Also key events are triggered because data changes mean user typed values.
8.5 DATA RELATED EVENTS 159 When a value is changed in a control and user tabs over to the next control: Although no key was pressed in the City control, the [tab] key acts like a bridge. KeyDown happens in CustomerName control, KeyPress and KeyUp in the City control. BeforeUpdate and AfterUpdate events are triggered at control level. BeforeUpdate: Before data is saved into record buffer. AfterUpdate: Record buffer is now updated
8.5 DATA RELATED EVENTS 160 When entire record is saved, the following events take place. First Record, City Control: Data is changed: KeyDown, KeyPress, Form_Dirty, Change, KeyUp [Tab] key is pressed: KeyDown, BeforeUpdate, AfterUpdate Form level events: BeforeUpdate, AfterUpdate City control level events: Exit, LostFocus Form level events: Current Second Record, Customer Control Enter, GotFocus, KeyPress, KeyUp
8.5 DATA RELATED EVENTS 161 Between Before and AfterUpdate at control level, current record buffer is updated with current control buffer. Between Before and AfterUpdate at form level, database is updated with current record buffer. Use the BeforeUpdate event at the control level: Validate the value entered in a control Compare the changed value with the old value (OldValue property) Use the AfterUpdate at the control level: Change the value the user entered React to the change in value and set a value in another control
8.5 DATA RELATED EVENTS 162
8.5 DATA RELATED EVENTS 163 Use the BeforeUpdate event at the form level: Validate the data entered (cross validation of fields) Make changes in current record before the record is saved Use the AfterUpdate event at the form level: To react to the saving of the record
8.5 DATA RELATED EVENTS 164
8.5 DATA RELATED EVENTS 165