Chapter 6 - Emunix Emich

advertisement
1
Chapter 6
Event-Driven
Programming and Access
Events
6
Chapter Objectives
• Learn the advantages and disadvantages of
event-driven programming
• Trigger macros, functions, and event
procedures through an Access event
• Develop a macro that highlights the control
that has focus
6
Chapter Objectives
• Distinguish between the various user
actions, data retrieval, and data update
events
• Determine the most appropriate event to use
in a particular situation
6
Event-Driven Programming and
Access Events
• Events
 Actions recognized by an object
 Include button clicks, keystrokes in text boxes,
the opening of forms, and record deletions
• Event-driven programming
 Type of programming where code associated
with some action or change in the system’s
environment executes when the event occurs
6
Event-Driven Programming
• Event-driven programming
 Supports the creation of systems that can react
to a wide range of user actions
 Does not require the development of
management components that anticipate the
variety of user actions that can occur
 Where system developer identifies important
events and associates macros and procedures
with them
6
Event-Driven Programming
• Some sources of frustration from event-driven
programming include:







Events occurring outside an anticipated sequence
Anticipated events that do not occur
Events occurring at an inappropriate time
Events occurring more than once
Timing problems between events
Difficulty in locating relevant code
Difficulty in understanding how the system executes
6
The World According to
Events
• Event property
 An object property that associates a procedure
or macro with an event that is recognized by the
object
 If the event occurs, the macro or procedure
identified by the event property executes
6
Overview of Events in Access
• An event is associated with a form, report, or
control object through an event property
Figure 6-1
Using an
event property
to trigger a
macro that
runs
frmProspects
6
Overview of Events in Access
• VBA procedures are identified by:
 A key phrase [Event Procedure] or
 An equals sign followed by a function name
• Event procedure
 A subroutine contained within a form or report
class module that is closely associated with an
event
6
Overview of Events in Access
Figure 6-2
Form_Current
event procedure
for frmProspects
6
Exploring Access Events
in Detail
• Access recognizes three types of events:
 User action events
 Occur primarily in the application’s user interface
 Data retrieval events
 Occur when data are placed in a form or report
 Data update events
 Occur when updates in a form are propagated to the
form’s record source
6
Events Related to User
Actions
• Exit event
 Occurs for the control that the cursor is leaving
• LostFocus event
 Occurs for the control in the old record that no longer
has the cursor
• BeforeUpdate and AfterUpdate events
 If changes were made in the previous record, the cursor
movement to a new record causes changes to be saved
 Events occur for the record being saved
6
Events Related to User
Actions
• Current event
 Occurs for the form when the new record becomes the
one (currently) being manipulated
• Enter event
 Occurs for the the control to which the cursor is moving
• GotFocus event
 Occurs for the control that contains the cursor in the
new record
6
Events Related to User
Actions
Table 6-1
User action
events
6
Events Related to User
Actions
Table 6-1
User action
events
(continued)
6
Events Related to User
Actions
Table 6-1
User action
events
(continued)
6
Events Related to User
Actions
Table 6-1
User action
events
(continued)
6
Focus
• Generally speaking, when an Access object
becomes the object of attention, it has “received
focus”
• As the attention moves from one object to another,
the object that had focus “loses focus” to the next
object that is receiving the focus
• Activate, Deactivate, Enter, Exit, GotFocus, and
LostFocus events
 Related to the process associated with moving from one
object to another
6
Event Timing
• Events related to focus demonstrate the
importance of understanding event timing
Figure 6-3 Order of control focus events
6
Event Timing
• When the cursor is in Control X and the
user clicks Control Y or tabs to Control Y,
the following events occur:




The Exit event for Control X
The LostFocus event for Control X
The Enter event for Control Y
The GotFocus event for Control Y
6
Event Timing
• Other user action events are mouse clicks
and keypresses
 DblClick event
 Access activates when the user clicks the mouse
twice
 Rarely used with a command button
 Users expect to double-click over a text box, list
box, or combo box
6
Events Related to Data
Retrieval
• Unlike the user action events, most data
retrieval events are not defined by the
different ways in which a user interacts with
the system
 Data retrieval events are identified by their role
in placing data from forms and reports
• Opening and closing of forms and reports
instigate most of the data retrieval events
6
Events Related to Data
Retrieval
Table 6-2
Data
retrieval
events
6
Events Related to Data
Retrieval
Table 6-2
Data
retrieval
events
(continued)
6
Events Related to Data
Retrieval
• Commonly used data retrieval events:
 Open
 Occurs when a form is opened but before the first
record displays
 Load
 Occurs after a form is opened, after the first record
is read from the database but just prior to the first
record being displayed
6
Events Related to Data
Retrieval
• Commonly used data retrieval events
(continued):
 Current
 Occurs when focus moves to a new record (making
it the current record) and when the form opens
6
Events Related to Data
Retrieval
Figure 6-4
Sequence of
events caused
by opening
and closing a
form
6
Data Update Events
Table 6-3
Data
update
events
6
Data Update Events
Table 6-3 Data update events (continued)
6
Data Update Events
Figure 6-5
Sequence of
events for
inserting and
updating records
through a bound
form
6
Data Update Events
• Current control buffer
 Where Access writes user-entered changes to a
control
 Storage area that contains the displayed value
of the control with focus
• If code in the BeforeUpdate event procedure
does not cancel the event, Access writes the
data to the current record buffer and Access
triggers an AfterUpdate event
6
Data Update Events
Figure 6-6
Focus
changes
before and
after
update
events and
buffers
6
Data Update Events
• BeforeUpdate event for a control
 Occurs when the user finishes adding or
changing data in the control
 OddValue property of a control
 Allows the system to compare the user-typed value
with the previous value of the control
• AfterUpdate event for a control
 Occurs after Access writes changed data in a
control to the record buffer
6
Data Update Events
• NonList event for a control
 Occurs when a user enters a value in the text
box portion of a combo box, but that value is
not in the combo box list
• BeforeInsert event for a form
 Occurs when the user types the first character in
a new record, but before the record is actually
created
6
Data Update Events
• AfterInsert event for a form
 Occurs after changes to the record are
completed and written to the database
• BeforeUpdate event for a form
 Occurs before changed data are written from
the current record buffer to the database
6
Data Update Events
• Delete event
 Occurs when a user performs any action that causes a
record to be deleted
• BeforeDelConfirm
 Occurs after the user deletes records but before Access
displays a dialog box requesting delete confirmation
• AfterDelConfirm
 Occurs after a user confirms a deletion and the records
are actually deleted or the deletions are canceled
6
Data Update Events
• Message box
 A window that displays a brief message and
then waits for the user to click a button
• MsgBox action
 Displays a message box
 MsgBox function can also be used in VBA code
6
Time and Error Events
• Timer event
 Occurs in forms at regular intervals when a
specified amount of time has passed
 Used to create a splashscreen that appears when
you open a database
• TimerInterval property
 Where the amount of time needed to pass
before Timer event will occur, is specified
6
Time and Error Events
• Use the timer event to create a splashscreen that
appears when you open a database
Figure 6-7
Event properties
of
frmSplashScreen
6
Time and Error Events
• Error events
 An event procedure attached to the OnError event
property of a form specifies VBA code to execute when
an error is detected on a form
• Examples of problems caught by OnError event
property include:
 Duplicate primary keys
 Violations of validation constraints
 Violation of foreign key constraints
6
Chapter Summary
• Event-driven programming
 An event is triggered by a change in an Access
object, a change in the environment
surrounding an object, or a user action
 Each event is associated with an event property
of an object
 The value of an event property identifies the
macro, function, or event procedure that should
execute when the event occurs
6
Chapter Summary
• Access events can be classified into several
categories:
 Events related to user actions center on interactions
with the user interface
 Focus events are a type of user action events that occur
when the cursor moves from one control to another
 Data retrieval events are associated with the display of
data in a form or report
 Data update events respond to a user-caused changes to
the values of some control
6
Chapter Summary
• Event timing and event interaction
sometimes make program debugging and
maintenance difficult
 Macros and VBA procedures should be
associated with the objects and events that are
most closely aligned with the code’s results and
conditions for execution
Download