Automating Database Processing
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 1
Chapter 6
“An organization’s ability to learn, and translate that learninginto action rapidly, is the ultimate competitive advantage.”
—Jack Welch
XPXPChapter Introduction• Design and implement user-friendly menu
– Called switchboard• Macros
– Automate repetitive tasks– Conditions
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 2
XPXPTools Covered in This Chapter• AutoExec macro• AutoKeys macro• Macro window• Startup dialog box• Switchboard Manager
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 3
XPXPLevel 1 Objectives: Automating Tasks with Switchboards and Macros• Create a well-designed switchboard to provide a
user interface for a database• Automate tasks by creating basic macros• Specify what happens when a database opens
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 4
XPXPUnderstanding Switchboards• Switchboard
– Special kind of form – Appears when database opened– Lists options for working with database objects
• Select up to eight objects to list on switchboard• Determine what action Access should take when
users select option
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 5
XPXPUnderstanding Switchboards (continued)• Typical switchboard buttons
– Open forms reports and other objects – Open other switchboards that list additional database
objects – Close switchboard– Open the Navigation Pane– Close Access
• Main switchboard– Large number of database objects– Launches other switchboards
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 6
XPXPTypical Switchboard Design
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 7
XPXPDesigning a Switchboard• Switchboard Manager
– Tool to design and customize switchboard– Create main switchboard– Add buttons– Specify button properties– Access creates switchboard form
• Only one switchboard per database– Can contain many pages
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 8
XPXPCreating a Switchboard • Open Switchboard Manager
– Switchboard Manager dialog box– Use five buttons on Switchboard Manager dialog box to
perform tasks
• Create secondary switchboards first• Verify new form creation• Switchboard item table
– Keeps track of switchboard items commands and arguments – Should be altered only through Switchboard Manager
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 9
XPXPSwitchboard Manager Buttons
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 10
XPXPEdit Switchboard Item Dialog Box
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 11
XPXPSwitchboard Commands
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 12
XPXPFormatting a Switchboard• By default Access switchboards share same
layout and design – Including background colors and placement of title
• Switchboard Manager doesn’t provide options for modifying switchboard design– Format switchboard in form design view– Do not change command buttons or properties
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 13
XPXPUnderstanding Basic Macros• Macro
– Action or series of actions – Save time and ensure accuracy– Usually assigned to key, key combination, or button– Composed of series of actions organized in sequence
in which they should be performed• Create macro
– Using Macro window
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 14
XPXPCommon Macro Actions
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 15
XPXPCreating a Macro• Create Access macro in Macro window• Select the Create tab, and then click the Macro
button in the Other group• Action column
– List actions macro performs– Click list arrow in action column– Access provides over 50 different actions
• Comment column– Document action by describing what task it performs
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 16
XPXPCreating a Macro (continued)• Action arguments pane
– Provide additional information for performing macro action
– Arguments needed vary depending on selected action chosen
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 17
XPXPRunning a Macro• In Macro window
– Click the Run button in the Design tab
• Click Run Macro on the Database Tools Ribbon, click the list arrow and select the macro, and then click OK
• In the Navigation Pane, click Macros, right-click the macro name, and then click Run
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 18
XPXPMacro Design Toolbar
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 19
XPXPEditing a Macro• Use Macro window• Rearrange order of actions
– Drag action line to new position• Insert new action between two existing actions
– Right-click row – Click insert rows
• Delete action– Right-click row – Click delete rows
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 20
XPXPSetting Startup Options• Startup options
– Access performs when database opens• Use Startup dialog box• Bypass Startup Options
– Press and hold down shift key when opening database
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 21
XPXPSpecifying Startup Options in the Startup Dialog Box• Options
– Display form– Display navigation pane– Allow full menus
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 22
XPXPStartup Dialog Box
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 23
XPXPCreating a Splash Screen• Splash screen
– Displays when database opens– Contains
• Logo• Text
• Can only display splash screen or switchboard automatically
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 24
XPXPCreating a Splash Screen (continued)• AutoExec
– Special macro– Runs when database opens– Before startup dialog box options– Can use to display splash screen
• Create splash screen form
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 25
XPXPCreating an AutoExec Macro• Create macro that contains actions Access should
perform when database opened• Save macro using name AutoExec
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 26
XPXPLevel 1 Summary• Switchboard
– Special kind of form – Appears when database opened
• Macro– Use to automate repeated actions
• Startup options dialog box• Splash screen
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 27
XPXPLevel 2 Objectives:Creating Advanced Macros• Consolidate automated tasks by creating a macro
group• Create buttons that can perform a series of
actions• Troubleshoot macros
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 28
XPXPUnderstanding Macro Groups• Macro group
– Two or more macros placed within same macro file– Consolidate related macros – Manage large numbers of macros– Only group name displayed as macro object in
database window
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 29
XPXPCreating a Macro Group• Logical to organize similar macros within group
– To create Click the Create tab and then click the Macro button– Click the Macro Names button on the Design tab. Access adds
the Macro Name column to the left of the Action column– In the Macro Name column, enter the name of a macro– In the Action column, enter the corresponding actions for the
macro– Enter comments as needed in the Comment column, and set
arguments as needed in the Action Arguments pane– Save the macro group, using the “mcr” prefix in the object
name
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 30
XPXPRunning the Macros in a Macro Group• Run particular macro within group
– Click the Run Macro button in the Macro group on the Database Tools Ribbon
– Select the list arrow to choose the macro name– Click the macro name– Click OK
• Switchboard Manager and many property sheets– Specify name of macro to run
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 31
XPXPRunning the Macros in a Macro Group• Run macro object
– Enter or select name– Run only one macro in macro group
• Use special notation mcrGroupName.mcrMacroName
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 32
XPXPCreating an AutoKeys Macro Group• AutoKeys macro group
– Assign macro action or set of actions to key or key combination
– Macro name column• Type key or key combination • Use special syntax
• SendKeys action – Sends keystrokes to Access or to another active
program
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 33
XPXPExamples of Key Combinations for the Autokeys Macro Group
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 34
XPXPModifying the Switchboard to Run Macros• Modify switchboard to include
– Macro group listing macros that open queries
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 35
XPXPSpecifying Conditions and Events for Macros• Specify conditions for performing macro actions • Assign macro to control• Event-driven programming
– Running macro when user interacts with object• Event
– State condition or occurrence that Access recognizes• Event property
– Specifies how object responds when event occurs– Set to macro name to run macro when even occurs
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 36
XPXPAssigning a Macro to a Control• Create macro or macro group that performs
series of actions• Assign macro to control
– Add command button to form by opening form in Design view
– Attach macro with desired actions to command button
• Using On Click property
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 37
XPXPSpecifying Conditions in a Macro• Where condition argument• Refer to controls on form that are not currently
active– Use control’s complete name– Format
• [Forms]![formName]![controlName]
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 38
XPXPTroubleshooting Macros• Macros containing arguments
– Especially error prone• Error types
– Syntax error– Logic error– Run-time error
• Troubleshooting tools– Single step through execution of macro, using debug
window– Print macro code
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 39
XPXPPrinting Macros• Open Documenter dialog box
– Select macro– Click options button to select details of macro to print
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 40
XPXPSingle Stepping a Macro• Runs macro one action at time
– Pausing between actions• Make sure actions appear in correct order and
with correct arguments• Use Single Step button
– Macro Single Step dialog box– Buttons
• Step• Halt• Continue
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 41
XPXPUsing Breakpoints and the Debug Window• Breakpoint
– Code inserted in macro– Signals where to stop macro
• Examine values of actions controls, and arguments macro using in debug window
• Debug window – Shows macro as it appears in VBA code– Examine details of macro code in debug window
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 42
XPXPUsing Breakpoints and the Debug Window (continued)• Immediate pane
– Displays current value of controls and arguments– Display using ? operator
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 43
XPXPLevel 2 Summary• Macro group• Conditions for macros• Assign macro to control• Debug macros
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 44
XPXPLevel 3 Objectives:Macro Conditions• Display message boxes under specified
conditions• Run a macro when a form opens or a report
prints• Use a macro to validate data
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 45
XPXPExploring Macro Conditions• Macro conditions
– Logical expressions that result in true or false answer– Depending on outcome
• Macro can perform one set of actions or another
• Enter macro conditions – Open condition column in Macro window
• By clicking Conditions button
• If condition true – Access performs corresponding action in action
column
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 46
XPXPExploring Macro Conditions• Perform more than one action if condition true
– Enter conditional statement in condition column – Select actions in action column in order – Enter ellipsis (…) for each subsequent action
associated with condition in condition column
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 47
XPXPExploring Macro Conditions (continued)• SetValue action
– Change property of object by disabling, updating, hiding, or displaying control
– Arguments • Item• Expression
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 48
XPXPAssigning a Macro to an Object Event• Assign macro to object
– By specifying macro name in event property of object• Event categories
– Report– Section
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 49
XPXPReport Events
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 50
XPXPReport Section Events
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 51
XPXPUsing Message Boxes with Forms• Form events can also trigger macros• Use to
– Validate data – Set values – Navigate between forms– Filter, find, and print records
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 52
XPXPForm Events
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 53
XPXPUsing Message Boxes with Forms• MsgBox command
– Opens message box – Displays warning or informational message– MsgBox ("message" sum of button and icon values "title")
– Returns value depending on which button clicked– Create condition that checks to see which button
users click
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 54
XPXPUsing Message Boxes with Reports• When conditions in macro references name of
control from source – Source must be open when condition tested– Can hide window while macro tests condition
• MsgBox action– Opens message box and displays warning or
informational message– Similar to using MsgBox command
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 55
XPXPUsing Message Boxes with Reports (continued)• MsgBox action arguments
– Message – Beep – Type– Title
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 56
XPXPValidating Data with Macros• Validation field properties have limits• Validate data using more than one rule and more
than one validation message– Create macro
• Domain aggregate functions– Calculate statistics for set of records (recordset) or
domain, from table or query• Dcount function
– Dcount(expression, table or query, condition)
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 57
XPXPCommon Domain Aggregate Functions
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 58
XPXPLevel 3 Summary• Conditional expressions in macros• Assign macros to events• Message boxes
– Msgbox command– Msgbox action
• Validate data with macros
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 59
XPXPChapter Summary• Switchboard
– Provides main menu for Access application• Macro
– Automates repetitive tasks– Can use conditional expressions– Associate with command buttons or other controls
• Debugger– Find errors in macros by stepping though each
statement
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach 60