One of the best ways to learn event sequence is to see events in action. In the WeddingMC.accdb sample database on the companion CD, you can find a special form that you can use to study events. Open the sample database, and then open the WeddingEvents form, as shown in Figure 17–2. (The form’s Caption property is set to Wedding List.)
Figure 17–2: Use the WeddingEvents form to study event sequence.
When you open the form, it also opens an event display pop-up form that shows the events that have occurred. All the events (except the Mouse and Paint events that fire so frequently that they would make it hard to study other events) are set to write the event name to the pop-up window. The pop-up window shows the most recent events at the top. (The event names displayed in the window show an underscore rather than a blank between words in the name.) When you open the form, the initial events are as follows:
Form Open This signals that the form is about to open.
Form Load This signals that the form is now open and the record source for the form is about to be loaded.
Form Resize The form’s Auto Resize property is set to Yes, so this indicates that Access is resizing the window to show an exact multiple of rows. (The form’s Default View property is set to Continuous Forms.)
Form Activate The form has now received the focus.
Form Current The form has now moved to the first row in the record source.
Title Enter The first control in the tab order has been entered.
Title GotFocus The first control in the tab order now has the focus.
After the form opened, we pressed the Tab key to move from the Title field to the First Name field. (The name of the control is First.) The form has its Key Preview property set to Yes, so you can see the keyboard events for both the form and the controls. The events occurred in the following sequence:
Form KeyDown The form detected that the Tab key (key code 9) was pressed.
Title KeyDown The Title combo box control detected that the Tab key was pressed.
Title Exit Pressing the Tab key caused an exit from the Title combo box control.
Title LostFocus The Title combo box control lost the focus as a result of pressing Tab.
First Enter The First text box control (First Name) was entered.
First GotFocus The First text box control received the focus.
Form KeyPress The form received the Tab key.
First KeyPress The First text box control received the Tab key.
Form KeyUp The form detected that the Tab key was released.
First KeyUp The First text box detected that the Tab key was released.
In Figure 17–2, you can also see a Form Timer event listed. The Timer Interval property for this form is set to 20,000 (the value is milliseconds), so you should see the timer event occur every 20 seconds as long as you have the form open.
You can have fun with this form moving around and typing new data. You’ll be able to see each character that you enter. You’ll also see the control and form BeforeUpdate and AfterUpdate events when you commit changed values. You can also switch to PivotChart or PivotTable view to watch events related to those views.
Figure 17–3 shows you a conceptual diagram of how editing data in a form works and when the critical Current, BeforeUpdate, and AfterUpdate events occur.
Figure 17–3: Many events occur behind the scenes when you edit data on a bound form.
Of course, the ultimate goal is to update data in your database table. When Access opens a form, it builds a special buffer to contain the contents of the current record displayed on the form. As you move from control to control on the form, Access fetches the field bound to each control from this record buffer and puts it into a special control buffer. As you type in the control, you’re changing the control buffer and not the record buffer or the actual record in the table.
If you have changed the data in a control and then you move to another control, Access signals the BeforeUpdate event for the control. If you do not cancel the event, Access then copies the updated contents of the control buffer into the record buffer and signals the control’s AfterUpdate event. Access then fetches into the control buffer the contents of the field bound to the control to which you moved from the record buffer and displays it in the control.
If you undo any edit, Access signals the appropriate Undo event and refreshes either the control buffer from the record buffer (if you undo a control) or the original record from the table into the record buffer (if you undo all edits). When you save a changed record-either by clicking the Save button in the Records group on the Home tab, by moving to another record, or by closing the form-Access first signals the BeforeUpdate event of the form. If you do not cancel this event, Access writes the changed record to your table and signals the AfterUpdate event of the form.
Now that you understand events and event sequence, the next chapter discusses macros and how you can design macro actions to respond to form and report events.