Download presentation
Presentation is loading. Please wait.
Published byRosemary Harrington Modified over 9 years ago
1
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
2
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
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
Find Records in a Recordset Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 16 Procedure finds employees from the state of CO
17
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
18
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
19
Insert Records in a Recordset Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 19 Procedure adds a new record using the AddNew method
20
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
21
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
22
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
23
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
24
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
25
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
26
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
27
Example VBA in Reports Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 27 Region 500 is hidden Other regions are visible
28
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
29
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
30
Setting the Record Source Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 30 User selects a region Preview the report from the button
31
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
32
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
33
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
34
Example Error Handling Routine Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 34 On Error statement added Exit routine added Error routine added
35
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
36
Setting a Breakpoint in the Code Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 36 Breakpoint was set Current line shown in yellow
37
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
38
Questions Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 38
39
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.