Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exploring Office 2003 - Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)

Similar presentations


Presentation on theme: "Exploring Office 2003 - Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)"— Presentation transcript:

1 Exploring Office 2003 - Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)

2 Exploring Office 2003 - Grauer and Barber 2 Objectives Describe the relationship of VBA to Microsoft Office Create an event procedure Describe the VBA editor Distinguish between Procedure and Full Module view Create a combo box to locate a record on a form

3 Exploring Office 2003 - Grauer and Barber 3 Objectives (continued) Describe MsgBox parameters Facilitate data entry through keyboard shortcuts Create an event procedure that substitutes an application-specific message for a standard Access error message Describe several types of data validation

4 Exploring Office 2003 - Grauer and Barber 4 Case Study: Back to Natalie’s Students will modify a database to include: Modify Add Records procedure to move focus to the appropriate field on the form Create a Find Record combo box and data entry shortcuts Display a message reminding the user to backup the database upon close

5 Exploring Office 2003 - Grauer and Barber 5 Introduction to VBA VBA is a subset of Visual Basic for all Microsoft Office applications VBA is event driven  An event is any action recognized by Access Procedures are sets of VBA statements that respond to specific events

6 Exploring Office 2003 - Grauer and Barber 6 A Better Student Form A form used throughout this chapter that: Improves on data validation Facilitates data entry, including creating keyboard shortcuts Employs error trapping Enhances communication with the user by displaying dialog boxes

7 Exploring Office 2003 - Grauer and Barber 7 Procedures Visual Basic code is developed in units called procedures Event procedures: run automatically in response to events General procedures: called explicitly from within another procedure

8 Exploring Office 2003 - Grauer and Barber 8 Modules All procedures are stored in modules; one or more procedures per module Every form in Access has its own module which contains procedures for that form Private procedure: accessible only from within the module in which it is contained Public procedure: accessible from anywhere Module window: where procedures in a module are displayed and edited

9 Exploring Office 2003 - Grauer and Barber 9 Module Window Name of procedure (Form_Close) MsgBox statement End of procedure Name of second procedure (Form_Current)

10 Exploring Office 2003 - Grauer and Barber 10 MsgBox function MsgBox takes three arguments (parameters):  Prompt: Specifies the message text  Buttons: Type of command buttons and icon  Title: Text that appears in title bar of the message box

11 Exploring Office 2003 - Grauer and Barber 11 Hands-on Exercise 1 Title of Exercise: Create a Combo Box and Associated VBA Procedure Objective: to create a combo box to locate a record; to create a VBA procedure to synchronize the combo box with the current record  Input file: Introduction to VBA  Output file: Introduction to VBA (modified)

12 Exploring Office 2003 - Grauer and Barber 12 Create an Event Procedure Right click on Form Selector box and choose properties Select Event tab Click On Current Select Code Builder

13 Exploring Office 2003 - Grauer and Barber 13 On Current Event Procedure Procedure header Name of Combo Box from step 3 Complete Word will provide completion options Procedure View button

14 Exploring Office 2003 - Grauer and Barber 14 Facilitating Data Entry Use the Default property to specify default values for certain fields Use VBA to create keyboard shortcuts A KeyDown event procedure can implement keyboard shortcuts

15 Exploring Office 2003 - Grauer and Barber 15 Hands-on Exercise 2 Title of Exercise: Facilitating Data Entry Objective: Create keyboard shortcuts to facilitate data entry  Input file: Introduction to VBA (after Exercise 1)  Output file: Introduction to VBA (modified)

16 Exploring Office 2003 - Grauer and Barber 16 KeyDown Event Procedure Click drop-down arrow And select Form Key words appear in blue Click drop-down arrow And select KeyDown Procedure View button

17 Exploring Office 2003 - Grauer and Barber 17 ShortCut Command Button Select the command Button Properties All tab Change Name to ShortCutButton Change Caption to &ShortCuts Command button tool Click and drag to create command button

18 Exploring Office 2003 - Grauer and Barber 18 Error Trapping Produce more user-friendly error message Find error number using the Immediate window Case statements test incoming variable value and produce the appropriate statement Once error is detected, call MsgBox statement to display your user-friendly error message Else will execute if all Case statements fail

19 Exploring Office 2003 - Grauer and Barber 19 Error Event Procedure Error number Case 1 Print MsgBox Else statement

20 Exploring Office 2003 - Grauer and Barber 20 Hands-on Exercise 3 Title of Exercise: Error Trapping Objective: to create an event procedure that substitutes application-specific messages for the standard Access error messages  Input file: Introduction to VBA (after Exercise 2)  Output file: Introduction to VBA (modified)

21 Exploring Office 2003 - Grauer and Barber 21 Data Validation Invalid data corrupts validity of information  Data validation is therefore crucial Use VBA to extend data validation capabilities within Access Use a nested IF statement to remind users to leave no fields empty

22 Exploring Office 2003 - Grauer and Barber 22 MsgBox Function vs. MsgBox Statement MsgBox function: displays a prompt to the user, then returns a value  Requires parentheses around arguments MsgBox statement: simply displays a message  Does not use parentheses

23 Exploring Office 2003 - Grauer and Barber 23 Hands-on Exercise 4 Title of Exercise: Data Validation Objective: to use Field and Table properties to implement different types of data validation  Input file: Introduction to VBA (after Exercise 3)  Output file: Introduction to VBA (modified)

24 Exploring Office 2003 - Grauer and Barber 24 Summary VBA is a subset of Visual Basic VBA is accessible by all Microsoft Office applications All VBA procedures are stored in modules Every form in Access has its own module that contains the event procedures for that form

25 Exploring Office 2003 - Grauer and Barber 25 Summary (continued) All procedures are either public or private:  Private: accessible only within its module  Public: accessible from anywhere Event procedures were created to illustrate how VBA is used to improve an application MsgBox function has three arguments:  Message  Intrinsic constant  Title bar message

26 Exploring Office 2003 - Grauer and Barber 26 End-of-chapter Exercises Multiple Choice Practice With Access  Exercise 1 – MsgBox Examples  Exercise 2 – Expanded Student Form  Exercise 3 – Return to Soccer  Exercise 4 – Enhancing a Report  Exercise 5 – Acme Computers

27 Exploring Office 2003 - Grauer and Barber 27 End-of-chapter Exercises (continued) On Your Own  Expanding Soccer  The VBA Primer  Debugging  Help for VBA

28 Exploring Office 2003 - Grauer and Barber 28 Questions?


Download ppt "Exploring Office 2003 - Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)"

Similar presentations


Ads by Google