11 Chapter 10 Customizing a Database with Macros and Visual Basic for Applications Exploring Microsoft Office Access 2007.

Slides:



Advertisements
Similar presentations
INTRODUCTORY MICROSOFT ACCESS Lesson 1 – Access Basics
Advertisements

Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Excel Project 7 Using Macros and Visual Basic for Applications (VBA) with Excel.
Using Macros and Visual Basic for Applications (VBA) with Excel
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Some Introductory Programming 1. Structured Query Language (SQL) - used for queries. - a standard database product. 2. Visual Basic for Applications -
Review Applications of Database Systems Applications of Database Systems Theory Practice.
VBA Modules, Functions, Variables, and Constants
Using Visual Basic 6.0 to Create Web-Based Database Applications
Microsoft Excel 2003 Illustrated Complete with Excel Programming.
Automating Tasks With Macros. 2 Design a switchboard and dialog box for a graphical user interface Database developers interact directly with Access.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Using the Visual Basic Editor Visual Basic for Applications 1.
Introduction To Form Builder
By Mary Anne Poatsy, Keith Mulbery, Eric Cameron, Jason Davidson, Rebecca Lawson, Linda Lau, Jerri Williams Chapter 10 Using Macros and SQL in Access 1.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
Access Tutorial 10 Automating Tasks with Macros
Chapter 9 Macros, Navigation Forms, PivotTables, and PivotCharts
Chapter 2 Querying a Database
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Pasewark & Pasewark Microsoft Office 2003: Introductory 1 INTRODUCTORY MICROSOFT ACCESS Lesson 1 – Access Basics.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Chapter 2 Querying a Database MICROSOFT ACCESS 2010.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
XP Chapter 7 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Enhancing User Interaction Through Programming Chapter.
Automating Tasks with Visual Basic. Introduction  When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Enhancing User Interaction Through Programming
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith.
Using Visual Basic 6.0 to Create Web-Based Database Applications
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 11 Committed to Shaping the Next Generation of IT Experts. Chapter 10 Customizing a Database.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
OCC Network Drives  H:\  P:\ 
Automating Database Processing
Key Applications Module Lesson 21 — Access Essentials
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Access Project 7 Advanced Report and Form Techniques.
Exploring Microsoft Access Chapter 8 Creating More Powerful Applications: Introduction to VBA.
Microsoft Access 2013 ®® Tutorial 10 Automating Tasks with Macros.
Microsoft Access 2010 Chapter 8 Advanced Form Techniques.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
Chapter 9 Macros And Visual Basic For Applications.
® Microsoft Access 2010 Tutorial 10 Automating Tasks with Macros.
Access Lesson 11 Creating and Running Macros Microsoft Office 2010 Advanced Cable / Morrison 1.
I Power Higher Computing Software Development Development Languages and Environments.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 10 1 Microsoft Office Access 2003 Tutorial 10 – Automating Tasks With Macros.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Chapter 7 Creating Templates, Importing Data, and Working with SmartArt, Images, and Screen Shots Microsoft Excel 2013.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
Chapter 11 Enhancing an Online Form and Using Macros Microsoft Word 2013.
Microsoft Access 2003 Illustrated Complete
Microsoft Access Illustrated
Microsoft Office Illustrated
Microsoft Excel 2003 Illustrated Complete
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Exploring Microsoft Office Access 2007
Review Applications of Database Systems
CIS16 Application Programming with Visual Basic
Exploring Microsoft® Access® 2016 Series Editor Mary Anne Poatsy
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

11 Chapter 10 Customizing a Database with Macros and Visual Basic for Applications Exploring Microsoft Office Access 2007

2 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

3 Objectives Understand Visual Basic for Applications (VBA) Use the Visual Basic Editor Create procedures Declare and use variables Manage data using SQL

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

5 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

Creating an Embedded Macro Click Design tab Click Button –Controls Group Command Wizard begins Command Button Wizard Button

Creating an Embedded Macro Choose the appropriate category Choose action that you want to perform Click Next Select the object you wish to use Click Next Choose a categoryChoose an actionChoose object that the action will be performed upon

Creating an Embedded Macro Specify action when form is opened –Screen varies by object and action Specify text or graphic Click Next Specify Text or graphic for the buttonSpecify how the button should work open an object

9 Creating an Embedded Macro Provide a name for the button Click Finish Completed button Finish

10 Creating Standalone Macros using the Macro Builder Click the Create tab Click Macro –Other group –Macro Builder launches Macro Macro Builder

11 Creating Standalone Macros using the Macro builder Select Action Provide Arguments when necessary –Value that provides information for action Run or Close and Save macro 1. Provide Argument Select Action 2. Argument appears here

12 Assigning Events Event –Action triggered by user or system Assign an Event –Right-click, select properties –Choose property-Event tab, Control list Property Sheet Events Choose object that the event is assigned to

13 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 Navigation Pane

14 Structured Query Language SQL –Defines and process database queries –Industry standard query language Microsoft Access SQL –Microsoft version of SQL SELECT fname FROM customer; SQL statement Show (SELECT) me the records in the first name (fname) field that are in (FROM) the customer table

15 Database Analysis Tools Clauses –Added to statements to restrict/specify records WHERE clause –Specifies which records to return ORDER BY clause –Specifies sort order 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

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

17 Launching the Visual Basic Editor Three ways to launch editor –Macro group, Database Tools tab –Event Property of an object/control –Press Alt+F11 VBA Editor

18 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

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

20 Creating Procedures Select Insert on menu bar Select Module New module opens New Module Click Insert, Module

21 Creating Sub Procedures Enter keyword Sub Enter the name of the procedure Enter a pair of parenthesis Enter End Sub Sub Procedure

22 Decision Structures 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 If Cost <=75 then purchase = 2

23 Variables Named storage location in memory A variable’s scope limits accessibility Three different scopes –Local – available to a specific procedure –Module-level – available to any procedure within module –Global – available to any procedure

24 Naming and Dimensioning Dim (dimension) variables before using –Use appropriate data type Name variables logically Use a meaningful name –Variable12 not meaningful –intQuantity meaningful Dim strName as string Public intCost as int Data typeVariable name Local or module variable Global variable

25 Assign Values to Variables Storage location named fname –John = value –Written fname = “John” Storage location named lname –Doe = value –Written lname= “Doe” JohnDoeValdosta fnamelnamecity Memory

26 Call a Procedure Call keyword or procedure name –Can be used to activate another procedure Call to CloseForm Procedure

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

Private Sub cmdHistoryForm_Click() 'Display the form DoCmd.OpenForm("Employee"),,,, acFormEdit End Sub Private Sub cmdEmployeeForm_Click() 'Display the form DoCmd.OpenForm("History"),,,, acReadOnly End Sub Private Sub cmdExit_Click() 'Exit the application intResponse = MsgBox("Do you want to exit the Compensation" & _ "application?", vbYesNo + vbCritical, "Exit Application?") If intResponse = vbYes Then Application.Quit acQuitPrompt End If End Sub

Private Sub cmdSummaryReport_Click() 'This procedure defines an SQL query as the record source 'for the "Retirement Summary" report; the SQL statement 'returns all records 'Assign the report name to the strObjectName variable strObjectName = "Retirement Summary" 'Declare a variable to store the SQL statement, define the 'SQL string strSQL = "SELECT Employee.LastName, Employee.FirstName, " & _ "Office.Region, Contribution.PayDate," & _ "Contribution.[401KEmployee], " & _ "Contribution.[401KMatch], Contribution.[401KTotal], " & _ "FROM (Office INNER JOIN Employee ON Office.[OfficeNumber] = " & _ "Employee.[OfficeLocation])" & _ "INNER JOIN Contribution ON Employee.[SSN] = Contribution.[SSN];"

'Open the Address List Report DoCmd.OpenReport strObjectName, acViewReport 'Assign the SQL query as the record source for the report Reports(strObjectName).RecordSource = strSQL 'clean up the public strSQL variable strSQL = "" End Sub

Private Sub cmdContribByRegion_Click() 'Assign the report name to the strObjectName variable strObjectName = "Retirement Summary" strSQL = "SELECT Employee.LastName, Employee.FirstName, " & _ "Office.Region, Contribution.PayDate," & _ "Contribution.[401KEmployee], " & _ "Contribution.[401KMatch], Contribution.[401KTotal], " & _ "FROM (Office INNER JOIN Employee ON Office.[OfficeNumber] = " & _ "Employee.[OfficeLocation])" & _ "INNER JOIN Contribution ON Employee.[SSN] = Contribution.[SSN]" & _ "WHERE (((Office.Region) = [Enter Region Name]));"

'Open the Address List Report DoCmd.OpenReport strObjectName, acViewReport 'Assign the SQL query as the record source for the report Reports(strObjectName).RecordSource = strSQL 'clean up the public strSQL variable strSQL = "" End Sub

Sub cmdClose_Click() strObjectName = "Employee" strShortName = "Employee Records" CloseForm strObjectName, strShortName End Sub

Sub cmdClose_Click() strObjectName = "Employee" strShortName = "Employee Records" CloseForm strObjectName, strShortName End Sub

Private Sub cmdClose_Click() strObjectName = "History" strShortName = "Contribution History" CloseForm strObjectName, strShortName End Sub

Private Sub cmdClose_Click() strObjectName = "History" strShortName = "Contribution History" CloseForm strObjectName, strShortName End Sub

Option Explicit 'Declare global variables Public strObjectName As String Public strShortName As String Public intResponse As Integer Public strSQL As String 'General procedure to close a form Sub CloseForm(ObjectName, ShortName) intResponse = MsgBox("Do you want to close the" &ShortName & _ "form?", vbYesNo + vbCritical, "Close Form?") If intResponse = vbYes Then DoCmd.Close acForm, ObjectName End If End Sub 'General Procedure to close a report Sub CloseReport(ObjectName, ShortName) intResponse = MsgBox("Do you want to close the" &ShortName & _ "report?", vbYesNo + vbCritical, "Close Report?") If intResponse = vbYes Then DoCmd.Close acReport, ObjectName End If End Sub