1. Trang chủ
  2. » Công Nghệ Thông Tin

microsoft access 2010 vba programming inside out

738 1,8K 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 738
Dung lượng 34,91 MB

Nội dung

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    Chapter1 UsingtheVBAEditorandDebuggingCode   3 Chapter2 UnderstandingtheVBALanguageStructure 39 Chapter3 UnderstandingtheVBALanguageFeatures  89    Chapter4 ApplyingtheAccessObjectModel 127 Chapter5 UnderstandingtheDataAccess ObjectModel                           161    Chapter6 UsingFormsandEvents                  231 Chapter7 UsingFormControlsandEvents           273 Chapter8 CreatingReportsandEvents              323    Chapter9 AddingFunctionalitywithClasses          339 Chapter10 UsingClassesandEvents                  359 Chapter11 UsingClassesandForms                  381    Chapter12 LinkingAccessTables                     395 Chapter13 IntegratingMicrosoftOfce              437   Chapter14 UsingSQLServer                        483 Chapter15 UpsizingAccesstoSQLServer             543 Chapter16 UsingSQLAzure                         589   Chapter17 BuildingApplications                     631 Chapter18 UsingADOandADOX                    659 www.it-ebooks.info www.it-ebooks.info   vii Table of Contents Whatdoyouthinkofthisbook?Wewanttohearfromyou! Microsoftisinterestedinhearingyourfeedbacksowecancontinuallyimproveourbooksandlearning resourcesforyouToparticipateinabriefonlinesurvey,pleasevisit: microsoftcom/learning/booksurvey Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix   Chapter1: UsingtheVBAEditorandDebuggingCode                        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 Conguring the Help System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Working with the Object Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 www.it-ebooks.info viii TableofContents Chapter2: UnderstandingtheVBALanguageStructure                       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 Dening ByRef and ByValue Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Private and Public Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Optional and Named Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 The ParamArray Qualier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 Chapter3: UnderstandingtheVBALanguageFeatures                       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

Ngày đăng: 05/05/2014, 15:09

TỪ KHÓA LIÊN QUAN

w