Chủ Nhật, 9 tháng 3, 2014

Tài liệu Access 2007 VBA Programming docx

Access

2007 VBA Programming For Dummies
®
Published by
Wiley Publishing, Inc.
111 River Street
Hoboken, NJ 07030-5774
www.wiley.com
Copyright © 2007 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
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, scanning or otherwise, except as permit-
ted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written
permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the
Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600.
Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing,
Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at
http://www.wiley.com/go/permissions.
Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the
Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, and related trade
dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United
States and other countries, and may not be used without written permission. Microsoft and Access are
trademarks or registered trademarks of Microsoft Corporation in the United States and/or other coun-
tries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not asso-
ciated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REP-
RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE
CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT
LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CRE-
ATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CON-
TAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE
UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR
OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A
COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE
AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION
OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FUR-
THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE
INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY
MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK
MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT
IS READ. FULFILLMENT OF EACH COUPON OFFER IS THE SOLE RESPONSIBILITY OF THE OFFEROR.
For general information on our other products and services, please contact our Customer Care
Department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.
For technical support, please visit www.wiley.com/techsupport.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may
not be available in electronic books.
Library of Congress Control Number: 2006939596
ISBN: 978-0-470-04653-1
Manufactured in the United States of America
01_046531 ffirs.qxp 1/16/07 9:07 PM Page iv
01_046531 ffirs.qxp 1/16/07 9:07 PM Page vi
Dedication
Joe Stockman: To my mom and all my friends and family who supported me —
and left me alone — during this project.
Alan Simpson: To Susan, Ashley, and Alec, as always.
Authors’ Acknowledgments
Even though only two authors’ names appear on the cover, every book is a
team project. These authors would like to thank the many people who con-
tributed to this book. To Carole McClendon and everyone at Waterside
Productions, thank you for finding this project and making it happen. Also,
many thanks to Kyle Looper and Jean Rogers at Wiley for taking a chance on
a new author to help with the rewrite. And also, thanks to Microsoft for
making Access a wonderful development environment.
01_046531 ffirs.qxp 1/16/07 9:07 PM Page vii
About the Author
Joe Stockman is an independent consultant, software designer, and author
who has been using Microsoft Access since its initial release. He’s also devel-
oped courseware and taught classes in Access and VBA. Joe developed his
first application in Access, and then migrated into Visual Basic and VB.NET,
where he specializes in creating applications for the Windows Mobile plat-
form. He worked for several software companies before forming his consult-
ing business in 2002, where he deals with all types of clients including
healthcare, financial, government, manufacturing, and small business. His
ability to turn his customers’ wishes into working applications keeps them
satisfied. Joe’s also writing the fundamentals column for the Advisor Guide to
Microsoft Access magazine.
Alan Simpson is the author of over 100 computer books on databases,
Windows, Web site design and development, programming, and networking.
His books are published throughout the world in over a dozen languages
and have millions of copies. Alan has also taught introductory and advanced
computer programming courses at San Diego State University and the UCSD
Extension. He has served as a consultant on high-technology, education-
oriented projects for the United States Navy and Air Force. Despite that,
Alan has no fancy job title because he has never had a real job.
01_046531 ffirs.qxp 1/16/07 9:07 PM Page v
Publisher’s Acknowledgments
We’re proud of this book; please send us your comments through our online registration form
located at
www.dummies.com/register/.
Some of the people who helped bring this book to market include the following:
Acquisitions, Editorial, and Media
Development
Associate Project Editor: Jean Rogers
(Previous Edition: Christopher Morris)
Acquisitions Editor: Kyle Looper
Copy Editor: Becky Whitney
Technical Editor: Russ Mullen
Editorial Manager: Kevin Kirschner
Media Development Specialists: Angela Denny,
Kate Jenkins, Steven Kudirka, Kit Malone
Media Development Coordinator:
Laura Atkinson
Media Project Supervisor: Laura Moss
Media Development Manager: Laura VanWinkle
Media Development Associate Producer:
Richard Graves
Editorial Assistant: Amanda Foxworth
Sr. Editorial Assistant: Cherie Case
Cartoons: Rich Tennant
(
www.the5thwave.com)
Composition Services
Project Coordinator: Jennifer Theriot
Layout and Graphics: Carl Byers,
Stephanie D. Jumper, Barbara Moore,
Barry Offringa, Alicia B. South
Proofreaders: Techbooks, Brian H. Walls
Indexer: Techbooks
Anniversary Logo Design: Richard Pacifico
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary Bednarek, Executive Acquisitions Director
Mary C. Corder, Editorial Director
Publishing for Consumer Dummies
Diane Graves Steele, Vice President and Publisher
Joyce Pepple, Acquisitions Director
Composition Services
Gerry Fahey, Vice President of Production Services
Debbie Stailey, Director of Composition Services
01_046531 ffirs.qxp 1/16/07 9:07 PM Page viii
Contents at a Glance
Introduction 1
Part I: Introducing VBA Programming 7
Chapter 1: Where VBA Fits In 9
Chapter 2: Your VBA Toolkit 21
Chapter 3: Jumpstart: Creating a Simple VBA Program 35
Part II: VBA Tools and Techniques 49
Chapter 4: Understanding Your VBA Building Blocks 51
Chapter 5: Controlling Access through VBA 71
Chapter 6: Programming Access Forms 87
Part III: VBA, Recordsets, and SQL 115
Chapter 7: The Scoop on SQL and Recordsets 117
Chapter 8: Putting Recordsets to Work 147
Part IV: Applying VBA in the Real World 173
Chapter 9: Creating Your Own Dialog Boxes 175
Chapter 10: Customizing Combo Boxes and List Boxes 201
Chapter 11: Creating Your Own Functions 239
Chapter 12: Testing and Debugging Your Code 265
Part V: Reaching Out with VBA 293
Chapter 13: Using VBA with Multiple Databases 295
Chapter 14: Integrating with Other Office Applications 315
Part VI: The Part of Tens 349
Chapter 15: Ten Commandments of Writing VBA 351
Chapter 16: Top Ten Nerdy VBA Tricks 357
Index 367
02_046531 ftoc.qxp 1/16/07 9:07 PM Page ix
02_046531 ftoc.qxp 1/16/07 9:07 PM Page x
Table of Contents
Introduction 1
About This Book 1
Conventions Used in This Book 2
What You’re Not to Read 2
Foolish Assumptions 3
How This Book Is Organized 3
Part I: Introducing VBA Programming 3
Part II: VBA Tools and Techniques 3
Part III: VBA, Recordsets, and SQL 4
Part IV: Applying VBA in the Real World 4
Part V: Reaching Out with VBA 4
Part VI: The Part of Tens 4
Icons Used in This Book 4
Web Site for This Book 5
Where to Go from Here 5
Part I: Introducing VBA Programming 7
Chapter 1: Where VBA Fits In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Taking a Look at Access 10
Understanding VBA 11
Seeing Where VBA Lurks 12
Finding standard modules 13
Finding class modules 13
From VBA to Access 15
Finding Out How VBA Works 17
Discovering VBA procedures 17
Recognizing VBA procedures 18
Chapter 2: Your VBA Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21
Using the Visual Basic Editor 21
Using Project Explorer 23
Using the Properties window 24
Using the Immediate window 26
Using the Code window 27
Referring to Objects from VBA 29
Setting References to Object Libraries 30
Using the Object Browser 30
Searching the Object Library 33
02_046531 ftoc.qxp 1/16/07 9:07 PM Page xi
Access 2007 VBA Programming For Dummies
xii
Chapter 3: Jumpstart: Creating a Simple VBA Program . . . . . . . . . . . .35
Creating a Standard Module 35
Creating a Procedure 36
Understanding Syntax 38
Getting keyword help 40
Help with arguments 43
About named arguments 45
Using Existing Code 46
Copy and paste code from the Web 46
Importing modules 47
Modifying existing code 48
Part II: VBA Tools and Techniques 49
Chapter 4: Understanding Your VBA Building Blocks . . . . . . . . . . . . .51
Commenting Your Code 52
Understanding VBA Data Types 53
Passing Data to Procedures 54
Storing data in variables and constants 57
Storing data in arrays 58
Module-level versus procedure-level 60
Naming conventions for variables 61
Repeating Chunks of Code with Loops 62
Using Do Loop to create a loop 62
Using While Wend to create a loop 64
Using For Next to create a loop 64
Making Decisions in VBA Code 66
Using If End If statements 67
Using a Select Case block 68
Chapter 5: Controlling Access through VBA . . . . . . . . . . . . . . . . . . . . . .71
Understanding Object Models 72
Distinguishing between objects and collections 72
Understanding properties and methods 75
Identifying the icons for objects, properties, and methods 77
Manipulating Properties and Methods 79
Getting the value of a property 79
Changing the value of a property 81
Using an object’s methods 82
Seeking help with properties and methods 84
Chapter 6: Programming Access Forms . . . . . . . . . . . . . . . . . . . . . . . . .87
Working with Class Procedures 87
Enabling or Disabling Form Controls 90
Using VBA to position the cursor 91
Choosing an object and event for the code 92
02_046531 ftoc.qxp 1/16/07 9:07 PM Page xii
Showing and hiding controls 95
Making controls read-only 96
Responding to Form Events 96
Changing the Appearance of Objects 99
Changing colors 99
Controlling boldface, italics, and such 103
Changing special effects 104
Using the With End With statements 104
Filling form controls with data 105
Opening and Closing Forms 107
Closing a form 109
Adding a related record to another table 109
More DoCmd methods for forms 112
Part III: VBA, Recordsets, and SQL 115
Chapter 7: The Scoop on SQL and Recordsets . . . . . . . . . . . . . . . . . . .117
What the Heck Is SQL? 117
Writing SQL without knowing SQL 120
Select queries versus action queries 121
Getting SQL into VBA 123
Hiding warning messages 124
Storing SQL statements in variables 125
Creating Tables from VBA 128
Creating new tables from existing tables 128
Creating a new, empty table from VBA 129
Closing and deleting tables through VBA 130
Adding Records to a Table 131
Appending a single record with SQL 132
Query to append one record 133
Changing and Deleting Table Records 134
Performing an Action Query on One Record 136
Working with Select Queries and Recordsets 137
Defining a connection 140
Defining the recordset and data source 141
Filling the recordset with data 142
Managing recordsets 143
Referring to fields in a recordset 145
Closing recordsets and collections 146
Chapter 8: Putting Recordsets to Work . . . . . . . . . . . . . . . . . . . . . . . . .147
Looping through Collections 147
Using For Each loops 149
Using shorter names for objects 152
xiii
Table of Contents
02_046531 ftoc.qxp 1/16/07 9:07 PM Page xiii

Không có nhận xét nào:

Đăng nhận xét