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

Access 2010 Programmer''''s Reference doc

1,3K 1,2K 2
Tài liệu đã được kiểm tra trùng lặp

Đ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

Tiêu đề Access 2010 Programmer’s Reference
Tác giả Teresa Hennig, Rob Cooper, Geoffrey Griffith, Jerry Dennison
Trường học Wrox
Chuyên ngành Database Management / General
Thể loại Reference
Năm xuất bản 2010
Định dạng
Số trang 1.276
Dung lượng 24,91 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Access 2010 Programmer’s Reference: • Shows you how to program VBA and macros for Access, including use of error handling, best practices, and helpful tips • Provides code examples that

Trang 1

Teresa Hennig, Rob Cooper, Geoffrey Griffith, Jerry Dennison

Wrox Programmer to Programmer™

Join the discussion @ p2p.wrox.com

Microsoft Access 2010 offers a rich set of features and design tools

for storing, analyzing, and viewing data, along with the inherent

ability to connect to a large variety of data sources With this book,

you’ll discover the benefits of using the newest features to build

small- to medium-scale database applications, integrate with other

programs, create Web applications, and upgrade existing solutions.

Access 2010 Programmer’s Reference:

• Shows you how to program VBA and macros for Access,

including use of error handling, best practices, and helpful tips

• Provides code examples that demonstrate how to enhance

and extend the functionality of Access applications

• Shows when, why, and how to use DAO and ADO in VBA

• Demonstrates how to build Access applications that leverage

SharePoint®, SQL Server®, Excel®, XML, and other sources

• Discusses deployment considerations, such as working with mixed

platforms and versions, 64-bit Office, Access Runtime, Access and

database security, and upgrading and conversions

Teresa Hennig is a Microsoft Access MVP, and is President of the Seattle Access

Group and the Pacific Northwest Access Developer Group Her company, Data

Dynamics NW, has been creating Access solutions since 1997

Rob Cooper is a senior test lead at Microsoft During the Access 2010 release, he

led the team that worked on exciting new features such as the Macro Designer,

Navigation Control, and Web Browser Control

Geoffrey Griffith has more than 13 years experience with Access He began his

Access life working in Access 2.0 and eventually worked on the Microsoft Access

Team for the Access 2007 release

Jerry Dennison is a Microsoft Access MVP and has been developing custom

and commercial Access applications since 1991 He is the owner of TradeIt!

Software Support Services, an Access-based barter management solution

Wrox Programmer’s References are designed to give the experienced developer

straight facts on a new technology, without hype or unnecessary explanations

They deliver hard information with plenty of practical examples to help you

apply new tools to your development projects today

Build robust database applications

with Access 2010

wrox.com

Programmer Forums

Join our Programmer to Programmer forums to ask and answer programming questions about this book, join discussions on the hottest topics in the industry, and connect with fellow programmers from around the world

Code Downloads

Take advantage of free code samples from this book, as well as code samples from hundreds of other books, all ready to use

Read More

Find articles, ebooks, sample chapters and tables of contents for hundreds of books, and more reference resources on programming topics that matter

Trang 2

Related Wrox Books

Beginning SharePoint 2010 Development

ISBN: 978-0-470-58463-7SharePoint provides content management (enterprise content management, Web content management, records management, and more), workflow, and social media features, and the new version boasts enhanced capabilities This introductory-level book walks you through the process of learning, developing, and deploying SharePoint 2010 solutions

Knight’s 24-Hour Trainer: Microsoft SQL Server 2008 Integration Services

ISBN: 978-0-470-49692-3

A unique lesson-based book that walks you through the essential parts of SSIS Each lesson is streamlined to teach a specific component of SSIS, helping you to focus on just what you need in order to succeed As many readers learn better by getting their hands on the product, this book provides you with a step-by-step lab for each lesson with a video to accompany it

Microsoft SQL Server Reporting Services Recipes: for Designing Expert Reports

ISBN: 978-0-470-56311-3 Packed with proven design practices, this book serves as a collection of recipes for solving design problems so that you don’t have to reinvent the wheel with each challenge you face Organized by specific types of reports, the book covers grouped reports, charts, composite reports, dashboards, forms and labels, interactive reports, and more Step-by-step instructions allow you to implement these best practices immediately so that you can solve your own design hurdles quickly

Microsoft SQL Server 2008 Integration Services: Problem, Design, Solution

ISBN: 978-0-470-52576-0 SQL Server Integration Services (SSIS) is the most widely adopted Business Intelligence (BI) component of SQL Server and the leading extraction, transformation, and loading (ETL) tool in the data warehouse industry Written by a team of Microsoft MVPs for SQL Server, this book presents you with an array of common problems that SSIS administrators and developers face on a daily basis, and walks you through the steps necessary to solve these challenges

Professional Microsoft PowerPivot for Excel and SharePoint

ISBN: 978-0-470-58737-9Authored by members of the Microsoft team behind the creation of PowerPivot, this book shows you how to use PowerPivot for Excel to create compelling BI solutions, perform data analysis, and achieve unique business insight You’ll learn how to use PowerPivot for SharePoint to share your BI solutions and collaborate with others And your organization will learn how to use SQL Server 2008 R2 management tools to achieve more efficient results

Professional Microsoft SQL Server Analysis Services 2008 with MDX

ISBN: 978-0-470-24798-3 This book explains how to best use these enhancements for your business needs The authors provide you with valuable insight

on how to use Analysis Services 2008 effectively to build, process, and deploy top-of-the-line business intelligence applications

You’ll explore everything that Analysis Services 2008 has to offer with the help of step-by-step instructions on building dimensional databases

multi-Professional Microsoft SQL Server 2008 Reporting Services

ISBN: 978-0-470-24201-8 This hands-on guide will show you how to harness the full power of Reporting Services to create reporting and business intel-ligence solutions that meet your company’s needs It walks you step-by-step through the fundamentals of designing the most effective reports by following careful planning considerations The authors progress from beginning to advanced report design and filtering techniques, showing you the conditions where reports could be more efficient They also explore holistic business intel-ligence solutions, comprehensive OLAP/Analysis Services reporting, and complete production-deployment scenarios

Professional SharePoint 2010 Development

978-0-470-52942-3 This comprehensive book shows readers how to build field-tested solutions and create custom content management applications

Get more out of

Wrox Online Library

Hundreds of our books are available online

through Books24x7.com

Wrox Blox

Download short informational pieces and code

to keep you up to date and out of trouble!

Join the Community

Sign up for our free monthly newsletter at newsletter.wrox.com

Browse

Ready for more Wrox? We have books and e-books available on NET, SQL Server, Java, XML, Visual Basic, C#/ C++, and much more!

Contact Us

We always like to get feedback from our readers Have a book idea?

Begi

978-0This ilanguproce

Begi

978-0This bdevelLeopa

Prof

978-0The sand itbottothe loand s

Safa

978-0With numbdevelfrom

of bufor iP

Trang 3

INTRODUCTION xxxiii

CHAPTER 1 Introduction to Microsoft Access 2010 1

CHAPTER 2 New Features 13

CHAPTER 3 Upgrading and Converting to Access 2010 33

CHAPTER 4 Macros in Access 2010 63

CHAPTER 5 Using the VBA Editor 119

CHAPTER 6 VBA Basics 141

CHAPTER 7 Using VBA in Access 173

CHAPTER 8 Creating Classes in VBA 227

CHAPTER 9 Extending VBA with APIs 283

CHAPTER 10 Working with the Windows Registry 319

CHAPTER 11 Using DAO to Access Data 355

CHAPTER 12 Using ADO to Access Data 419

CHAPTER 13 Using SQL with VBA 459

CHAPTER 14 Using VBA to Enhance Forms 489

CHAPTER 15 Enhancing Reports with VBA 549

CHAPTER 16 Customizing the Ribbon 581

CHAPTER 17 Customizing the Offi ce Backstage 631

CHAPTER 18 Working with Offi ce 2010 671

CHAPTER 19 Working with SharePoint 707

CHAPTER 20 Working with NET 757

CHAPTER 21 Building Client-Server Applications with Access 799

CHAPTER 22 The Access 2010 Templates 841

CHAPTER 23 Access Runtime Deployment 861

CHAPTER 24 Database Security 875

CHAPTER 25 Access 2010 Security Features 919

Trang 4

APPENDIX C ADO Object Model Reference 1035

APPENDIX D 64-Bit Access 1095

APPENDIX E References for Projects 1103

APPENDIX F Reserved Words and Special Characters 1113

APPENDIX G Naming Conventions 1127

APPENDIX H The Access Source Code Control 1137

APPENDIX I Tips and Tricks 1145

INDEX 1191

Trang 5

PROGRAMMER’S REFERENCE

Teresa Hennig Rob Cooper Geoffrey Griffi th Jerry Dennison

Trang 6

10475 Crosspoint Boulevard

Indianapolis, IN 46256

www.wiley.com

Copyright © 2010 by Teresa Hennig, Rob Cooper, Geoffrey Griffi th, Jerry Dennison

Published by Wiley Publishing, Inc., Indianapolis, Indiana

Published simultaneously in Canada

ISBN: 978-0-470-59166-6

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1

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 permitted 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

Permis-sions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008,

or online at http://www.wiley.com/go/permissions.

Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with

respect to the accuracy or completeness of the contents of this work and specifi cally disclaim all warranties, including

without limitation warranties of fi tness for a particular purpose No warranty may be created or extended by sales or

pro-motional materials The advice and strategies contained 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

pub-lisher nor the author shall be liable for damages arising herefrom The fact that an organization or Web site is referred to

in this work as a citation and/or a potential source of further information does not mean that the author or the publisher

endorses the information the organization or Web site may provide or recommendations it may make Further, readers

should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was

written and when it is read.

For general information on our other products and services please contact our Customer Care Department within the

United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.

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: 2010929738

Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress

are trademarks or registered trademarks of John Wiley & Sons, Inc and/or its affi liates, in the United States and other

countries, and may not be used without written permission Microsoft and Access are registered trademarks of Microsoft

Corporation in the United States and/or other countries All other trademarks are the property of their respective owners

Wiley Publishing, Inc is not associated with any product or vendor mentioned in this book.

Trang 7

moments — sights, sounds, and emotions to be cherished May we all remember to pause and savor the blessings that we have, especially the time that

we share with those we love

— Teresa

To my family, for all of the love and support you’ve

shown me over the years.

— Rob

To my wife, Jamie, and my son, Ryan, I couldn’t have been blessed with a better family and this would not have been possible without all of your love and

support!

— Geoff

To my wife Dianne, my son, Jeremy, my daughter, Amber, and my late parents, Clifford and Jeanette Dennison You are my inspiration and my life Thanks Dad, for instilling into me the insatiable appetite to explore and learn, without which I would not be where I am To my grandchildren: Brianna, Fisher,

and Huntlee You are our future.

— Jerry

Trang 8

TECHNICAL EDITORS

Dr Jeff BoyceAlbert D KallalArmen Stein

TIP CONTRIBUTORS

Dane MillerGarry RobinsonSteve SchapelLarry Strange

PRODUCTION EDITOR

Kathleen Wisor

COPY EDITORS

Nancy RapoportPaula Lowell

Trang 9

TERESA HENNIG loves challenges, solving problems, and making things happen So

it is no surprise that she was immediately hooked on Access; by its tools for rapid development and the ability to quickly create intuitive, user-friendly applications

Within a month, she started her own company as an Access developer and business consultant (1997) With a strong background in business and project management, Teresa (and her company, Data Dynamics NW), focuses on using Access to provide cost-effective custom database solutions In recognition of her expertise and dedication to the Access commu-nity, Teresa has been awarded as a Microsoft Access MVP (Most Valuable Professional) every year since 2006 She continues to serve as President of both the Pacifi c Northwest Access Devel-opers Group (PNWADG) and the Seattle Access Group Her leadership, expertise, and service

to the Access community have also earned her recognition as a National Community Champion from INETA Being the lead author of several Access books has afforded Teresa the opportunity

to work with esteemed colleagues and to invite others to share the experience of becoming lished authors Learn more at www.DataDynamicsNW.com and www.SeattleAccess.org

pub-ROB COOPER is a Senior Test Lead at Microsoft He started at Microsoft as a support engineer in Charlotte, North Carolina in 1998 and joined the Access 2003 test team

in Redmond in 2001 During the Access 2010 release, he led the team that worked on exciting new features such as the Macro Designer, Navigation Control, Web Browser Control, and the design and migration of objects for Access Services He also helped test the 64-bit version of VBA in Offi ce 2010 and programmability and security in Access 2010 He

is currently working on Microsoft Bing Rob has spoken at user group meetings and conferences such as the Portland Access User Group conference and Offi ce DevCon in Brisbane, Australia, and has written for the Microsoft Knowledge Base Aside from writing code in Access and C#, he also enjoys spending time with his family cooking, watching movies, going to the zoo and aquarium, and hanging out in and around Seattle

GEOFFREY GRIFFITH is a professional software developer from Colorado, where he owns his own software consulting company He holds a Bachelor of Science degree

in Computer Science from the University of Colorado at Boulder, where he studied software engineering He began his Access life working on various database systems, starting with Access 2.0, developed at GBS, Inc., a software development fi rm in Boulder, Colorado An avid Access user, he worked on the Microsoft Access Team for the Access

2007 release Today, Geoff continues his work with Access, by writing books about Access and building applications that use Access databases

JERRY DENNISON has over 18 years of professional experience designing and oping MS Access database applications beginning with Access v1.0 He has been awarded the Microsoft Offi ce Access Most Valuable Professional Award for the past four years Jerry is an active contributor and Administrator at UtterAccess.com, the premier MS Access forum on the Web He is considered by many of his peers to be one of the foremost experts on the Forms of Data Normalization (a set of guidelines for relational databases developed by Dr E F Codd) Jerry currently lives in Easley, South Carolina with his wife

devel-of 21 years and their two dogs, Duke and Duchess

Trang 10

ABOUT THE CONTRIBUTING AUTHORS

AND TECHNICAL EDITORS

BEN CLOTHIER is an Access developer working as an independent contractor through development shops such as Advisicon and J Street Technology He is also a certifi ed MySQL developer, and has designed and supported Access front-end applications for corporate databases In recognition of his contributions to the Access community, Ben is recognized as a Microsoft Access MVP (Most Valued Professional) Ben lives

in San Antonio, Texas with his wife, Suzanne, and his son, Harry

Suzanne, only with you could I have the time and energy to become a contributing author — you

are the absolute best!

DOUG (DAGI) YUDOVICH is the Director of Business Information Applications for UW Physicians in Seattle The department’s primary responsibilities range from develop-ing enterprise-level Web-based reports and OLAP objects to developing database applications in support of various business needs for managing data The applications vary in scope, from stop-gap applications, to bolt on tools, to mission critical–level applications that support up to 250 users All of the database applications use Access for the FE,

with some using Jet as the database platform, and some using SQL Server Doug is also an

Adminis-trator on UtterAccess Forums and UtterAccess’s Access Wiki Joining UA in 2004, Doug progressed

through the ranks (VIP, Editor, Administrator) UA is Doug’s cyber home, where he shares his

passion and knowledge of database design and information management with members who seek

help, and with fellow developers discussing best practices Doug is an active member of the Pacifi c

Northwest Access Developer Group (PNWADG) and a presenter in the Seattle Access Group (SAG)

Doug participated in two Developers’ Kitchen events with the Microsoft Access Dev Team to work

on early development versions of Access 2010 In acknowledgment of his contributions to the Access

developers’ community in UtterAccess and the user groups, and his work with the Access Dev Team,

Doug was awarded as a Microsoft Access MVP in 2009 and 2010

I’d like to thank my wife, Lisa, who patiently picks up my slack as I dive head-fi rst into new

adven-tures I love you.

Trang 11

DR JEFF BOYCE has been providing process and technology solutions to both public and private sectors for over 30 years Jeff’s approach focuses fi rst on business need, emphasizing processes and practices before evaluating suitable technologies Jeff has designed and developed over 25 applications Most recently, his integrated Micro-soft Offi ce solutions provide data-based applications employing Access, Word, and SQL Server The support he has provided in the Microsoft Access newsgroups has resulted in his recognition as a Microsoft Access MVP for the past eight years When not working at his day job, consulting, or offering support in the newsgroups, Jeff enjoys wallyball, swimming, and recreational landscaping.

Jeff acknowledges and thanks his wife, Sue — her forbearance made his contribution possible.

ALBERT D KALLAL is the owner of HCS consulting group and has been professionally developing software for more than 20 years His fi rst major project started while he was studying Computing Science at the University of Alberta Albert was the original developer of a pilot project called Anat-sim, which was the basis for Omni-sim, the

fi rst successful commercial authoring system that allowed educators to create tions without having to write code Albert’s early success was an omen of great things to come His software is currently used in more than six countries around the world with database platforms ranging from PDAs to server and mainframe systems In recognition of Albert’s contributions to the Access community, he received a Microsoft Most Valuable Professional award Always seeking to stay on the leading edge, Albert was an early beta tester for the new Access 2010 Web Services that enables Access developers to build browser-neutral Web applications with Access 2010

applica-ARMEN STEIN is the president of J Street Technology, a Microsoft Gold Certifi ed Partner with a team of database application developers near Seattle, Washington J Street also offers CartGenie, a complete e-commerce Web storefront and shopping cart Armen is a Microsoft Access MVP and MCP, has taught college-level database classes, and has also developed and taught one-day training classes on Access and Access/SQL Server devel-opment Armen has spoken at various user groups and conferences, including Microsoft TechEd in the

United States and Offi ce DevCon in Australia, and is co-author of Access 2007 VBA Programmer’s

Reference (Wrox) His other interests include activities with his family, travel, backgammon, Mariners

baseball, and driving his 1969 Ford Bronco in the sun

Thanks to my wife, Lori, and to my kids, Lauren and Jonathan I also would like to thank my great team at J Street Technology, who capably manage projects so that I can spend time on books like this.

Trang 13

AS A TEAM, WE would like to express our respect and appreciation for all of the people who uted to the editing and production of our book Every book seems like a new adventure and we are fortunate to have the ongoing guidance from Wiley and Wrox to ensure that they are produced with the best standards So we send a special thanks to Bob Elliott and Tom Dinse — is this our 5th book together? We’d also like to recognize some of the great behind-the-scenes players, Ashley Zurcher, Helen Russo, Mary Beth Wakefi eld, and Nancy Rapoport, and many others.

contrib-Of course, we wouldn’t have anything to write about if it weren’t for those incredible people on the Microsoft Access team Their passion, dedication, and commitment to continuously expand and enhance Access are truly remarkable and inspiring We see this, not only in the new features, but also in their support of the user community And speaking of community, the Access community is

a large part of what makes Access so special Whether you are a novice or seasoned developer, if you have a problem, you can easily fi nd assistance, guidance, and even friendship, from a variety of user groups and online forums

We’ll wrap up with a resounding round of appreciation for our fi ve tech editors - and contributing authors With the incredible new features and dramatic changes in Access, we have relied on their expertise to not only identify potential errors and omissions, but to question our content and writ-ing to help ensure that it is both comprehensive and comprehensible It is no wonder that they have each been recognized by Microsoft as Access MVPs It is a pleasure to acknowledge you as an inte-gral part of our team and to the success of this book

— The Authors

FIRST AND FOREMOST, I want to thank my wonderful family who taught me to embrace life and

to live each day with respect, integrity, and purpose Thank you for helping me to recognize and celebrate the opportunities that life offers I can rely on your encouragement and support as I dive into new adventures and projects such as this book

I also want to thank my colleagues and great friends who continue to fuel my passion for helping others to learn about and use Access In particular, I want to express my appreciation for the people

in my user groups (Michka, Randy, Brandi, et al.) and the amazing people who are Access MVPs and community experts (Larry, Truitt, Arvin, George, our TEs, the list goes on) — friendships that are priceless!

One of my favorite aspects of being an author is the opportunity to involve and recognize tions from people from around the world In addition to our remarkable team of authors and editors,

contribu-we have the benefi t of sharing tips from our esteemed colleagues, Dane Miller, Garry Robinson, Steve Schapel, and Larry Strange I especially want to recognize and thank Ben Clothier for his dedication, humor, and tenacity Neither of us had any idea of what was to come when Ben became a contributing

Trang 14

author Even working into the wee hours of the morning, we were still having fun as we completed

countless iterations of reviews That epitomizes a great Access developer — passion, commitment, and

collaboration My thanks to everyone — a project of this magnitude is truly a team effort!

— Teresa

THANKS TO EVERYONE ON the Access team at Microsoft for their outstanding work during this

release and in particular to the testers on my team for listening to my various ramblings about

Access (and food): Nishant Kumar, Kevin Nickel, Michael Tucker, Sanghmitra Gite, Kevin Bell, and

Rasika Chakravarthy Thanks for all of your hard work on this release!

To Geoff Hollander, Jack Stockton, and everyone who’s been a part of the Portland Access User Group

conference the past few years Thanks for the invite and for including me as a part of your family!

Last but not least, continued thanks to my wife, Sandi, and children, Isabel and Gillian, for their

understanding while I was working evenings and weekends To my oldest, Taryn, for coming to visit

earlier in the year and indulging me with sushi (Yup, still better on the left coast.)

— Rob

I’D LIKE TO THANK my wife, Jamie Without all of your love and support, I would be nothing, and

I appreciate everything you do for our family I would like to thank my son, Ryan, and apologize

for the time I missed in your life while I wrote this book You two are the most important people in

the world to me! I would like to thank my friends from GBS, Inc.: Andrei, Vladimir, Tianru, Steph,

Mia, Richard, Bob, and Jeff Without you, I would never have gotten my start on Access and the

experience was invaluable to me I would like to thank so many of my friends from the Access team

who taught me so much about Access, and to call out a few specifi cally: Michael, Rob, Tosh, Tim,

Shawn, and Sherri Thanks for the knowledge and opportunities you gave me; the experience was

invaluable And, of course, I would like to thank all of my other friends, colleagues, and teachers

that have meant to so much me over the years You are all Rock Stars!

— Geoff

I’D LIKE TO THANK my wife, Dianne, a shining light guiding this lost soul into the sunshine My son,

Jeremy and daughter, Amber, no father could ask for better or brighter children to keep him

hum-bled I’d like to also acknowledge my online “family” at UtterAccess.com; especially Gord, Ricky,

and George You guys are the best Most of all, I would like to thank Teresa Hennig for giving me

the opportunity to work on this project with such a great team (thanks Rob and Geoff for being so

patient with the “new kid”)

— Jerry

Trang 15

INTRODUCTION xxxiii

CHAPTER 1: INTRODUCTION TO MICROSOFT ACCESS 2010 1

Microsoft Offi ce Access 2010 3SQL Server 2008 Express Edition 3

Getting Started in Access 2010 5Access 2010 Database Templates 5

Integration with Offi ce Themes 18

Trang 16

Forms 22

Macros 25

Working with Data on SharePoint 27Publish the Database to SharePoint 27Additional SharePoint Features 27

Introducing the Web Form Designer 29Introducing Web Report Designer 29Feature Restrictions (Features Disabled in Web Applications) 30

ISAMs 30Replication Confl ict Resolver 31

Feature Sets and File Extensions: What’s New, What’s Replaced, What Happens 36

Installing Multiple Versions of Access on One PC 46

Selecting the Default File Format 48Overriding the Default File Format 48

Steps for Converting or Enabling 49File Conversion Using Access 2010: A One-Stop Shop 49Other Considerations When Converting 50Converting to Access 97 or Earlier Is a Two-Version Process 51

Converting a Password-Protected Database 52Converting a Database with Password-Protected VBA 53

Trang 17

Converting a Replicated Database 53

Enabling the Experience: Opening 95 or 97 Files with Access 2010 56

Porting an Access application to a 64-bit Platform 58

Summary 61

CHAPTER 4: MACROS IN ACCESS 2010 63

Sharing Macros Using Access 2010 76

Data Macro Actions and Arguments 98

Variables 101

Summary 117

CHAPTER 5: USING THE VBA EDITOR 119

Trang 18

Object Browser Components 122

The Debug.Print Statement 127The Debug.Assert Statement 128Breakpoints 128

Summary 139

Variables 147

Variable Scope and Lifetime 151

Properties 162Methods 162Events 162

Summary 171

Trang 19

CHAPTER 7: USING VBA IN ACCESS 173

Copying Trees of Parent and Child Records 192Using Bookmark and RecordsetClone 194

Stepping through Your Code 204

Trang 20

Drilling Down with Double-Click 206

The Diff erence Between & and + 211String Concatenation Example 212

Two Kinds of Errors: Unexpected and Expected 214

Cleaning Up after an Error 221More on Absorbing Expected Errors 222

Summary 225

CHAPTER 8: CREATING CLASSES IN VBA 227

Adding a Class Module to the Project 230

A Brief Word on Naming the Class 230Instantiating Class Objects 231

Creating Property Procedures 234

Identifying a Class Instance 243

Initialize and Terminate Events 243Creating Custom Class Events 244

Handling Errors in Classes 248

Creating the Subclassed Form 261Creating a Parent Property 261

Trang 21

Creating and Using Collection Classes 264

Encapsulation 275Inheritance 276Polymorphism 276

Instancing 280

Summary 281

CHAPTER 9: EXTENDING VBA WITH APIS 283

Specifying the Lib(rary) and Argument List 290

Pointers in 64-Bit Windows 301

Err.LastDLLError 304 Distributing Applications That Reference Type Libraries

Returning the Path to the Windows Folder 306

Trang 22

Determining Whether the System Processor Is 32-Bit or 64-Bit 307Determining Whether Windows Is 32-Bit or 64-Bit 308Determining Whether Offi ce Is 32-Bit or 64-Bit 308Displaying the Windows Open Dialog Box 309Finding the Position of a Form 311Finding the Temp Directory 312Generating a Unique Temp Filename 312Finding the Login Name of the Current User 313Finding the Computer Name 314Opening or Printing Any File 314

Getting the Path to a Special Folder 315

Summary 317

CHAPTER 10: WORKING WITH THE WINDOWS REGISTRY 319

What the Registry Controls 321

Registry Organization on 64-Bit Windows 328

SaveSetting 330GetSetting 330GetAllSettings 331DeleteSetting 332Typical Uses for the Built-In VBA Registry Functions 332

Setting the Value for a Key 342Getting the Value for a Key 343Deleting a Registry Value 344

Testing the Function Wrappers 346Opening an Existing Registry Key 348Connecting to the Registry on a Remote Computer 349Enumerating Registry Keys and Values 350

Summary 353

Trang 23

CHAPTER 11: USING DAO TO ACCESS DATA 355

Multi-Value Lookup Fields 357

The Workspaces Collection 361

The Default (Access) Database 366

Opening an External Database 368

Creating, Setting, and

Creating Tables and Fields 375

Creating Multi-Value Lookup Fields 382Creating Calculated Fields 385

Filtering and Ordering Recordsets 391

Bookmarks and Recordset Clones 399

Working with Attachment Fields 409

Summary 417

Trang 24

CHAPTER 12: USING ADO TO ACCESS DATA 419

Creating an Implicit Connection 421Creating an ADO Connection Object 422Creating a Connection String 423Creating a Data Link Connection 424

Overview of the ADO Object Model 429

Working with Data in Recordsets 441

Implementing ADO Event Methods 449Implicitly Triggering Events 449Explicitly Calling Events 450Testing the State Property 451

Specifying Constraint Columns 452Using ACE Specifi c Schemas 452

Adding References to ADOX 453

Working with Views (Queries) 455Managing Security with ADOX 457

Summary 457

CHAPTER 13: USING SQL WITH VBA 459

Building SQL Strings with Quotes 460Using Single Quotation Marks instead of Double Quotation Marks 461

Trang 25

Concatenating Long SQL Strings 462

Selections on Index Forms 466

Using SQL for Report Selection Criteria 475Altering the SQL inside Queries 480

The ReplaceOrderByClause and

Columnar and Tabular Layouts 495Anchoring 496The Modal Dialog Box Mode 499Control Wizards — Creating Command Buttons Using

On Click(): Open a Form Based on a Value on the Current Form 531

Displaying Data in TreeView and ListView Controls 540

Trang 26

New in Access 2007 551

Changing the RecordSource at Runtime 565Gathering Information from a Form 566

Dictionary-Style Headings 568

Conditional Formatting of a Control 572Creating a Progress Meter Report 573

Ribbon Customization Using the Options Dialog Box 583

Defi ning a Ribbon Using XML 586Writing Callback Routines and Macros 597

Trang 27

Refreshing Ribbon Content 607

Building the Report Manager 609Building the Custom Filter Interface 613

Defi ning the Tabs and Groups 616

Building the Settings Tab 625Building the Administration Tab 626

Summary 630

CHAPTER 17: CUSTOMIZING THE OFFICE BACKSTAGE 631

Uses for the Backstage in Custom Applications 633

Tab 634Group 635TaskGroup, Category, and Task 637TaskFormGroup 638Button 640GroupBox 641Hyperlink 641ImageControl 641LayoutContainer 642RadioGroup 643

Adding a Group to an Existing Tab 649Adding a Category to an Existing TabFormGroup 650Adding a Task to an Existing TaskGroup 651

Trang 28

onShow 653onHide 653getStyle 654getHelperText 654getTitle 654getTarget 655

Access Runtime Experience 655Setting the Title of a Tab to the Application Title 656About Page and Contact Form 656Warning for a Missing Reference 660Custom Database Information 662Creating a Bulleted or Numbered List 664

Summary 668

CHAPTER 18: WORKING WITH OFFICE 2010 671

Setting References to Outlook 672Creating Outlook Application Objects 672Working with MailItem objects 674Outlook Security Features 678

More Information about Outlook 684

Setting References to Excel 684Creating Excel Application Objects 685Working with Excel Workbooks 686Working with Sheets in Excel 689Working with Data in Excel 691More Information about Excel 695

Setting References to Word 695Creating Word Application Objects 696Working with Document Objects 698Working with Data in Word 701More Information about Word 705

Summary 705

Trang 29

CHAPTER 19: WORKING WITH SHAREPOINT 707

SharePoint 2010 Requirements 708

Access Services on SharePoint Server 709

SharePoint Features in Access 2010 709Access Features in SharePoint 2010 710

Linked Tables to SharePoint 724Migrating a Database to SharePoint 731Publishing a Database to SharePoint 736

SharePoint 2.0 Shows Access Features 740

Importing from SharePoint 748Access Views on SharePoint 752

Summary 756

CHAPTER 20: WORKING WITH NET 757

Getting Visual Studio 2010 759Installing Visual Studio 2010 760

Writing Code in Visual Studio 2010 762Debugging Code in Visual Studio 2010 769

Types of NET Applications 776Building Client Applications 776Building Web Applications 779Other Methods of Using Access Databases 781

Trang 30

Automating Access with NET 782

Creating Code to Automate Access 783Running the Automated Application 785

The Benefi ts of COM Add-Ins 786Creating a New COM Add-In Project 786Setting References to the Access PIA 787Adding Custom Code to the Add-In 788Installing the COM Add-In 791

Creating a Managed Library in NET 791Calling a Managed Library from VBA 796

Installing the Sample Database 802Choosing the Correct File Format 803

Creating a DSN via Access 805

Using ACE with ODBC Data Sources 807Increasing ODBC Performance 810

Using ADPs to Link to Data 814Query Options on SQL Server 816

Sharing Application Files 819

Controlling Login for ACCDB/MDB Files 820Controlling the Login Process for ADPs 823

Trang 31

Binding ADO Recordsets 826

Binding to a Form, ComboBox, or ListBox 826

Using Persisted Recordsets 830

When to Use Unbound Forms 832

Summary 840

CHAPTER 22: THE ACCESS 2010 TEMPLATES 841

Standalone Database Templates 842Access Web Application Templates 843Templates for SharePoint Applications 844

Features Not Supported in ACCDT Files 846

The Template’s Root Folder 851

Summary 860

CHAPTER 23: ACCESS RUNTIME DEPLOYMENT 861

Why Use the Access Runtime? 862Access 2010 Runtime Versions 862Getting the Access 2010 Runtime 862

Manual Installation of the Runtime 865The Package Solution Wizard 866Using the Package Solution Wizard 866

Summary 874

Trang 32

CHAPTER 24: DATABASE SECURITY 875

Shared-Level Security for ACCDBs 877Securing VBA Code in ACCDB 883

Summary 917

CHAPTER 25: ACCESS 2010 SECURITY FEATURES 919

What Is the Trust Center? 920

Types of Digital Certifi cates 935

Sandbox Mode in Access 2010 942

Workarounds 943

Summary 944

Trang 33

APPENDIX A: THE ACCESS OBJECT MODEL 945

APPENDIX B: DAO OBJECT METHOD AND

APPENDIX C: ADO OBJECT MODEL REFERENCE 1035

APPENDIX D: 64-BIT ACCESS 1095

APPENDIX E: REFERENCES FOR PROJECTS 1103

APPENDIX F: RESERVED WORDS AND SPECIAL CHARACTERS 1113

APPENDIX G: NAMING CONVENTIONS 1127

APPENDIX H: THE ACCESS SOURCE CODE CONTROL 1137

APPENDIX I: TIPS AND TRICKS 1145

INDEX 1191

Trang 35

Welcome to Access 2010 Programmer’s Reference, your guide to the new features and opportunities

that Access 2010 has to offer With this release, Access has expanded its reach beyond the desktop and network to join the ranks in the cloud In support of this new level of connectivity, you will fi nd signifi cant changes within the developer environment that require more decisions, different pro-gramming tools, and more options With changes of this magnitude, you will want to leverage com-munity resources to get up-to-speed quickly so that you are working smarter and more effi ciently That’s where this book comes in

What makes this book so special? In addition to the extensive coverage of subject matter, it is the unparalleled team of authors and tech editors who are as devoted to helping fellow developers as they are passionate about Access Jerry and Teresa have both earned Access MVP status in recogni-tion of their expertise and contributions to the Access community, and Rob and Geoff have worked

on the Microsoft Access test team They are joined by a team of fi ve technical editors who are all Access MVPs; they are not only technical experts but also seasoned consultants and leading-edge developers Their areas of focus and expertise cover the spectrum from VBA to macros, and from simple solutions to enterprise deployments to Web apps, and all areas in between

Access 2010 builds on many of the new features that were introduced in Access 2007 Features such

as templates, application parts, and Offi ce themes will help you leverage existing resources to quickly create and tailor a custom application To support the expanding use of macros, we have a new Macro Designer that makes it faster and easier to create and use macros With their new functionality, you will want to learn how to utilize UI and Data macros in local solutions, not just Web

applications

For many of us, being able to take advantage of the new features, right out-of-the-box, is more than enough reason to upgrade So although the primary focus of this book is to help you extend the power of Access, we will also discuss the new features of Access 2010 Because many of you are familiar with prior versions of Access, we also point out some of the major changes, particularly if they affect the way that you will be working

The goal is for Access 2010 Programmer’s Reference to be your primary resource and tool to

help you leverage Access’s built-in functionality along with VBA and other tools in a manner that helps you to create solid, robust applications Access 2010 makes it easy to start working as soon as it’s installed With the new UI (user interface), people will be building complex applications using the tools and resources that ship with Access And, with a little outside guidance, they can work a lot smarter, with more confi dence, and avoid several pitfalls

This book is for the typical Access user as well as the seasoned programmer It will help you utilize the power of Microsoft Access more effectively and help you choose when to let the wizards do the work, as well as showing you how to modify and enhance the code that the wizards create Access builds great forms and reports that can be customized on-the-fl y by using VBA code to respond to

Trang 36

a multitude of events Interactive reports, Web apps, and new options for distributing applications

exemplify the power and potential of Access

With all the templates, macros, and built-in features, it is easier than ever to open the program and

begin creating tables, forms, and reports with confi dence When you consider how easy it is to get

started, you’ll realize that it is doubly important to be working smart and in the right direction Use

this book and its online resources as your guide to better programming and more effective solutions

WHY USE VBA?

Microsoft Visual Basic for Applications (VBA) enables programmers to develop highly customized

desktop applications that integrate with a variety of Microsoft and non-Microsoft programs For

example, all of the Microsoft Offi ce System products support VBA In addition, many third-party

programs, such as accounting software packages, mapping software, and drafting programs also

support VBA Those companies often provide an integration tool, or SDK (software development

kit), which typically requires VB or VBA to work with it

VBA is actually a subset of the Visual Basic programming language and is a superset of VB Script

(another member of the Visual Basic family of development tools) VBA includes a robust suite

of programming tools based on the Visual Basic development, arguably the world’s most popular

rapid application development system for desktop solutions Developers can add code to tailor

any VBA-enabled application to their specifi c business processes Whether you are starting with a

blank database or building on a template, you can build complex custom solutions For example,

a construction company can use VBA within Microsoft Access to develop a sophisticated system

covering estimating, ordering, scheduling, costing, and inventory control The look and operation

of the system can be tailored for each group and it can easily limit what data a person can view or

change

You might wonder why you should develop in VBA rather than Visual Basic or NET, which are also

robust, popular, and capable programming languages Using VBA within Access gives you a couple

of key benefi ts First, you can leverage the built-in Access Object Library, taking full advantage of

a wide variety of Access commands, including executing any command from the Ribbon or custom

toolbar in Access And second, it’s cost effective because VBA is included in all Microsoft Offi ce

System applications Other languages and frameworks, such as NET, require a separate tool such as

Visual Studio and thus a purchase and license And although they can be cost-effective tools if they

are needed, they can also be an unnecessary burden on a project — both in time and money But do

not fear, the book discusses leveraging the power of NET framework as well

Access now includes another tool that is often an alternative to VBA — the new Macro

Designer — so we have a chapter dedicated to a discussion of how to create and use macros If

you’ve worked with macros in other Offi ce programs, you will be a step ahead That is one of the

benefi ts of working with tools that are an intrinsic part of Microsoft Offi ce If you learn how to

program using VBA and macros in Access, your code and skills are transferable to other programs

Even if your primary focus is on Access, knowing VBA will make it much easier to integrate your

solution with other applications

Trang 37

WHAT DOES THIS BOOK COVER?

Access 2010 Programmer’s Reference covers the key topics of greatest importance to Access developers

Although the premise is that most readers will have some familiarity with the VBA programming language, the book includes a brief introduction to VBA and to macros And to help you leverage the new tools that Access provides, it also has a chapter that highlights the new features in Microsoft Offi ce Access 2010 And, because many people will be faced with upgrading and converting programs from prior versions, a chapter is dedicated to discussing some of the key factors and issues associated with upgrading and running Access in mixed environments

To help you build a strong foundation on the fundamentals for programming in VBA, the book discusses how to create and name variables, how to use Data Access Object (DAO) and ActiveX Data Object (ADO) to manipulate data both within Access and within other applications, proper error-handling techniques, and advanced functions, such as creating classes and using APIs Key new areas, such as using macros, customizing the Ribbon, and working with Offi ce Backstage are also explored

Once the basics are covered, the focus turns to forms and reports, the two most powerful tools for working with and displaying data Several examples demonstrate how all of the pieces fi t together

to create an application that not only meets your immediate needs, but is also easy to maintain and enhance

Working with other applications is covered extensively both in general, and more specifi cally for Microsoft Offi ce applications, Windows SharePoint Services, Access Services, and SQL Server Of course, this book wouldn’t be complete without discussing security issues And, to supplement the chapters, it has a full compliment of appendixes that put helpful reference material conveniently at your fi ngertips

The Chapters

Chapters 1–6 provide material that you need if you’re new to Access, VBA, or macros We start with an introduction to Access and then provide a review of Access 2010’s new features To help you make informed decisions about converting and upgrading, we’ll discuss some of the major factors, processes, and options After that, you learn about the 2010 macros, how to use them, and how they compare with using VBA Chapters 5 and 6 discuss the VBA language, VBA Editor, and the build-ing blocks of VBA, including variables, procedures, events, and properties

The next two chapters are devoted to building a strong foundation on which you will build strong, versatile applications Chapter 7 emphasizes the dynamics of an Access application — and the need

to control what happens to the data and when In this chapter, you will learn how do to that with

a variety of tools and techniques, including using events, procedures, expressions, and Recordsets

In Chapter 8, we delve a bit deeper and examine VBA classes and class objects You’ll learn how to create, name, and use classes and objects

The next step is working toward program integrations Chapter 9 shows you how to use tion programming interface (API) calls to work with and leverage libraries and objects from other

Trang 38

applica-sources, such as using Windows APIs That leads you to the Win32 Registry In Chapter 10, you

will learn how the Registry is structured and how it works Then we show you how to write basic

VBA code to create, retrieve, edit, and delete Registry values

Chapters 11 and 12 focus on using VBA to access data Both DAO and ADO provide methods for

accessing and working with data in Microsoft Access and other external data sources, which can

be relational, such as Informix, SQL Server, or other RDBMSs (relational database management

systems), as well as nonrelational, such as text fi les, spreadsheets, and XML, and a multitude of

other possible sources including numerous accounting programs

In Chapters 13 to 17, you will begin to use VBA within an Access application — or at least in

dem-onstrations You start by learning the structure for writing SQL statements, and we explain some

critical techniques and nuances, such as the difference between using single and double quotes

As we focus on forms and reports, we’ll provide real-world examples and techniques that you can

incorporate directly into your solutions From there, we’ll move into more specialized areas, such

as customizing the Ribbon and working with the new Offi ce Backstage and manipulating XML for

both Ribbon and Offi ce Backstage

Chapters 18–21 provide information about working with other Offi ce applications, such as Word,

Excel, Outlook, and PowerPoint In addition to learning about the new Web applications, you’ll

also discover some new techniques for deploying database solutions and for sharing and securing

data using SharePoint We’ll expand the discussion to focus on programming with NET to

auto-mate Access 2010 and working with Visual Studio We wrap up with a chapter on client-server

development

The fi nal chapters discuss ways to create and deploy applications and some of the security options

Templates and application parts can provide an invaluable tool for quickly creating a solution

Chapter 22 explains how to fi nd, create, and use templates and application parts As you may have

experienced, not everyone that wants to use your application has a copy of Microsoft Access, but

that need not preclude them from using your solution In Chapter 23, you learn how the Access

Runtime enables developers to deploy solutions in a custom package that allows their application to

run even though a complete version of Access is not installed on the machine The last two chapters,

24 and 25, discuss database security and Access security features You’ll review options for locking

the VBA project, using ACCDE fi les, and employing data encryption With Access security, you’ll

learn about the trust center, disabled mode and macros, digital certifi cates, the sandbox mode, and

some workarounds

Although that brings you to the end of the chapters, it is far from the end of the book There are still

nine appendixes fi lled with reference material

The Appendixes

As a developer, you can often spend hours going from source to source looking for reference

mate-rial The authors of this book have applied the principles of relational databases (doing the work

once so it can be used many times in multiple ways) to the appendixes, and have created a

compila-tion of data from a myriad of sources

Trang 39

The fi rst three appendixes document the Object Models for Access, DAO and ADO In dix D, you will fi nd a discussion on working with 64-bit Access As mentioned in the discussions

Appen-on upgrading in Chapter 3, there are signifi cant issues to cAppen-onsider and specifi c steps to take if you are migrating to a 64-bit version of Access, particularly if you will be integrating with 32-bit applications

Appendix E discusses references and is an invaluable resource when you are converting from or to different versions of Access or moving an application from one platform or environment to another The next three appendixes, F, G, and H, are about good practices, such as avoiding reserved words and special characters, implementing good naming conventions, and having a way to manage code, such as by using Source Code Control

Appendix I, is like the extra topping on the cake We share tips and techniques that will help you design polished and professional applications We demonstrate the techniques and provide a work-ing application so it can become part of your developer toolbox In addition to our demo application,

we also have tips and fi les from four of our esteemed colleagues, Dane Miller, UtterAccess VIP and database consultant; Garry Robinson, Access MVP and editor of www.vb123.com/kb; Steve Schapel, Access MVP; and Larry Strange, with www.AccessHosting.com

HOW TO USE THIS BOOK

The initial chapters provide background information to help you build a strong foundation They are written in a tutorial format with detailed examples Subsequent chapters delve deeper into tech-nical areas and demonstrate numerous techniques that will help you build robust solutions Most chapters refer to online fi les that provide the code snippets and help demonstrate the scenarios True

to the Wrox Programmer’s Reference format, the book includes numerous reference appendixes

with details on the various Object Models, as well as guidelines and best practices that will help you

to work with Access and to write VBA code

Real-world examples are given for many, if not most, of the programming topics covered in this book These are just of few of the topics and examples that are included:

‰ Creating intuitive, user-friendly forms to quickly fi nd and work with data, and minimizing data entry errors

‰ Creating custom reports based on information entered on a form

‰ Leveraging report browse, the interactive report feature that enables drilling into data on reports

‰ Summarizing and graphically displaying data using cross-tab reports

‰ Using VBA to transfer data between Access and other Offi ce programs such as Outlook, Word, and Excel

Trang 40

‰ Confi guring custom Ribbons, toolbars, and menus for your Access database applications

‰ Creating custom Web apps and leveraging the features offered through SharePoint and

Access Services

Throughout the book, we’ve also included tips and tricks discovered during the authors’

program-ming experiences

We recommend that as you go through the book, you also work with the code and sample databases

provided in the companion download fi les on Wrox.com Being able to run the code, see the effect

of minor changes, and experiment on your own will reinforce the learning process (See the “Source

Code” section later in this Introduction for details on downloading the code.) By working with the

code and examples, you will begin to take ownership of the concepts and learn how to incorporate

them into your work and solutions

OTHER ACCESS/VBA SOURCES

You’ve heard the saying that there are as many ways to build a solution as there are programmers

And that may only be a slight exaggeration So, although this book is an excellent reference for

all of your Access 2010 programming needs, there just isn’t enough time and ink to cover

every-thing — not to mention fi xes, updates, and add-ons That’s where networking, newsgroups, and

other information sites come in Here are some of the authors’ favorites for you to check out:

Microsoft Answers: Microsoft provides a landing place for users to ask

ques-tions about their products, and Access is one of several products that are covered

at the forums To quickly get to the Access questions and answers, start with this

URL: http://social.answers.microsoft.com:80/Forums/en-US/addbuz/

threads?filter=mf%3a6edab96e-a4cc-4ccb-ae44-7bcdb5ffa589

The newsgroup comp.databases.ms-access: An active community that can be accessed

using an NNTP newsreader as well as by using groups.google.com If you do not have

NNTP access, free NNTP servers are available — for example, eternal-september.org

Microsoft’s Community website: At (discussion groups) www.microsoft.com/office/

community/en-us/default.mspx

MVPS.ORG (www.mvps.org/access): Your jumping-off point to a number of

interesting offerings provided by a few folks associated with the Microsoft Most Valuable

Professional (MVP) program

Microsoft Offi ce Online — Access (http://office.microsoft.com/en-us/

FX010857911033.aspx): Provides quick tips, and direct links to Access resources such as

downloads, templates, training, add-ins, and other pertinent information

Utter Access (www.utteraccess.com): Currently the leading independent forum for

Micro-soft Access questions and solutions

Ngày đăng: 08/03/2014, 17:20

TỪ KHÓA LIÊN QUAN

w