www.it-ebooks.info www.it-ebooks.info Microsoft ® Access ® 2010 VBA Programming Inside Out Andrew Couch www.it-ebooks.info Published with the authorization of Microsoft Corporation by: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, California 95472 Copyright © 2011 by Andrew Couch All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. ISBN: 978-0-7356-5987-2 1 2 3 4 5 6 7 8 9 M 6 5 4 3 2 1 Printed and bound in the United States of America. Microsoft Press books are available through booksellers and distributors worldwide. If you need support related to this book, email Microsoft Press Book Support at mspinput@microsoft.com. Please tell us what you think of this book at http://www.microsoft.com/learning/booksurvey. Microsoft and the trademarks listed at http://www.microsoft.com/about/legal/en/us/ IntellectualProperty/Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property of their respective owners. The example companies, organizations, products, domain names, email addresses, logos, people, places, and events depicted herein are ctitious. No association with any real company, organization, product, domain name, email address, logo, person, place, or event is intended or should be inferred. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the author, O’Reilly Media, Inc., Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Kenyon Brown Teresa Elsey Octal Publishing, Inc. Alan Cossey Denise Getz Twist Creative • Seattle Karen Montgomery Robert Romano www.it-ebooks.info pour Pamela, ma raison d’être www.it-ebooks.info www.it-ebooks.info v Contents at a Glance Chapter1 UsingtheVBAEditorandDebuggingCode 3 Chapter2 UnderstandingtheVBALanguageStructure 39 Chapter3 UnderstandingtheVBALanguageFeatures 89 Chapter4 ApplyingtheAccessObjectModel 127 Chapter5 UnderstandingtheDataAccess ObjectModel 161 Chapter6 UsingFormsandEvents 231 Chapter7 UsingFormControlsandEvents 273 Chapter8 CreatingReportsandEvents 323 Chapter9 AddingFunctionalitywithClasses 339 Chapter10 UsingClassesandEvents 359 Chapter11 UsingClassesandForms 381 Chapter12 LinkingAccessTables 395 Chapter13 IntegratingMicrosoftOfce 437 Chapter14 UsingSQLServer 483 Chapter15 UpsizingAccesstoSQLServer 543 Chapter16 UsingSQLAzure 589 Chapter17 BuildingApplications 631 Chapter18 UsingADOandADOX 659 www.it-ebooks.info www.it-ebooks.info vii Table of Contents Whatdoyouthinkofthisbook?Wewanttohearfromyou! Microsoftisinterestedinhearingyourfeedbacksowecancontinuallyimproveourbooksandlearning resourcesforyouToparticipateinabriefonlinesurvey,pleasevisit: microsoftcom/learning/booksurvey Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Chapter1: UsingtheVBAEditorandDebuggingCode 3 Debugging Code on a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 Entering the VBA Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 The Application and VBA Code Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 Creating Modules and Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 Creating a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Creating a Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Executing a Subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Executing a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Viewing and Searching Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Split Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Searching Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Debugging Code in a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Debug Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Breakpointing Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Set Next Command. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Breakpoint Step and Run Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Displaying Variables in the Locals Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Tracing Procedures with the Call Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Watching Variables and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Adding Conditional Watch Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Working with the Immediate Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Changing Code On-the-Fly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Using the Object Browser and Help System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Conguring the Help System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Working with the Object Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 www.it-ebooks.info viii TableofContents Chapter2: UnderstandingtheVBALanguageStructure 39 VBA Language Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Setting Option Explicit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Selecting Option Compare. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Compiling Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Conditional Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Working with Constants and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Improving Code Quality with Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 The Enum Keyword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Variables and Database Field Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Handling NULL Values, IsNull and Nz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Using Static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Using Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Variable Scope and Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Working with Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Type Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Functions and Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Managing Code with Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Dening ByRef and ByValue Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Private and Public Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Optional and Named Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 The ParamArray Qualier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Organizing Code in Modules and Class Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Control Statements and Program Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 IF… Then… Else… Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 IIF Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Choose Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Select Case Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 TypeOf Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 For and For Each Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Do While and Do Until Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Exit Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 The With Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 GoTo and GoSub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Line Continuation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Splitting SQL Over Multiple Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Chapter3: UnderstandingtheVBALanguageFeatures 89 Using Built-In Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Date and Time Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 String Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Domain Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Constructing Where Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 SQL and Embedded Quotes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 www.it-ebooks.info [...]... introduction of object-oriented programming, VBA has kept pace with the expectations of modern programming The two products that have shaped VBA the most are Microsoft Excel and Microsoft Access; Excel introduced VBA and originally gained VBA programming features in advance of these becoming available within Access A significant strength of VBA is that it is universal to the Microsoft Office suite of programs;... background, which means that you can learn VBA programming in Access very quickly We will spend no time explaining how to create a table, form, or report, and if you cannot do this, you need to first learn these actions in more detail We recommend our companion text Microsoft Access 2010 Inside Out by Jeff Conrad and John Viescas If you have some VBA Programming experience, you can skim over Chapters 1–3... dig deep into programming with the objects that make up Access, including the DAO programming language, which is the bread and butter programming technique for any Access VBA developer Part 3, “Working with Forms and Reports” Chapters 6, 7, and 8 illustrate how to apply VBA when working with forms, controls, and reports This develops your core techniques in understanding how to apply VBA for building... Linked to Access 460 Using MS Query and Data Sources 468 Connecting Access to Outlook 471 Extracting Information from Outlook 472 Creating Objects in Outlook 475 Writing to Access from Outlook... aspects of VBA programming As well, we have devoted a significant number of our pages to supporting you in developing with both SQL Server and cloud computing www.it-ebooks.info Introduction xxi Assumptions About You We make a basic assumption in this book that you are experienced either in working with Access or that you have a strong programming background, which means that you can learn VBA programming. .. revolutionary feature introduced with the programming language of Access Basic, conforming to the new VBA standard established in Excel VBA suddenly provided the longawaited platform for the simple integration of the Office products and building solutions that could easily exploit the strengths of each component product in the Office suite The combination of Access and VBA offers an extremely productive environment... 675 www.it-ebooks.info www.it-ebooks.info Introduction Microsoft Visual Basic for Applications (VBA) is an exceptional programming language and environment The language has grown out of a need to have a programming language that would allow more business-focused individuals to write programs, but equally support the programming features that developers look for in a product The environment... for example, “Open the form frmSample and rightclick the ListBox control.” Design Conventions INSIDE OUT T his statement illustrates an example of an Inside Out heading These are the book’s signature tips In these tips, you get the straight scoop on what’s going on with the software inside information about why a feature works the way it does You’ll also find handy workarounds to deal with software... more code examples and provide useful utilities to add to your program libraries To access and download the companion content, visit: http://go .microsoft. com/ FWLink/?Linkid=223727 Chapter or topic Content Chapter 1 ● VBAEnvironment.accdb Chapter 2 ● VBAExamples.accdb Chapter 3 ● VBAFeaturesExamples.accdb Chapter 4 ● AccessObjectModel.accdb Chapter 5 ● DAOExamples.accdb ● CountryLibrary.accdb ● Find_IT.accdb... book gives you access to an online edition of the book (You can also download the online edition of the book to your own computer; see the next section.) To access your online edition, do the following: 1 Locate your voucher inside the back cover, and scratch off the metallic foil to reveal your access code 2 Go to http://microsoftpress.oreilly.com/safarienabled 3 Enter your 24-character access code . www.it-ebooks.info www.it-ebooks.info Microsoft ® Access ® 2010 VBA Programming Inside Out Andrew Couch www.it-ebooks.info Published with the authorization of Microsoft Corporation by: O’Reilly. to this book, email Microsoft Press Book Support at mspinput @microsoft. com. Please tell us what you think of this book at http://www .microsoft. com/learning/booksurvey. Microsoft and the trademarks. Microsoft and the trademarks listed at http://www .microsoft. com/about/legal/en/us/ IntellectualProperty/Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks