Exploring Microsoft Office Access 2007 Chapter 10 Customizing a Database with Macros and Visual Basic for Applications Robert Grauer, Keith Mulbery, Maurie Wigman Lockley Committed to Shaping the Next Generation of IT Experts.1 Copyright © 2008 Pearson Prentice Hall. All rights reserved. Objectives Understand the purpose of macros Create embedded macros using Wizards Create macros using the Macro Builder Assign macros to events Use SQL conditions to control macro actions Copyright © 2008 Pearson Prentice Hall. All rights reserved. 2 Objectives Understand Visual Basic for Applications (VBA) Use the Visual Basic Editor Create procedures Declare and use variables Manage data using SQL Copyright © 2008 Pearson Prentice Hall. All rights reserved. 3 Customization in Access Customization needed when additional functionality required Access provides two methods of Customization Visual Basic for Applications User creates code using VBA Macros Automates tasks Copyright © 2008 Pearson Prentice Hall. All rights reserved. 4 Macros Two types of Macros in Access Embedded – belongs to a single object or control Standalone – used with more than one control Control – object in Access (textbox, list box) Macro Group Group of macros stored in a single macro object Copyright © 2008 Pearson Prentice Hall. All rights reserved. 5 Creating an Embedded Macro Button Click Design tab Click Button Command Button Wizard Controls Group Command Wizard begins Copyright © 2008 Pearson Prentice Hall. All rights reserved. 6 Creating an Embedded Macro Choose a category Choose an action Choose object that the action will be performed upon Choose the appropriate category Choose action that you want to perform Click Next Select the object you wish to use Click Next Copyright © 2008 Pearson Prentice Hall. All rights reserved. 7 Creating an Embedded Macro Specify how the button should work open an object Specify Text or graphic for the button Specify action when form is opened Screen varies by object and action Specify text or graphic Click Next Copyright © 2008 Pearson Prentice Hall. All rights reserved. 8 Creating an Embedded Macro Completed button Finish Provide a name for the button Click Finish Copyright © 2008 Pearson Prentice Hall. All rights reserved. 9 Creating Standalone Macros using the Macro Builder Macro Macro Builder Click the Create tab Click Macro Other group Macro Builder launches Copyright © 2008 Pearson Prentice Hall. All rights reserved. 10 Creating Standalone Macros using the Macro builder 2. Argument appears here Select Action 1. Provide Argument Select Action Provide Arguments when necessary Value that provides information for action Run or Close and Save macro Copyright © 2008 Pearson Prentice Hall. All rights reserved. 11 Assigning Events Property Sheet Events Choose object that the event is assigned to Event Action triggered by user or system Assign an Event Right-click, select properties Choose property-Event tab, Control list Copyright © 2008 Pearson Prentice Hall. All rights reserved. 12 Running a Macro Design view Form, Report or Table view Click the Run button Button in Form View Click button or perform assigned Navigation Pane event Navigation Pane Double-click Macro Copyright © 2008 Pearson Prentice Hall. All rights reserved. 13 Structured Query Language SQL statement SELECT fname FROM customer; Show (SELECT) me the records in the first name (fname) field that are in (FROM) the customer table SQL Defines and process database queries Industry standard query language Microsoft Access SQL Microsoft version of SQL Copyright © 2008 Pearson Prentice Hall. All rights reserved. 14 Database Analysis Tools SELECT fname FROM customer WHERE lname=“Smith” ORDER BY fname Returns records in the fname field only where the lname field is equal to Smith. Records are sorted in ascending order by first name Clauses Added to statements to restrict/specify records WHERE clause Specifies which records to return ORDER BY clause Specifies sort order Copyright © 2008 Pearson Prentice Hall. All rights reserved. 15 Visual Basic for Applications Programming language Allows you to create/customize applications It is a host application Resides in Office application Visual Basic Editor Workspace for writing/editing VBA procedures Copyright © 2008 Pearson Prentice Hall. All rights reserved. 16 Launching the Visual Basic Editor VBA Editor Three ways to launch editor Macro group, Database Tools tab Event Property of an object/control Press Alt+F11 Copyright © 2008 Pearson Prentice Hall. All rights reserved. 17 VBA Terminology Comment – explains the code’s purpose Keyword – recognized as part of a language Syntax – rules for entering statements Standard Module – stores procedures used by events Class module – contains procedures for specific objects Option Explicit – helps eliminate common errors Copyright © 2008 Pearson Prentice Hall. All rights reserved. 18 VBA Terminology (cont.) Event – action that triggers program instructions Sub procedure – performs an action Property procedure – creates or manipulates custom properties Function procedure – performs action, returns value Public Procedure – available to all objects Private Procedure – available to a specific object Copyright © 2008 Pearson Prentice Hall. All rights reserved. 19 Creating Procedures Click Insert, Module New Module Select Insert on menu bar Select Module New module opens Copyright © 2008 Pearson Prentice Hall. All rights reserved. 20 Creating Sub Procedures Sub Procedure Enter keyword Sub Enter the name of the procedure Enter a pair of parenthesis Enter End Sub Copyright © 2008 Pearson Prentice Hall. All rights reserved. 21 Decision Structures If Cost <=75 then purchase = 2 Performs logical test to compare statements Uses relations operators such as: = , < > equal to, not equal to <, > less than, greater than >= Greater than or equal to <= Less than or equal to Copyright © 2008 Pearson Prentice Hall. All rights reserved. 22 Variables Named storage location in memory A variable’s scope limits accessibility Three different scopes Local – available to a specific procedure Global – available to any procedure Module-level – available to any procedure within module Copyright © 2008 Pearson Prentice Hall. All rights reserved. 23 Naming and Dimensioning Variable name Data type Dim strName as string Dim intCost as int Dim (dimension) variables before using Use appropriate data type Name variables logically Use a meaningful name Variable12 not meaningful intQuantity meaningful Copyright © 2008 Pearson Prentice Hall. All rights reserved. 24 Assign Values to Variables fname John lname Doe city Valdosta Memory Storage location named fname John = value Written fname = “John” Storage location named lname Doe = value Written lname= “Doe” Copyright © 2008 Pearson Prentice Hall. All rights reserved. 25 Call a Procedure Call to CloseForm Procedure Call keyword or procedure name Can be used to activate another procedure Copyright © 2008 Pearson Prentice Hall. All rights reserved. 26 Passing and “Catching” Arguments Argument passed to CloseForm procedure Passed to and “caught” here. Notice the variable names you are passing from and to do not have to match Copyright © 2008 Pearson Prentice Hall. All rights reserved. 27 Copyright © 2008 Pearson Prentice Hall. All rights reserved. 28
© Copyright 2026 Paperzz