INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.

Slides:



Advertisements
Similar presentations
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
Advertisements

Chapter 1 Getting Started with Access Databases. Objectives Identify Good Database Design Create a Table and Define Fields in a New Blank Database Change.
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. by Mary Anne Poatsy, Keith Mulbery, Lynn Hogan, Amy Rutledge, Cyndi Krebs, Eric.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
With Microsoft Excel 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Excel 2010.
With Microsoft Access 2010© 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access.
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. by Mary Anne Poatsy, Keith Mulbery, Lynn Hogan, Amy Rutledge, Eric Cameron, Cyndi.
By Mary Anne Poatsy, Keith Mulbery, Eric Cameron, Jason Davidson, Rebecca Lawson, Linda Lau, Jerri Williams Chapter 7 Advanced Forms and Reports 1 Copyright.
INSERT BOOK COVER 1Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Access 2010 by Robert Grauer, Keith Mast,
GO! with Microsoft® Excel 2010
With Microsoft ® Excel 2010© 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Excel.
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. Chapter 12 Templates, Styles, and Macros by Mary Anne Poatsy, Keith Mulbery, Eric.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
With Microsoft® Access 2010© 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access.
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
With Microsoft Access 2010© 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access.
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.
With Microsoft Access 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access.
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. 1 by Mary Anne Poatsy, Keith Mulbery, Lynn Hogan, Amy Rutledge, Cyndi Krebs, Eric.
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. by Mary Anne Poatsy, Keith Mulbery, Eric Cameron, Jason Davidson, Rebecca Lawson,
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. by Mary Anne Poatsy, Keith Mulbery, Lynn Hogan, Amy Rutledge, Cyndi Krebs, Eric.
Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall.
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall.1 Exploring Microsoft Office Excel Copyright © 2008 Prentice-Hall. All rights.
With Windows 7 Comprehensive© 2012 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Windows 7 Comprehensive.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 1 Committed to Shaping the Next Generation of IT Experts. Chapter 4: Create, Edit, and Perform.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
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.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith.
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall. 1 1 Copyright © 2008 Prentice-Hall. All rights reserved. Committed to Shaping the.
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall 1 Committed to Shaping the Next Generation of IT Experts. Chapter 1: Finding Your.
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. by Mary Anne Poatsy, Keith Mulbery, Lynn Hogan, Amy Rutledge, Cyndi Krebs, Eric.
Chapter 11: Introduction to the Visual Basic Environment Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. Access Module 1 Workshop 1 The Four Main Database Objects Series Editor Amy Kinser.
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. Access Module 2 Workshop 4 Accessing, Maintaining, and Presenting Information Series.
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Access 2010 by Robert Grauer, Keith Mast, Mary Anne Poatsy Chapter.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Access 2010 by Robert Grauer, Keith Mast,
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith Mast, and Mary Anne.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 11 Committed to Shaping the Next Generation of IT Experts. Chapter 10 Customizing a Database.
Committed to Shaping the Next Generation of IT Experts. Exploring Microsoft Office Word 2007 Chapter 4: Share, Compare and Document Robert Grauer, Keith.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith.
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. by Mary Anne Poatsy, Keith Mulbery, Lynn Hogan, Amy Rutledge, Cyndi Krebs, Eric.
GO! with Office 2013 Volume 1 By: Shelley Gaskin, Alicia Vargas, and Carolyn McLellan Access Chapter 3 Forms, Filters, and Reports.
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. 1 by Mary Anne Poatsy, Keith Mulbery, Lynn Hogan, Amy Rutledge, Cyndi Krebs, Eric.
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith Mulbery, and Mary.
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall 11 Committed to Shaping the Next Generation of IT Experts. Chapter 3: Customize, Analyze,
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. by Mary Anne Poatsy, Keith Mulbery, Lynn Hogan, Amy Rutledge, Cyndi Krebs, Eric.
GO! with Office 2013 Volume 1 By: Shelley Gaskin, Alicia Vargas, and Carolyn McLellan Word Chapter 1 Creating Documents with Microsoft Word 2013.
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith Mulbery, and Mary.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
1 Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith Mulbery, and Mary Anne Poatsy Chapter 4 Excel Datasets and Tables Copyright © 2011 Pearson.
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. by Mary Anne Poatsy, Keith Mulbery, Eric Cameron, Jason Davidson, Rebecca Lawson,
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall 1 1 Committed to Shaping the Next Generation of IT Experts. Chapter 4: Create, Edit,
Skills for Success with Microsoft Office 2013 Volume 1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. by Kris Townsend, Catherine.
With Microsoft Excel 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Excel 2010.
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith Mast, and Mary Anne.
By Mary Anne Poatsy, Keith Mulbery, Eric Cameron, Jason Davidson, Rebecca Lawson, Linda Lau, Jerri Williams Chapter 6 Action and Specialized Queries 1.
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith Mast, and Mary Anne.
Committed to Shaping the Next Generation of IT Experts. Exploring Microsoft Office Word 2007 Chapter 1: What Will Word Processing Do For Me? Robert Grauer,
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office 2010 Fundamentals by Robert Grauer, Lynn Hogan, and Mary.
Copyright © 2009 Pearson Education, Inc. Publishing as Prentice Hall. 1 Skills for Success with Microsoft ® Office 2007 PowerPoint Lecture to Accompany.
Exploring Microsoft Office Access 2007
GO! with Microsoft Office 2016
Microsoft Access Illustrated
Exploring Microsoft® Access® 2016 Series Editor Mary Anne Poatsy
exploring Microsoft Office 2013 Plus
Excel Datasets and Tables
Presentation transcript:

INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by Robert Grauer, Keith Mulbery, Keith Mast, and Mary Anne Poatsy Chapter 3 Customizing Access With VBA

Objectives Write code in modules Write code for forms and reports Work with objects Create functions Use DAO to access data Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 2

Objectives Use ADO to access data Add VBA to forms Add VBA to reports Handle errors in VBA Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 3

Introduction to VBA and Access Within Access, you can create standard VBA modules that will run whenever you execute them. Create form and report modules that contain procedures that run whenever an event occurs. Create functions using the same VBA programming code you use for other modules. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 4

Writing Code in Modules A standard module stores procedures that are available to any event in the application. Class modules are advanced programs that are used to create custom objects. An Access object module stores procedures that are available to a specific form or report. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 5

Example Class Module Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 6 Click Insert – Class Module to add a new class module Expand and collapse as needed A new class module appears Type Public Sub, and then the procedure name The editor adds End Sub automatically Click Insert – Class Module to add a new class module Expand and collapse as needed Type Public Sub, and then the procedure name

Writing Code for Forms and Reports Open the object in Design view, and then click Property Sheet in the Tools group. On the Event Tab, select an event, and then click Build on the right side of the property cell. The Choose Builder dialog box appears with three options—Macro Builder, Expression Builder, and Code Builder on the Event tab. Select the Code Builder, and then click OK to open the VB Editor. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 7

Open the Code Builder Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 8 Property Sheet Form in Design view Select Code Builder, and then click OK to open the VB Editor Click the Build button to create VBA code for an event

Working with Objects VBA works with objects in the various object libraries. Use objects in the object libraries with their resources to accomplish programming tasks. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 9

Understanding Properties A property is a physical attribute of an object. Some properties include: – Size – Type of border – Back color Properties can be changed using VBA code. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 10

Understanding Methods A method is an action that is performed by an object. Example of Access methods: – Open – Close – Refresh – Go to new record – Filter Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 11

Understanding Events Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 12 Form data is being edited User can “undo” the changes by clicking No Advance to the next record to trigger a save

Creating Functions Steps to create a custom function include: – Open a module. – Type a statement into the Code window with the following format example: Function NameOfFunction(Argument as data type) As Data Type End function – Type the VBA statements to be executed. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 13

Working with Recordsets A recordset is a set of records in memory selected from a table or query. – Similar to a table or a query The same data source would be represented in all three cases. Manipulate recordsets using DAO (Data Access Objects) and ADO (ActiveX Data Objects). Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 14

Using DAO to Access Data To create a recordset in DAO: – First explicitly connect to a database, usually the database that is currently opened. – Create a recordset object based on a table, a query, or an SQL statement. – Then loop through the records in the recordset to count records, find a specific record, or modify a field data. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 15

Find Records in a Recordset Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 16 Procedure finds employees from the state of CO

Update Records in a Recordset Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 17 Procedure finds employees from State = CO, and changes billrate to $170

Delete Records in a Recordset Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 18 Procedure finds employees that are inactive and deletes record from the table

Insert Records in a Recordset Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 19 Procedure adds a new record using the AddNew method

Using ADO to Access Data ADO (ActiveX Data Objects) was designed to connect to a wide range of external data sources. ADO was designed to connect to relational databases, ISAM data sources, and disk files. ADO uses the CurrentProject.Connection argument. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 20

Link to the ADO Object Library Click Tools on the menu bar. Select References in the dialog box. Click OK. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 21

Adding VBA to Forms Almost all of the VBA code added to a form is triggered by an event. Common events include: – On Open – On Click – On Enter – On Exit – Before Update – After Update Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 22

Add an Event Procedure to a Form To notify the user when a record is being changed: – Add a hidden message to the top of the form. – Make the message visible while it is being changed. – After the record is saved, the message could be hidden again. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 23

Add an Event Procedure (Cont.) Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 24 Label is hidden when record is saved Label is visible when record becomes dirty

Validate Data Before an Update Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 25 Validation of hire date Validation of exemptions Validation of bill rate Validation of salary Validation of 401k

Adding VBA to Reports Modify the layout of a report before the report prints. Reports that contain multiple sections may have one or more sections that need to be hidden, even though they contain data. Use VBA to test if a section matches certain criteria and then tell Access how to respond when condition is met. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 26

Example VBA in Reports Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 27 Region 500 is hidden Other regions are visible

Understanding Report Events Report events are similar to form events. Once a report runs, most of the events happen in succession with very little intervention by the user. The On Open, On Load, and On Activate events fire in succession. A user cannot change the order of events; the order is predefined by Access. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 28

Set the Record Source Based on Form Properties A report is more versatile if the user sets one or more parameters before it is open. Use a blank form that contains no record source to set the parameters for the report. The form would contain all the parameters that affect the report and a method for selecting the parameters. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 29

Setting the Record Source Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 30 User selects a region Preview the report from the button

The On Open Event Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 31 Combo box reference on the parameter form SQL statement becomes the record source

Handling Errors in VBA There are two types of errors in VBA: – Handled errors: Procedures with a built-in error handling routine Errors can be intercepted and VBA can display a friendly message to the user, with options that make sense. – Unhandled errors: Otherwise the code will stop working and the user will be presented with a confusing list of options Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 32

Add an Error Handling Routine To produce a more user-friendly error message, an error routine can be added to the code. The On Error statement directs the code to skip to a particular line in the procedure (usually at the bottom) where the appropriate message is displayed and any corrective action can be taken. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 33

Example Error Handling Routine Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 34 On Error statement added Exit routine added Error routine added

Test and Debug Code The VBA debugger enables the programmer to suspend the execution of VBA code so the following debug tasks can be done: – Check the value of a variable in its current state – Enter VBA code in the Immediate window to view the results – Execute each line of code one at a time – Continue execution of the code – Halt execution of the code Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 35

Setting a Breakpoint in the Code Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 36 Breakpoint was set Current line shown in yellow

Summary Explored the use of VBA in an Access database Enhanced the functionality of an Access Database using VBA events Coded events for an Access form or report Handled errors in an effective and efficient manner Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 37

Questions Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 38

Copyright Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 39 All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America.