Tải bản đầy đủ

Microsoft Access 2003_ Inside out


PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2004 by John L. Viescas
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.
Library of Congress Cataloging-in-Publication Data
Viescas, John, 1947-
Microsoft Office Access 2003 Inside Out / John L. Viescas.
p. cm.
Includes index.
ISBN 0-7356-1513-6
1. Database management. 2. Microsoft Access. I. Title.
QA76.9.D3V545 2003
005.75'65--dc21 2003056160
Printed and bound in the United States of America.
1 2 3 4 5 6 7 8 9 QWT 8 7 6 5 4 3
Distributed in Canada by H.B. Fenn and Company Ltd.

A CIP catalogue record for this book is available from the British Library.
Microsoft Press books are available through booksellers and distributors worldwide. For further informa-
tion about international editions, contact your local Microsoft Corporation office or contact Microsoft
Press International directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress.
Send comments to mspinput@microsoft.com.
ActiveX, FoxPro, FrontPage, InfoPath, Microsoft, Microsoft Internet Explorer (logo only), Microsoft
Press, MSDN, MS-DOS, MSN, the Office logo, OpenType, Outlook, PivotChart, PivotTable, PowerPoint,
SharePoint, Visual Basic, Visual C# , Visual C++ , Visual FoxPro, Visual J#, Visual Studio, the
Microsoft Visual Tools eMbedded logo, Windows, and Windows NT are either registered trademarks or
trademarks of Microsoft Corporation in the United States and/or other countries. Other product and
company names mentioned herein may be the trademarks of their respective owners.
The example companies, organizations, products, domain names, e-mail addresses, logos, people, places,
and events depicted herein are fictitious. No association with any real company, organization, product,
domain name, e-mail address, logo, person, place, or event is intended or should be inferred.
Acquisitions Editor: Alex Blanton
Project Editor: Sandra Haynes
Series Editor: Sandra Haynes
Technical Editor: Curt Philips
Body Part No. X09-71422
For my bestest pal in the whole wide world. Without your love and
support, we wouldn’t have made this project happen.
And for mom. I finished this book just in time for your 90
th
birthday. I
hope I’ve made you proud.

v
Contents at a Glance
Part 1
Understanding Microsoft
Access
Chapter 1
What Is Microsoft Access? . . . . . . . 3
Chapter 2
The Many Faces of
Microsoft Access . . . . . . . . . . . . . 17
Chapter 3
Designing Your Database
Application. . . . . . . . . . . . . . . . . . 53
Part 2
Building a Microsoft Access
Desktop Application
Chapter 4
Creating Your Database
and Tables . . . . . . . . . . . . . . . . . . 85
Chapter 5
Modifying Your Table Design . . . . 139
Chapter 6
Importing and Linking Data . . . . . 179
Chapter 7
Creating and Working
with Simple Queries . . . . . . . . . . 219
Chapter 8
Building Complex Queries . . . . . . 277
Chapter 9
Modifying Data with
Action Queries . . . . . . . . . . . . . . 337
Part 3
Creating Forms and Reports
in a Desktop Application
Chapter 10
Using Forms . . . . . . . . . . . . . . . 365
Chapter 11
Building a Form . . . . . . . . . . . . . 397
Chapter 12
Customizing a Form . . . . . . . . . . 433
Chapter 13
Advanced Form Design. . . . . . . . 481
Chapter 14
Using Reports . . . . . . . . . . . . . . 523
Chapter 15
Constructing a Report . . . . . . . . 537
Chapter 16
Advanced Report Design . . . . . . 555
Part 4
Designing an
Access Project
Chapter 17
Building Tables in an
Access Project . . . . . . . . . . . . . 599
Chapter 18
Building Queries in an
Access Project . . . . . . . . . . . . . 637
Chapter 19
Designing Forms in an
Access Project . . . . . . . . . . . . . 685
Chapter 20
Building Reports in an
Access Project . . . . . . . . . . . . . 701
Contents at a Glance
vi
Part 5
Automating an Access
Application
Chapter 21
Understanding Event
Processing . . . . . . . . . . . . . . . . . 713
Chapter 22
Understanding Visual
Basic Fundamentals . . . . . . . . . . 757
Chapter 23
Automating Your Application
with Visual Basic . . . . . . . . . . . . 857
Chapter 24
The Finishing Touches . . . . . . . . 927
Part 6
Linking Access and
the Web
Chapter 25
Publishing Data on the Web . . . . 961
Chapter 26
Creating Static and
Dynamic Web Pages . . . . . . . . . . 985
Chapter 27
Building Data Access Pages . . . 1015
Chapter 28
Working with XML
and SharePoint . . . . . . . . . . . . . 1095
Part 7
After Completing Your
Application
Chapter 29
Upsizing a Desktop
Application to a Project . . . . . . 1133
Chapter 30
Securing Your Database . . . . . . 1163
Chapter 31
Distributing Your Application . . 1203
Part 8
Appendix
Appendix
Installing Microsoft Office . . . . 1223
Part 9
Articles
Article 1
Understanding SQL . . . . . . . . . . . A1
Article 2
Exporting Data . . . . . . . . . . . . . A45
Article 3
Visual Basic Function
Reference . . . . . . . . . . . . . . . . . A49
Article 4
Internet Explorer Web Page
Color Names . . . . . . . . . . . . . . . A59
vii
Table of Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
We’d Like to Hear from You! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
About the CD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvii
What’s on the CD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Sample Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxviii
Using the CD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxixi
System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Support Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Conventions and Features Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . xxxi
Text Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi
Design Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi
Syntax Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxv
Microsoft Access Today . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvi
About This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvii
Part 1
Understanding Microsoft Access
Chapter 1
What Is Microsoft Access? 3
What Is a Database? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Database Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Microsoft Access as an RDBMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Data Definition and Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Data Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Data Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Microsoft Access as an Application Development System . . . . . . . . . . . . . 11
Deciding to Move to Database Software. . . . . . . . . . . . . . . . . . . . . . . . . . 13
Chapter 2
The Many Faces of Microsoft Access 17
The Architecture of Microsoft Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Exploring a Desktop Database—Housing Reservations . . . . . . . . . . . . . . . 19
Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Data Access Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Table of Contents
viii
Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Organizing Your Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Exploring a Project File—LawTrack Contacts . . . . . . . . . . . . . . . . . . . . . . . 48
Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Views, Functions, and Stored Procedures. . . . . . . . . . . . . . . . . . . . . 50
The Many Faces of Microsoft Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Chapter 3
Designing Your Database Application 53
Application Design Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Step 1: Identifying Tasks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Step 2: Charting Task Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Step 3: Identifying Data Elements . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Step 4: Organizing the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Step 5: Designing a Prototype and a User Interface . . . . . . . . . . . . . 55
Step 6: Constructing the Application . . . . . . . . . . . . . . . . . . . . . . . . 55
Step 7: Testing, Reviewing, and Refining . . . . . . . . . . . . . . . . . . . . . 56
An Application Design Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Analyzing the Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Selecting the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Organizing Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Data Analysis. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Choosing the Database Subjects. . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Mapping Subjects to Your Database . . . . . . . . . . . . . . . . . . . . . . . . 66
Database Design Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Waste Is the Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Normalization Is the Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Efficient Relationships Are the Result . . . . . . . . . . . . . . . . . . . . . . . 77
When to Break the Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Improving Performance of Critical Tasks . . . . . . . . . . . . . . . . . . . . . . 79
Capturing Point-In-Time Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Creating Report Snapshot Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Part 2
Building a Microsoft Access Desktop Application
Chapter 4
Creating Your Database and Tables 85
Creating a New Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Using a Database Template to Create a Database . . . . . . . . . . . . . . 86
Creating a New Empty Database . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Creating Your First Simple Table by Entering Data . . . . . . . . . . . . . . . . . . . 92
Creating a Table Using the Table Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Creating a Table in Design View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
ix
Table of Contents
Defining Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Understanding Field Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Setting Field Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Completing the Fields in the Companies Table . . . . . . . . . . . . . . . . 110
Defining Simple Field Validation Rules . . . . . . . . . . . . . . . . . . . . . . 111
Defining Input Masks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Defining a Primary Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Defining a Table Validation Rule. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Understanding Other Table Properties . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Defining Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Defining Your First Relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Creating a Relationship on Multiple Fields . . . . . . . . . . . . . . . . . . . 128
Adding Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Single Field Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Multiple-Field Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Setting Table Design Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Printing a Table Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Database Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Chapter 5
Modifying Your Table Design 139
Before You Get Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Making a Backup Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Checking Object Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Deleting Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Renaming Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Changing Field Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Moving Fields. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Inserting Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Copying Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Deleting Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Changing Data Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Changing Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Changing Data Lengths. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Dealing with Conversion Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Changing Other Field Properties . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Reversing Changes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Using the Table Analyzer Wizard. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Taking a Look at Lookup Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Changing the Primary Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Compacting Your Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Chapter 6
Importing and Linking Data 179
A Word About Open Database Connectivity (ODBC) . . . . . . . . . . . . . . . . . 179
Importing vs. Linking Database Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Table of Contents
x
Importing Data and Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Importing dBASE Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Importing Paradox Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Importing SQL Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Importing Access Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Importing Spreadsheet Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Preparing a Spreadsheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Importing a Spreadsheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Fixing Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Importing Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Preparing a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Importing a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Fixing Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Modifying Imported Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Linking Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Security Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Performance Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Linking Access Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Linking dBASE and Paradox Files . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Linking Text and Spreadsheet Files . . . . . . . . . . . . . . . . . . . . . . . . 213
Linking SQL Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Modifying Linked Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Unlinking Linked Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Using the Linked Table Manager . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Chapter 7
Creating and Working with Simple Queries 219
Selecting Data from a Single Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Specifying Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Setting Field Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Entering Selection Criteria. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Using Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Using the Expression Builder. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Specifying Field Names. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Sorting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Testing Validation Rule Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Checking a New Field Validation Rule. . . . . . . . . . . . . . . . . . . . . . . 249
Checking a New Table Validation Rule . . . . . . . . . . . . . . . . . . . . . . 250
Working in Query Datasheet View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Moving Around and Using Keyboard Shortcuts . . . . . . . . . . . . . . . . 252
Working with Subdatasheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Changing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Working with Hyperlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Sorting and Searching for Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
xi
Table of Contents
Chapter 8
Building Complex Queries 277
Selecting Data from Multiple Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Creating Inner Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Building a Query on a Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Using Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Using a Query Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Summarizing Information with Totals Queries . . . . . . . . . . . . . . . . . . . . . 296
Totals Within Groups. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Selecting Records to Form Groups. . . . . . . . . . . . . . . . . . . . . . . . . 301
Selecting Specific Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Building Crosstab Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Partitioning Data in a Crosstab Query . . . . . . . . . . . . . . . . . . . . . . 306
Using Query Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Customizing Query Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Controlling Query Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Working with Unique Records and Values . . . . . . . . . . . . . . . . . . . . 312
Defining a Subdatasheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Other Query Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Editing and Creating Queries in SQL View. . . . . . . . . . . . . . . . . . . . . . . . 319
Limitations on Using Select Queries to Update Data . . . . . . . . . . . . . . . . 323
Creating PivotTables and PivotCharts from Queries . . . . . . . . . . . . . . . . . 324
Building a Query for a PivotTable . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Designing a PivotTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Designing a PivotChart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Chapter 9
Modifying Data with Action Queries 337
Updating Groups of Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Testing with a Select Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Converting a Select Query to an Update Query . . . . . . . . . . . . . . . . 339
Running an Update Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Updating Multiple Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Creating a New Table with a Make-Table Query . . . . . . . . . . . . . . . . . . . . 346
Creating a Make-Table Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Running a Make-Table Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Inserting Data from Another Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Creating an Append Query. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Running an Append Query. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Deleting Groups of Rows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Testing with a Select Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Using a Delete Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Deleting Inactive Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Troubleshooting Action Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Solving Common Action Query Errors and Problems . . . . . . . . . . . . 360
Looking at an Error Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Table of Contents
xii
Part 3
Creating Forms and Reports in a Desktop Application
Chapter 10
Using Forms 365
Uses of Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
A Tour of Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Headers, Detail Sections, and Footers. . . . . . . . . . . . . . . . . . . . . . 366
Multiple-Page Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Continuous Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Subforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Pop-Up Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Modal Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Special Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
PivotTables and PivotCharts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Moving Around on Forms and Working with Data . . . . . . . . . . . . . . . . . . . 380
Viewing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Adding Records and Changing Data . . . . . . . . . . . . . . . . . . . . . . . . 384
Searching for and Sorting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Performing a Simple Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Performing a Quick Sort on a Form Field . . . . . . . . . . . . . . . . . . . . 392
Adding a Filter to a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Printing Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Chapter 11
Building a Form 397
Forms and Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . 397
Starting from Scratch—A Simple Input Form. . . . . . . . . . . . . . . . . . . . . . 400
Building a New Form with Design Tools . . . . . . . . . . . . . . . . . . . . . 400
Building a Simple Input Form for the tblCompanies Table. . . . . . . . . 411
Customizing Colors and Checking Your Design Results . . . . . . . . . . 418
Working with Form Wizards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Creating the Basic Products Form with a Form Wizard . . . . . . . . . . . 420
Modifying the Products Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Simplifying Data Input with a Form. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Taking Advantage of Combo Boxes and List Boxes . . . . . . . . . . . . . 426
Using Toggle Buttons, Check Boxes, and Option Buttons . . . . . . . . . 430
Chapter 12
Customizing a Form 433
Aligning and Sizing Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Sizing Controls to Fit Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Adjusting Control Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
“Snapping” Controls to the Grid . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Lining Up Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
xiii
Table of Contents
Enhancing the Look of a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Lines and Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Colors and Special Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Fonts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Setting Control Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Formatting Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Adding a Scroll Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Enabling and Locking Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Setting the Tab Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Adding a Smart Tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Understanding Other Control Properties . . . . . . . . . . . . . . . . . . . . . 465
Setting Form Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Allowing Different Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Setting Navigation Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Defining a Pop-Up and/or Modal Form . . . . . . . . . . . . . . . . . . . . . . 471
Controlling Edits, Deletions, Additions, and Filtering . . . . . . . . . . . . 472
Defining Window Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Setting the Border Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Understanding Other Form Properties . . . . . . . . . . . . . . . . . . . . . . 474
Setting Form and Control Defaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Changing Control Defaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Working with AutoFormat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
Defining a Template Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Chapter 13
Advanced Form Design 481
Basing a Form on a Multiple-Table Query . . . . . . . . . . . . . . . . . . . . . . . . 482
Creating a Many-to-One Form. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Creating and Embedding Subforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Specifying the Subform Source . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Designing the Innermost Subform . . . . . . . . . . . . . . . . . . . . . . . . . 490
Designing the First Level Subform . . . . . . . . . . . . . . . . . . . . . . . . . 494
Embedding a Subform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Specifying the Main Form Source. . . . . . . . . . . . . . . . . . . . . . . . . . 499
Creating the Main Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Creating a Subdatasheet Subform . . . . . . . . . . . . . . . . . . . . . . . . . 502
Displaying Values in an Option Group. . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Using Conditional Formatting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
Working with the Tab Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
Creating Multiple-Page Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Introducing ActiveX Controls—The Calendar Control . . . . . . . . . . . . . . . . 515
Working with PivotChart Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Building a PivotChart Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Embedding a Linked PivotChart . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
Table of Contents
xiv
Chapter 14
Using Reports 523
Uses of Reports. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
A Tour of Reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Print Preview—A First Look . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Headers, Detail Sections, Footers, and Groups . . . . . . . . . . . . . . . 526
Subreports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
Objects in Reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
Printing Reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Print Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Chapter 15
Constructing a Report 537
Starting from Scratch—A Simple Report . . . . . . . . . . . . . . . . . . . . . . . . 537
Building the Report Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Designing the Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Sorting and Grouping Information . . . . . . . . . . . . . . . . . . . . . . . . . 541
Completing the Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Using a Report Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Selecting a Report Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Specifying Wizard Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
Viewing the Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
Chapter 16
Advanced Report Design 555
Building a Query for a Complex Report. . . . . . . . . . . . . . . . . . . . . . . . . . 556
Creating the Basic Facility Occupancy by Date Report . . . . . . . . . . . . . . . 557
Defining the Sorting and Grouping Criteria . . . . . . . . . . . . . . . . . . . . . . . 559
Setting Section and Report Properties . . . . . . . . . . . . . . . . . . . . . . . . . . 562
Section Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
Report Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Using Calculated Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Adding the Print Date and Page Numbers . . . . . . . . . . . . . . . . . . . . 572
Performing Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Hiding Redundant Values and Concatenating Text Strings . . . . . . . . 578
Calculating Percentages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
Using Running Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Taking Advantage of Conditional Formatting . . . . . . . . . . . . . . . . . . 584
Creating and Embedding a Subreport. . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Understanding Subreport Challenges . . . . . . . . . . . . . . . . . . . . . . . 586
Building a Report with a Subreport . . . . . . . . . . . . . . . . . . . . . . . . 589
Adding a PivotChart to a Report. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
Designing the PivotChart Form . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
Embedding a PivotChart in a Report . . . . . . . . . . . . . . . . . . . . . . . 593
xv
Table of Contents
Part 4
Designing an Access Project
Chapter 17
Building Tables in an Access Project 599
Creating a New Project File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
Building a New SQL Server Database. . . . . . . . . . . . . . . . . . . . . . . 600
Connecting to an Existing SQL Server Database . . . . . . . . . . . . . . . 603
Creating a Table in Design View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Defining Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
Column Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
Completing the Columns in the Companies Table . . . . . . . . . . . . . . 614
Understanding Column Properties . . . . . . . . . . . . . . . . . . . . . . . . . 616
Defining a Primary Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
Adding Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
Creating Additional Tables in Contact Tracking . . . . . . . . . . . . . . . . . . . . 623
Defining Check Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Creating Additional Constraint Expressions . . . . . . . . . . . . . . . . . . 628
Defining Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
Defining Relationships in Table Design View . . . . . . . . . . . . . . . . . . 629
Defining Relationships Using Database Diagrams . . . . . . . . . . . . . . 632
Setting Table Design Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
Chapter 18
Building Queries in an Access Project 637
Building Queries Using the Query Designer. . . . . . . . . . . . . . . . . . . . . . . 640
Understanding the Query Designer . . . . . . . . . . . . . . . . . . . . . . . . 640
Working with Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
Working with In-Line Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
Working with Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . 661
Building Queries Using a Text Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
Building a Text Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . 667
Building a Text Scalar Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
Building a Text Table-Valued Function . . . . . . . . . . . . . . . . . . . . . . . 683
Chapter 19
Designing Forms in an Access Project 685
Understanding Form Differences in an Access Project . . . . . . . . . . . . . . . 685
Choosing Option Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
Setting Project Form Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
Setting Recordset Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692
Understanding Max Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692
Working with Server Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
Setting Input Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
Table of Contents
xvi
Chapter 20
Building Reports in an Access Project 701
Understanding Report Differences in an Access Project. . . . . . . . . . . . . . 701
Setting Project Report Properties. . . . . . . . . . . . . . . . . . . . . . . . . . 702
Working with Server Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
Working with Input Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705
Part 5
Automating an Access Application
Chapter 21
Understanding Event Processing 713
Access as a Windows Event-Driven Application . . . . . . . . . . . . . . . . . . . . 713
Understanding Events in Windows . . . . . . . . . . . . . . . . . . . . . . . . . 713
Leveraging Access Events to Build an Application . . . . . . . . . . . . . . 714
Summary of Form and Report Events. . . . . . . . . . . . . . . . . . . . . . . . . . . 715
Opening and Closing Forms and Reports . . . . . . . . . . . . . . . . . . . . 716
Changing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
Detecting Focus Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
Detecting Filters Applied to Forms . . . . . . . . . . . . . . . . . . . . . . . . . 722
Trapping Keyboard and Mouse Events . . . . . . . . . . . . . . . . . . . . . . 722
Detecting Changes in PivotTables and PivotCharts . . . . . . . . . . . . . 725
Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
Trapping Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728
Detecting Timer Expiration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728
Understanding Event Sequence and Form Editing . . . . . . . . . . . . . . . . . . 728
Summary of Macro Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
Opening and Closing Access Objects . . . . . . . . . . . . . . . . . . . . . . . 732
Printing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
Executing a Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
Testing Conditions and Controlling Action Flow . . . . . . . . . . . . . . . . 737
Setting Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
Searching for Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
Building a Custom Menu and Executing Menu Commands . . . . . . . . 740
Controlling Display and Focus . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741
Informing the User of Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
Renaming, Copying, Deleting, Saving, Importing, and
Exporting Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
Running Another Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
The Macro Design Facility—An Overview. . . . . . . . . . . . . . . . . . . . . . . . . 745
Working with the Macro Design Window . . . . . . . . . . . . . . . . . . . . . 745
Defining Multiple Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
Grouping Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753
Converting Your Macros to Visual Basic . . . . . . . . . . . . . . . . . . . . . 755
xvii
Table of Contents
Chapter 22
Understanding Visual Basic Fundamentals 757
The Visual Basic Development Environment . . . . . . . . . . . . . . . . . . . . . . 758
Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758
The Visual Basic Editor Window. . . . . . . . . . . . . . . . . . . . . . . . . . . 760
Working with Visual Basic Debugging Tools. . . . . . . . . . . . . . . . . . . 766
Variables and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774
Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
Variable and Constant Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
Declaring Constants and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778
Const Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778
Dim Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779
Enum Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 782
Event Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783
Private Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784
Public Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786
ReDim Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
Static Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788
Type Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789
Collections, Objects, Properties, and Methods . . . . . . . . . . . . . . . . . . . . 791
The Access Application Architecture. . . . . . . . . . . . . . . . . . . . . . . . 791
The Data Access Objects (DAO) Architecture. . . . . . . . . . . . . . . . . . 793
The ActiveX Data Objects (ADO) Architecture . . . . . . . . . . . . . . . . . 795
Referencing Collections, Objects, and Properties . . . . . . . . . . . . . . 798
Assigning an Object Variable—Set Statement. . . . . . . . . . . . . . . . . 801
Object Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803
Functions and Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812
Function Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812
Sub Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814
Understanding Class Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815
Property Get . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
Property Let . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818
Property Set. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
Controlling the Flow of Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823
Call Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823
Do…Loop Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824
For…Next Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
For Each…Next Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
GoTo Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
If…Then…Else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827
RaiseEvent Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828
Select Case Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829
Stop Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
While…Wend Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
With…End With Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831
Table of Contents
xviii
Running Macro Actions and Menu Commands . . . . . . . . . . . . . . . . . . . . 832
DoCmd Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 832
Executing a Menu Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833
Actions with Visual Basic Equivalents . . . . . . . . . . . . . . . . . . . . . . 834
Trapping Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834
On Error Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834
Some Complex Visual Basic Examples. . . . . . . . . . . . . . . . . . . . . . . . . . 836
A Procedure to Randomly Load Data . . . . . . . . . . . . . . . . . . . . . . . 836
A Procedure to Examine All Error Codes . . . . . . . . . . . . . . . . . . . . . 850
Chapter 23
Automating Your Application with Visual Basic 857
Why Aren’t We Using Macros? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857
When to Use Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
When to Use Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
Assisting Data Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859
Filling In Related Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859
Handling the NotInList Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
Fixing an E-Mail Hyperlink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867
Providing a Graphical Calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . 867
Working with Linked Photos. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872
Validating Complex Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 878
Checking for Possible Duplicate Names . . . . . . . . . . . . . . . . . . . . . 878
Testing for Related Records When Deleting a Record . . . . . . . . . . . 880
Verifying a Prerequisite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881
Maintaining a Special Unique Value . . . . . . . . . . . . . . . . . . . . . . . . 883
Checking for Overlapping Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 884
Controlling Tabbing on a Multiple-Page Form . . . . . . . . . . . . . . . . . . . . . . 886
Automating Data Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 887
Working with a Multiple-Selection List Box . . . . . . . . . . . . . . . . . . . 888
Providing a Custom Query By Form . . . . . . . . . . . . . . . . . . . . . . . . 891
Selecting from a Summary List . . . . . . . . . . . . . . . . . . . . . . . . . . . 898
Filtering One List with Another . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899
Linking to Related Data in Another Form or Report . . . . . . . . . . . . . . . . . 902
Linking Forms Using a Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 902
Linking to a Report Using a Filter. . . . . . . . . . . . . . . . . . . . . . . . . . 903
Synchronizing Two Forms Using a Class Event . . . . . . . . . . . . . . . . 906
Automating Complex Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 909
Triggering a Data Task from a Related Form . . . . . . . . . . . . . . . . . . 909
Linking to a Related Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913
Calculating a Stored Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917
Automating Reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917
Allowing for Used Mailing Labels . . . . . . . . . . . . . . . . . . . . . . . . . . 917
Drawing on a Report. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
Dynamically Filtering a Report When It Opens . . . . . . . . . . . . . . . . . 923
xix
Table of Contents
Chapter 24
The Finishing Touches 927
Creating Custom Menus and Toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . 927
Understanding the Customize Toolbar Facility . . . . . . . . . . . . . . . . . 928
Creating a Custom Form Toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . 931
Creating a Custom Form Menu Bar . . . . . . . . . . . . . . . . . . . . . . . . 936
Creating Custom Shortcut Menus . . . . . . . . . . . . . . . . . . . . . . . . . 940
Setting Form, Form Control, and Report Menu and
Toolbar Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
Fine-Tuning with the Performance Analyzer Wizard . . . . . . . . . . . . . . . . . . 944
Disabling Form Design View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
Defining Switchboard Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947
Designing a Switchboard Form from Scratch. . . . . . . . . . . . . . . . . . 947
Using the Switchboard Manager to Design Switchboard Forms. . . . . 949
Controlling How Your Application Starts and Runs . . . . . . . . . . . . . . . . . . 952
Setting Startup Properties for Your Database . . . . . . . . . . . . . . . . . 952
Starting and Stopping Your Application. . . . . . . . . . . . . . . . . . . . . . 953
Creating an AutoKeys Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956
Performing a Final Visual Basic Compile. . . . . . . . . . . . . . . . . . . . . . . . . 957
Part 6
Linking Access and the Web
Chapter 25
Publishing Data on the Web 961
Working with the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961
Understanding HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 962
Introducing XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969
Maintaining Static Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971
Creating Dynamic Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 973
Delivering Dynamic Query Results . . . . . . . . . . . . . . . . . . . . . . . . . 973
Processing Live Data with HTML Forms . . . . . . . . . . . . . . . . . . . . . 975
Sharing Your Data with SharePoint. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978
Introducing SharePoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978
Office and SharePoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979
Discovering the Possibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979
Viewing Static HTML Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979
Exploring Active Server Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 981
Looking at Data Access Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . 982
Chapter 26
Creating Static and Dynamic Web Pages 985
Creating a Static HTML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985
Improving the Look of Exported Data in HTML. . . . . . . . . . . . . . . . . 988
Generating an HTML Page from an Access Report . . . . . . . . . . . . . 995
Writing HTML from Visual Basic. . . . . . . . . . . . . . . . . . . . . . . . . . . 997
Table of Contents
xx
Creating a Dynamic Active Server Page . . . . . . . . . . . . . . . . . . . . . . . . 1001
Defining a System Data Source . . . . . . . . . . . . . . . . . . . . . . . . . . 1001
Exporting Access Data to an Active Server Page . . . . . . . . . . . . . . 1004
Programming Active Server Pages . . . . . . . . . . . . . . . . . . . . . . . . 1006
Chapter 27
Building Data Access Pages 1015
Introducing Data Access Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1016
Underlying Structure of a Data Access Page . . . . . . . . . . . . . . . . . 1016
Usefulness and Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1017
Types of Pages You Can Create . . . . . . . . . . . . . . . . . . . . . . . . . . 1018
Designing Data Access Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1021
Setting Options for Data Access Pages . . . . . . . . . . . . . . . . . . . . 1021
Creating Pages Using the Wizards . . . . . . . . . . . . . . . . . . . . . . . . 1023
Understanding the Data Access Page Design Facility. . . . . . . . . . . 1028
Modifying a Wizard-Generated Data Access Page . . . . . . . . . . . . . 1038
Generating Data Access Pages from Forms and Reports . . . . . . . . 1047
Creating a Data Access Page in Design View . . . . . . . . . . . . . . . . 1055
Automating Data Access Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1065
Using the Microsoft Script Editor with Data Access Pages . . . . . . . 1065
Creating Scripts for Data Access Pages . . . . . . . . . . . . . . . . . . . . 1068
Updating Data Access Page Links and Connections . . . . . . . . . . . . . . . 1081
Updating Links from Access to Data Access Pages . . . . . . . . . . . . 1081
Updating Connections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1082
Updating Links and Connections from Within Visual Basic . . . . . . . 1088
Uploading Your Pages to a Web Server. . . . . . . . . . . . . . . . . . . . . . . . . 1090
Copying Pages Using the Windows XP My Web Sites . . . . . . . . . . . 1091
Publishing Pages Using FTP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1093
Chapter 28
Working with XML and SharePoint 1095
Exploring XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1095
Well-Formed XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1096
Understanding XML File Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 1097
Using XML in Microsoft Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1105
Exporting and Importing XML from the User Interface . . . . . . . . . . 1105
Importing and Exporting XML in Visual Basic . . . . . . . . . . . . . . . . 1115
Working with SharePoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1119
Using SharePoint from Access. . . . . . . . . . . . . . . . . . . . . . . . . . . 1121
Using Access from SharePoint. . . . . . . . . . . . . . . . . . . . . . . . . . . 1126
xxi
Table of Contents
Part 7
After Completing Your Application
Chapter 29
Upsizing a Desktop Application to a Project 1133
Benefits of the Client/Server Architecture . . . . . . . . . . . . . . . . . . . . . . 1134
Deciding When to Upsize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1135
Deciding on a Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1137
Upsizing Using the Import Facility . . . . . . . . . . . . . . . . . . . . . . . . 1137
Using the Upsizing Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1138
Preparing Your Desktop Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1138
General Upsizing Considerations . . . . . . . . . . . . . . . . . . . . . . . . . 1138
Preparing Your Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1139
Modifying Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1141
Changing Forms and Reports . . . . . . . . . . . . . . . . . . . . . . . . . . . 1142
Modifying Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1143
Using the Upsizing Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1147
Ensuring That Your Server Is Started . . . . . . . . . . . . . . . . . . . . . . 1148
Running the Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1149
Starting Over If the Wizard Fails. . . . . . . . . . . . . . . . . . . . . . . . . . 1154
Correcting the Upsize Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1155
Adding Relationships and Constraints and Fixing Indexes . . . . . . . 1155
Correcting Query Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1156
Fixing Parameters in Forms and Reports . . . . . . . . . . . . . . . . . . . 1159
Fixing Combo Boxes and List Boxes That Use
a Large Row Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1161
Chapter 30
Securing Your Database 1163
Securing a Desktop Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1163
Access Security Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1164
Users, Groups, and Permissions . . . . . . . . . . . . . . . . . . . . . . . . . 1165
Using the Security Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1170
Setting Up Your Secured Database . . . . . . . . . . . . . . . . . . . . . . . 1182
Security Considerations in a Project File. . . . . . . . . . . . . . . . . . . . . . . . 1195
Understanding Macro Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1197
Chapter 31
Distributing Your Application 1203
Using Linked Tables in a Desktop Database . . . . . . . . . . . . . . . . . . . . . 1204
Taking Advantage of the Database Splitter Utility . . . . . . . . . . . . . 1204
Creating Startup Code to Verify and Correct
Linked Table Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1206
Verifying Server Connection in a Project File . . . . . . . . . . . . . . . . . . . . . 1210
Understanding Runtime Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1213
Creating an Execute-Only Database . . . . . . . . . . . . . . . . . . . . . . . . . . . 1214
Table of Contents
xxii
Creating an Application Shortcut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1215
Understanding the Visual Studio Tools for the Microsoft
Office System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1218
Part 8
Appendix
Appendix
Installing Microsoft Office 1223
Installing Microsoft Office . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1224
Choosing Options When You Have No Previous Version of Office . . 1224
Choosing Options to Upgrade a Previous Version of Office . . . . . . . 1228
Installing the Microsoft SQL Server Data Engine (MSDE) . . . . . . . . . . . . 1230
Managing ODBC Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1233
Creating an ODBC System Data Source Name
for Microsoft Access. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1235
Using ODBC for Connecting to Microsoft SQL Server . . . . . . . . . . . 1237
Converting from a Previous Release of Microsoft Access. . . . . . . . . . . . 1241
Conversion Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1242
Creating an Office Toolbar in Windows XP . . . . . . . . . . . . . . . . . . . . . . . 1243
Part 9
Articles
Article 1
Understanding SQL A1
Article 2
Exporting Data A45
Article 3
Visual Basic Function Reference A49
Article 4
Internet Explorer Web Page Color Names A59
Index of Troubleshooting Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1247
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1249
xxiii

Acknowledgments
The folks on the Microsoft Access development team provided invaluable technical support
as I worked with the beta software and tried to figure out some of the challenging technical
details in Microsoft Office Access 2003. Special thanks to Bill Ramos, Mike Wachal, Tim
Getsch, and Clint Covington. You guys make an author’s job so much easier. But any errors
or omissions in this book are ultimately mine.
This book wouldn’t have happened without the outstanding efforts of Sandra Haynes, my
project editor; Curt Phillips, the excellent technical editor on the book; and Andrea Fox, who
kept all my commas in order. Thanks also to the entire production team at Microsoft Press.
Special thanks to Alex Blanton, who talked me into “running the gauntlet” again.
Thanks also to G. L. Sanders, my “partner in crime” at LawTrack, who thought it would be a
neat idea to include our contact and sales tracking database as one of the main examples in
this book. Including a real production database in the book helps readers see the true value of
using Microsoft Access. And last, but certainly not least, thanks to my son, Michael, who
wrote major portions of five of the chapters in this book. He’s really turning into a database
expert in his own right (maybe it’s genetic)—not bad for someone who majored in psychol-
ogy in college.
John Viescas
Austin, Texas
August 2003

xxv
We’d Like to Hear from You!
Our goal at Microsoft Press is to create books that help you find the information you need to
get the most out of your software.
The Inside Out series was created with you in mind. As part of our ongoing effort to ensure
that we’re creating the books that meet your learning needs, we’d like to hear from you. Let us
know what you think. Tell us what you like about this book and what we can do to make it
better. When you write, please include the title and author of this book in your e-mail mes-
sage, as well as your name and contact information. We look forward to hearing from you!
How to Reach Us
E-Mail: nsideout@microsoft.com
Mail: Inside Out Series Editor
Microsoft Press
One Microsoft Way
Redmond, WA 98052
Note: Unfortunately, we can’t provide support for any software problems you might experi-
ence. Please go to http://support.microsoft.com for help with any software issues.

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×

×