Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exploring Microsoft Office Access 2007

Similar presentations


Presentation on theme: "Exploring Microsoft Office Access 2007"— Presentation transcript:

1 Exploring Microsoft Office Access 2007
Chapter 10 Customizing a Database with Macros and Visual Basic for Applications This chapters focus is on customizing a database. Robert Grauer, Keith Mulbery, Maurie Wigman Lockley Committed to Shaping the Next Generation of IT Experts. Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1

2 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 The objectives for the chapter are: Understand the purpose of macros in Access databases; create embedded macros using Wizards; create macros using the Macro Builder; assign macros to events; and use SQL conditions to control macro actions. The objectives continue on the next slide. Copyright © 2008 Pearson Prentice Hall. All rights reserved.

3 Copyright © 2008 Pearson Prentice Hall. All rights reserved.
Objectives Understand Visual Basic for Applications (VBA) Use the Visual Basic Editor Create procedures Declare and use variables Manage data using SQL Additional Objectives for the chapter are: Understand Visual Basic for Applications (VBA); use the Visual Basic Editor; create procedures; declare and use variables; are manage data using SQL. Copyright © 2008 Pearson Prentice Hall. All rights reserved.

4 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 Customization in Access is needed when additional functionality beyond the normal forms, reports, tables, and queries is required. This customization can only be gained through programming. Access provides two methods of customization. Visual Basic for Applications allows the user to create code for Access. Macros are an easier method than writing code and are good when you need to automate a task. Copyright © 2008 Pearson Prentice Hall. All rights reserved.

5 Copyright © 2008 Pearson Prentice Hall. All rights reserved.
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 Macros add functionality to a database. They allow users to customize repetitive tasks and to group multiple tasks into one package. Macros can be thought of as a simplified programming language. They are recyclable – create once and run an infinite number of times. There are two types of Macros in Access. Embedded – belongs to a single object or control; and standalone – used with more than one control. Recall that a control is an object in Access such as a textbox or list box. You can also create macro groups, which is a group of macros stored in a single macro object. Copyright © 2008 Pearson Prentice Hall. All rights reserved.

6 Creating an Embedded Macro
Button Command Button Wizard To create an embedded macro, click the Design tab then click the button located on the Controls group, and the Command Button Wizard will begin. Click Design tab Click Button Controls Group Command Wizard begins Copyright © 2008 Pearson Prentice Hall. All rights reserved.

7 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 Follow these steps to create an embedded macro: 1. Choose the appropriate category; 2. Choose the action that you want to perform; 3. Click Next; 4. Select the object you wish to use; 5. Click Next. Clicking Finish will result in no customization of the buttons (i.e., adding picture or title). Copyright © 2008 Pearson Prentice Hall. All rights reserved.

8 Creating an Embedded Macro
Specify how the button should work open an object Specify Text or graphic for the button Next, specify the action to take when the form is opened. Keep in mind, that many of the screens in this wizard will vary according to the object you are working with. Example purposes for which a form can been used are in the Wizard. Then, specify pictures or text for the button. Click Next. 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.

9 Creating an Embedded Macro
Completed button In the last dialog box of the Wizard, you will provide a name for the button. You may then click Finish and test your button to see if it works appropriately. Finish Provide a name for the button Click Finish Copyright © 2008 Pearson Prentice Hall. All rights reserved.

10 Creating Standalone Macros using the Macro Builder
To create a macro using the Macro builder, click the Create tab and Macro in the Other group. The Macro builder will launch. Click the Create tab Click Macro Other group Macro Builder launches Copyright © 2008 Pearson Prentice Hall. All rights reserved.

11 Creating Standalone Macros using the Macro builder
2. Argument appears here Select Action 1. Provide Argument Select the appropriate action, such as opening a form or query. Then provide the argument for the action. The argument for opening a form would be the form name. You can provide the argument in the lower section of the Macro builder. After providing the argument, it will appear in the top pane in the Arguments section next to its related action. 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.

12 Copyright © 2008 Pearson Prentice Hall. All rights reserved.
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 An event is an action that is triggered by the user or system. To assign an event to a standalone macro, right-click the macro and select properties from the short-cut menu. Choose property and the Property Sheet will open. Click the Event tab. Choose the event you want to use to trigger the macro. The macro you are assigning the event to should automatically appear in the section to the right of the chosen action. However, if it does not, you may click the for down arrow and choose the macro you desire from the drop-down list. Copyright © 2008 Pearson Prentice Hall. All rights reserved.

13 Copyright © 2008 Pearson Prentice Hall. All rights reserved.
Running a Macro Design view Click the Run button Form, Report or Table view Click button or perform assigned event Navigation Pane Double-click Macro Button in Form View There are several different ways to run macros. You may click the Run button in Design view or double-click the macro in the navigation pane. Of course you can also click or perform the assigned action in Form, Report or Table view to execute the macro. Navigation Pane Copyright © 2008 Pearson Prentice Hall. All rights reserved.

14 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 Structured Query Language, or SQL defines and process database queries. SQL is the industry standard query language and Microsoft Access SQL is Microsoft's version of SQL. Copyright © 2008 Pearson Prentice Hall. All rights reserved.

15 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 Clauses can be added to select statement in order to restrict or further define query results. The WHERE clause specifies which records to return based on a condition – where this is equal to that or where this is true or that is false. The ORDER BY clause sorts records in either ascending or descending order and according to the field you specify. TO sort in descending order, add the keyword desc after the field name you are sorting by. Copyright © 2008 Pearson Prentice Hall. All rights reserved.

16 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 Visual Basic for Applications is a programming language that allows you to create/customize applications. It is a host application in that it resides in the specific Office application you are working with. The Visual Basic Editor is a text editor that provides workspace for writing/editing VBA procedures. Copyright © 2008 Pearson Prentice Hall. All rights reserved.

17 Launching the Visual Basic Editor
VBA Editor There are three ways to launch the Visual Basic Editor: From the Macro group on the Database Tools tab; From the Event Property of an object/control; or By pressing Alt+F11 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.

18 Copyright © 2008 Pearson Prentice Hall. All rights reserved.
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 Let’s review some VBA terminology: A Comment explains the code’s purpose; A Keyword is recognized as part of a language; Syntax is rules for entering statements; A Standard Module stores procedures used by events; A Class module contains procedures for specific objects; and Option Explicit helps eliminate common errors. Terminology continues on the next slide. Copyright © 2008 Pearson Prentice Hall. All rights reserved.

19 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 Additional VBA terminology includes: An Event which is an action that triggers program instructions; A Sub procedure that performs an action; A Property procedure which creates or manipulates custom properties; A Function procedure which performs an action and then returns a value; A Public Procedure that is available to all objects; and A Private Procedure that is only available to a specific object. Copyright © 2008 Pearson Prentice Hall. All rights reserved.

20 Copyright © 2008 Pearson Prentice Hall. All rights reserved.
Creating Procedures Click Insert, Module To create a procedure in VBA, select Insert form on the menu bar and click Module. The new module appears and is ready for you to type in your code. New Module Select Insert on menu bar Select Module New module opens Copyright © 2008 Pearson Prentice Hall. All rights reserved.

21 Creating Sub Procedures
To create a Sub Procedure, you enter keyword Sub, enter the name of the procedure, enter a pair of parenthesis, and then enter End Sub. 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.

22 Copyright © 2008 Pearson Prentice Hall. All rights reserved.
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 Decision structures use relational operators to compare values in the programming statement and make a decision based on that comparison. Copyright © 2008 Pearson Prentice Hall. All rights reserved.

23 Copyright © 2008 Pearson Prentice Hall. All rights reserved.
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 A variable is simply a named storage location in memory. All variable have scope – which limits accessibility to the variable. In VBA, you can use three different scopes: Local – available to a specific procedure; Global – available to any procedure; or Module-level – available to any procedure within a module. Copyright © 2008 Pearson Prentice Hall. All rights reserved.

24 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 Before using a variable, you must name and dimension the variable. Follow good practice when naming a variable and make sure the name reflect what the variable will be used for. Standard convention says to use an abbreviated form of the data type before the variable name. In other words a variable that will be an integer data type will have the letters int before the variable name. You use the Dim keyword to begin the dimension statement that allows you to specify both the name and the data type of the variable. Copyright © 2008 Pearson Prentice Hall. All rights reserved.

25 Assign Values to Variables
fname lname city John Doe Valdosta Memory Storage location named fname John = value Written fname = “John” Storage location named lname Doe = value Written lname= “Doe” In the example, the first storage location is named fname. A value has been assigned to that storage location. The value is John. In VBA we would assign the value to the variable in this manner: fname=“John”. Notice that character values go inside quotation marks. The second storage location is named lname. The value stored in that location is Doe. We would assign this value to the lname variable in this manner: lname = “Doe”. How would you write the assignment statement for the last variable? Copyright © 2008 Pearson Prentice Hall. All rights reserved.

26 Copyright © 2008 Pearson Prentice Hall. All rights reserved.
Call a Procedure Call to CloseForm Procedure Sometimes, you may want a procedure to become active while you are in another procedure. You can invoke, or call, a procedure by using the keyword Call and the procedure name, or by using only the procedure name. Call keyword or procedure name Can be used to activate another procedure Copyright © 2008 Pearson Prentice Hall. All rights reserved.

27 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 It may also be necessary to send the called procedure information (arguments). If this is so, the argument you want to send should be inserted into the parenthesis after the procedure name you are calling. The argument is then sent and “caught” inside the variable that is the argument for the procedure you are calling. Copyright © 2008 Pearson Prentice Hall. All rights reserved.

28 Copyright © 2008 Pearson Prentice Hall. All rights reserved.
Be sure to read the material in the text carefully, complete the exercises as directed by your instructor, and ask questions as they arise. Copyright © 2008 Pearson Prentice Hall. All rights reserved. 28


Download ppt "Exploring Microsoft Office Access 2007"

Similar presentations


Ads by Google