559044 FM.qxd 3/30/04 2:42 PM Page iii

Expert One-on-One Microsoft® Access Application Development

Helen Feddema 559044 FM.qxd 3/30/04 2:42 PM Page ii 559044 FM.qxd 3/30/04 2:42 PM Page i

Expert One-on-One Microsoft® Access Application Development 559044 FM.qxd 3/30/04 2:42 PM Page ii 559044 FM.qxd 3/30/04 2:42 PM Page iii

Expert One-on-One Microsoft® Access Application Development

Helen Feddema 559044 FM.qxd 3/30/04 2:42 PM Page iv

Expert One-on-One Microsoft® Access Application Development Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 Copyright  2004 by Helen Feddema. Published by Wiley Publishing, Inc. All rights reserved. Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under Section 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, Inc., 222 Rose- wood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4447, E-mail: [email protected].

LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REP- RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CON- TENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CRE- ATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CON- TAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOT THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FUR- THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFOR- MATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.

For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Trademarks: Wiley, the Wiley Publishing logo, Wrox, the Wrox logo, and Programmer to Programmer are trade- marks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates. Microsoft is a registered trademark of Microsoft Corporation. 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. 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 Cataloging-in-Publication Data: ISBN: 0-7645-5904-4 Feddema, Helen Bell. Expert one-on-one application development / Helen Feddema. p. cm. Includes index. ISBN 0-7645-5904-4 (paper/website) 1. Microsoft Access. 2. management. 3. Computer software—Development. I. Title. QA76.9.D3F4365 2004 005.75’65—dc22 2004001551 Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 1B/RU/QU/QU/IN 559044 FM.qxd 3/30/04 2:42 PM Page v

Credits Vice President and Executive Group Publisher Technical Editor Richard Swadley Mary Hardy

Vice President and Executive Publisher Senior Production Editor Robert Ipsen Fred Bernardi

Vice President and Publisher Development Editor Joseph B. Wikert Emilie Herman

Executive Editorial Director Production Editor Mary Bednarek Pamela Hanley

Executive Editor Project Coordinator Robert M. Elliott Erin Smith

Editorial Manager Text Design & Composition Kathryn A. Malm Wiley Composition Services

Proofreading and Indexing TECHBOOKS Production Services 559044 FM.qxd 3/30/04 2:42 PM Page vi 559044 FM.qxd 3/30/04 2:42 PM Page vii

Dedication

To all the developers who have posted their code on the Internet (and its predecessors) for others to use—you may not have been paid, but your work is appreciated! 559044 FM.qxd 3/30/04 2:42 PM Page viii 559044 FM.qxd 3/30/04 2:42 PM Page ix

Contents

Acknowledgments xv About the Author xvii Introduction xix Part One: Creating an Access Application 1

Chapter 1: Creating a Database for an Application 3 Gathering Data 4 Figuring Out Business Tasks and Objects 5 Determining Your Entities 5 Creating Tables for an Application 7 Creation Methods 8 Creating the Tables 11 Table Field Data Types 16 Normalization 31 First Normal Form: Eliminate Repeating Groups 31 Second Normal Form: Eliminate Redundant Data 31 Third Normal Form: Eliminate Columns Not Dependent on Key 32 Fourth Normal Form: Isolate Independent Multiple Relationships 32 Fifth Normal Form: Isolate Semantically Related Multiple Relationships 33 Setting Up Relationships 33 One-to-Many Relationships 34 One-to-One Relationships 36 Many-to-Many Relationships 37 Summary 38

Chapter 2: Using Forms to Work with Data 39 Form Types 39 Creating the Forms 44 Using the Design Schemes Add-in 44 Creating a Standard Main Form 46 Creating and Embedding Datasheet and Single Form Subforms 57 Creating Continuous Forms 74 Creating a Form for a Many-to-Many Relationship 76 Summary 78 559044 FM.qxd 3/30/04 2:42 PM Page x

Contents

Chapter 3: Selecting the Right Controls for Forms 79 Standard Controls 79 Useful Standard Controls 80 Not-So-Useful Standard Controls 112 Special Controls 112 Tab Control 112 Calendar Control 113 TreeView Control 114 ActiveX Controls 117 DateTimePicker 117 MonthView Control 118 Summary 119

Chapter 4: Sorting and Filtering Data with Queries 121 Select Queries 121 Basic Select Queries 121 Totals Queries 127 Crosstab Queries 132 Action Queries 136 Append Queries 136 Update Queries 139 Make-Table Queries 145 Delete Queries 146 SQL-specific Queries 147 Union Queries 147 Data-definition Queries 149 Subqueries 151 Summary 153

Chapter 5: Using PivotTables and PivotCharts to Interact with Data 155 Creating a Data Source Query 156 PivotTables 157 The PivotTable Toolbar 164 The PivotTable Properties Sheet 166 PivotCharts 167 The PivotChart Toolbar 171 Embedded PivotCharts 173 Summary 174

x 559044 FM.qxd 3/30/04 2:42 PM Page xi

Contents

Chapter 6: Printing Data with Reports 175 Report Design Principles 175 The Report Wizard 176 Using Report Templates 179 Tabular Reports 181 Grouped Reports 183 Labels Reports 187 Columnar Reports 189 Reports with Subreports 192 Filtered by Form Reports 193 Summary Reports 197 Special Formatting for Reports 198 Conditional Formatting of Report Controls and Sections 198 Placing a Watermark on a Report 201 Creating a Main Menu with the Menu Manager Add-In 205 Installation 205 Preparation 206 Running the Add-in 208 Summary 212

Chapter 7: Writing VBA Code in Modules 213

The Project Explorer 213 The Properties Sheet 214 The Immediate Window 215 The Module Pane 215 Getting Help 216 Module Types 217 Statements for Use in Modules 217 Call 218 Do While . . . Loop 218 Do Until . . . Loop 219 For . . . Next 219 For Each . . . Next 220 GoTo 220 If . . . Then . . . Else 220 Select Case . . . End Select 221 With . . . End With 224 Standard Module Code Samples 224 Using Data in tblInfo 224 Date Range Code on Main Menu 226

xi 559044 FM.qxd 3/30/04 2:42 PM Page xii

Contents

Code Behind Forms Code Samples 228 New Thing Form 231 Sort and Filter Forms 235 DblClick Event from Datasheet Subform 241 Summary 243 Part Two: Modifying, Updating, and Maintaining Access Applications 245

Chapter 8: Managing the Application Life Cycle 247 Modifying an Application 247 Upgrading to a New Office Version 248 Upgrading an Application to a New Database Format 250 Setting Up Separate Boot Partitions for Different Office Versions 250 Working with Access 2000 in Higher Versions of Access 251 Working with Clients 260 Client Relations 260 Making Client-Requested Changes 261 Application-Specific Changes 261 Standard Changes 274 Customizing the Main Menu’s Word Letters Component 278 Summary 286

Chapter 9: Reworking an Existing Application 287 Applying a Naming Convention 287 The Leszynski Naming Convention 289 Applying the LNC to a Database 295 Creating a Main Menu 302 Normalizing Tables and Relationships 302 Reworking Forms to Enter and Display Many-to-Many Linked Data 309 Reworking the fpriEBookNotes Form 310 Reworking the frmTreeViewEBookNotes Form 315 Creating the New frmAuthors Form 320 Reworking Reports to Enter and Display Many-to-Many Linked Data 321 Summary 328

Chapter 10: Moving Old Data into a New Database 329 Getting at the Old Data 329 Using Queries with Calculated Fields to Append Data from a Non-Normalized Table to a Normalized Table 332

xii 559044 FM.qxd 3/30/04 2:42 PM Page xiii

Contents

Using VBA Code to Move Data from a Non-Normalized Table to Linked Normalized Tables 341 Denormalizing Data 351 Summary 359 Part Three: Working with Other Office Components (and More) 361

Chapter 11: Working with Word 363 Writing Automation Code 364 Some Handy Toolbar Buttons 365 The Word Object Model 366 Viewing the Word Object Model Diagram 366 A Note on Enums 370 The Basic Automation Commands 371 A Word VBA Shortcut 372 Exporting Access Data to Word Documents 373 Merge Types 373 Merge Examples 375 Importing Data from Word Tables into Access 413 Automation Problems and How to Avoid Them 420 References 420 Getting the Documents Directory from Word 421 Summary 424

Chapter 12: Working with Outlook 425 Writing Automation Code 425 The Outlook Object Model 426 Viewing the Outlook Object Model Diagram 426 The Basic Automation Commands 430 NameSpaces and Other Oddly Named Objects 430 Syntax for Referencing Outlook Objects 431 Referencing Outlook Items in the Interface and in Code 432 The Outlook-Exchange Wizard 433 Exporting Data from Access to Outlook Items 434 Creating a Mail Message from the Main Menu 437 MultiSelect Listbox Form 440 Datasheet Form 451 Email a Report Form 471

xiii 559044 FM.qxd 3/30/04 2:42 PM Page xiv

Contents

Importing Data from Outlook Items to Access 483 Using the Redemption Library to Avoid the Object Model Guardian 492 Summary 494

Chapter 13: Working with Excel 495 Writing Automation Code 498 The Excel Object Model 499 Viewing the Excel Object Model 499 The Basic Automation Commands 503 Workbooks, Worksheets, and Ranges 503 A Shortcut: Using the Excel Macro Recorder 504 Exporting Data from Access to Excel 505 MultiSelect Listbox Form 505 Datasheet Form 520 Importing Data from Excel 526 Importing from Worksheets and Ranges 526 Linking to Excel Worksheets and Ranges 529 Summary 530

Chapter 14: Working Outside of Office 531 Sending Faxes with WinFax 531 Sending a Single Fax Using DDE 531 Faxing a Report Using DDE 543 Sending Multiple Faxes Using Automation 550 Writing Data to a Text File 555 Export Contacts to Outlook 557 Summary 562

Index 563

xiv 559044 FM.qxd 3/30/04 2:42 PM Page xv

Acknowledgments

As always, thanks to my agent, Claudette Moore, for her work on this book project. Much thanks to my editors at Wiley, Bob Elliott and Emilie Herman, and to production editor Pamela Hanley and all the production staff who worked on this book. I am very grateful to my tech editor, Mary Hardy, who has greatly improved the quality of the sample databases by reviewing them in many versions, spotting problems and suggesting improvements. 559044 FM.qxd 3/30/04 2:42 PM Page xvi 559044 FM.qxd 3/30/04 2:42 PM Page xvii

About the Author

Helen Feddema grew up in New York City. She was ready for computers when she was 12, but personal computers were still in the future, so she got a B.S. in Philosophy from Columbia and an M.T.S. in Theological Studies from Harvard Divinity School, while working at various office jobs. It was at HDS that she got her first computer, an Osborne, and soon computers were her primary interest. She started with word processing and spreadsheets, went on to learn dBASE, and did dBASE development for six years, part of this time as a corporate developer. After being laid off in a flurry of corporate downsizing in the 1980’s, she started doing independent consulting and development, using dBASE, ObjectVision, WordPerfect and Paradox.

Always looking for something new and better, Helen beta tested Access 1.0, and soon recognized that this was the database she had been looking for ever since Windows 3.0 was introduced and she saw the gap waiting to be filled by a great Windows database. Since then she has worked as a developer of applications, concentrating on Access, Word and Outlook.

Helen co-authored Inside Microsoft Access 1.0 (New Riders, 1992), and wrote two books for Pinnacle’s “The Pros Talk Access” series, Power Forms and Power Reports (1994). She also co-authored Access How- Tos for the Waite Group Press (1995), and contributed to The Handbook (Osborne- McGraw-Hill), Que’s Special Edition: Using Microsoft Outlook 97 (1997), Office Annoyances (O’Reilly, 1997), and Outlook Annoyances (O’Reilly, 1998). She also contributed chapters to Que’s Special Edition: Using 98 (1997) and Sams’ Teach Yourself Project (1998).

Helen co-authored Sybex’ MCSD: Access 95 Study Guide (1998), and contributed chapters on Outlook programming to Que’s Special Edition: Using Microsoft Outlook 2000 (1999), and wrote DAO Object Model: The Definitive Reference for O’Reilly (2000), and Access 2002 Inside-Out for Microsoft Press.

Helen has been a regular contributor to Pinnacle’s Smart Access and Office Developer journals, Woody’s Underground Office newsletter, PC Magazine’s Undocumented Office and the MS Office and VBA Journal (now OfficePro). She is the editor of the Woody’s Access Watch ezine, and writes its Access Archon .

Helen is a big-time beta tester, sometimes having 7 or 8 betas running at once, mostly Microsoft, but with some from other vendors as well. She has participated in every Access beta from 1.0 to 2003, and is a member of the Access Insiders group.

She lives in the mid-Hudson valley area of New York state, with varying numbers of cats and computers.

Helen maintains a Web page (www.helenfeddema.com) with a large selection of code samples concen- trating on connecting Access, Outlook, Word and Excel (including several Access add-ins), and all her Access Archon articles. 559044 FM.qxd 3/30/04 2:42 PM Page xviii 559044 FM.qxd 3/30/04 2:42 PM Page xix

Introduction

If you are developing databases for your own use, you may not mind opening forms or printing reports directly from the database window, and you know what query to run before printing which report or exporting data to Word. However, if you are planning to create applications for the use of others, partic- ularly for clients who aren’t familiar with Access, and don’t understand databases in general, you have to do a lot more work, mostly in the form of writing VBA code to automate the application’s processes. As a rule of thumb, it’s generally true that the easier the application is for the end user, the harder (and more time-consuming) it is to develop. In this book I concentrate on teaching you how to set up your tables and relationships to ensure that the database is properly normalized, and write VBA code to cre- ate the connective tissue that turns a bunch of tables, queries, forms and reports into a complete and coherent application.

Over the last eight years, I have created several Access add-ins, to save time while creating applications. I use these add-ins to create a main menu for an application, automatically apply a naming convention to database objects, and ensure a consistent and professional appearance of the application’s forms. I’ll discuss the use of my add-ins to help construct an application in the appropriate chapters of this book.

You won’t find excruciatingly detailed step-by-step explanations of how to create a table, or lists of all the properties of a textbox—I assume that an experienced Access user either already knows how to do these things, or can look them up in Help or the Object Browser. But this book isn’t aimed at the advanced developer either, such as those who want to create their own custom objects and controls, use Windows API calls, or delve into the mysteries of the PrtMip property of the Print dialog (when you reach that point, I recommend the Access Developers Handbook, by Getz, Litwin and Gilbert).

Instead, this book concentrates on writing VBA code to connect the components of a database into a functioning, coherent application, with special attention to the very important but inadequately docu- mented area of Automation code, which is used to communicate with other Office applications, since sometimes the best way to add a requested feature to an Access application is to use a built-in feature of Word or Outlook.

Audience I wrote this book for experienced Access users, who know how to create tables, queries, forms and other Access objects, and have some familiarity with writing Access VBA code, but need help in making the transition from an experienced and competent Access user who can create databases for personal use, to an Access developer who can make a living developing applications for clients. 559044 FM.qxd 3/30/04 2:42 PM Page xx

Introduction How This Book is Organized Part One of this book, Creating an Access Application, covers the basics—creating tables, forms, and other database objects as part of an application, with chapters on creating a database (including tables and relationships), forms, controls, queries, PivotTables and PivotCharts, reports, and modules.

Part Two, Modifying, Updating, and Maintaining Access Applications, covers the techniques you need to use over the course of an application’s life cycle, including reworking applications created by non- developers, moving old data into a new database, and upgrading to a new Office version.

Part Three, Working with Other Office Components (and More), deals with using Automation code to exchange data among Office components, and even a few non-Office programs, with separate chapters for exchanging data with Word, Outlook, and Excel, and a chapter for communicating with non-Office applications, including Symantec WinFax. If you need to write Access data to Word letters or Excel worksheets, you can go right to the relevant chapter.

Access Versions The book covers Access versions from Office 2000 to Office 2003, and most of the book’s material applies to all of these versions. Where a specific element (such as PivotCharts, introduced in Access 2002) requires a higher version of Access, this will be noted in the text.

With the exception of the ActiveX controls discussed in Chapter 3 (Selecting the Right Controls for Forms), all you need to work through the examples in this book is Access 2000 or higher; if you want to use the ActiveX controls, however, you need the Developer edition of Access (which includes these con- trols) or another program that includes them, such as Visual Basic.

Conventions To help you get the most from the text and keep track of what’s happening, we’ve used a number of con- ventions throughout the book.

Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text.

Tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.

As for styles in the text:

❑ We highlight important words when we introduce them ❑ We show keyboard strokes like this: Ctrl+A ❑ We show file names, URLs, and code within the text like so: persistence.properties

xx 559044 FM.qxd 3/30/04 2:42 PM Page xxi

Introduction

❑ We present code in two different ways:

In code examples we highlight new and important code with a gray background.

The gray highlighting is not used for code that’s less important in the present context, or has been shown before.

Source Code As you work through the examples in this book, you may choose either to type in all the code manually (this is practical if you just need to add a few lines of code to an existing procedure), or you can use the sample databases that accompany the book, or import forms, reports, or modules from the sample databases into your own databases. All of the sample databases used in this book are available for download at http://www.wrox.com. Once at the site, simply locate the book’s title (either by using the Search box or by using one of the title lists) and click the Download Files link on the book’s detail page to obtain all the sample files for the book. There is a zip file for each part of the book, containing the sample databases, add-ins and other files needed for the chapters in that part.

Because many books have similar titles, you may find it easiest to search by ISBN; for this book the ISBN is 0-764-55904-4.

Once you download the zip file, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at http://www.wrox.com/dynamic/books/ download.aspx to see the sample databases and/or code available for this book and all other Wrox books.

Sample Files A number of sample databases are used as illustrations for various chapters; and three of my add-ins are also used to enhance database functionality; the chapters where the sample databases and add-ins are used are listed in the table below. In the case of add-ins, they may also be mentioned briefly in other chapters; the listed chapters are the ones where the use of the add-in is described in detail.

Zip File Contents Chapter(s)

Part One.zip Design Schemes.zip 2 (Forms) Menu Manager Add-in.zip 6 (Reports) PivotTables and PivotCharts.mdb 5 (PivotTables and PivotCharts) Query Expressions.mdb 4 (Queries) Toy Workshop (*).mdb 1 (Creating a Database for an Application), 2 (Forms), 3 (Controls), 4 (Queries), 6 (Reports), and 7 (Modules) (Start and Finish versions) Table continued on following page

xxi 559044 FM.qxd 3/30/04 2:42 PM Page xxii

Introduction

Zip File Contents Chapter(s)

Part Two.zip EBook Companion.mdb 9 (Reworking an Existing Application) LNC Rename Add-in.zip 9 (Reworking an Existing Application) Original EBook Companion.mdb 9 (Reworking an Existing Application) Test References.mdb 8 (Managing The Application Life Cycle) Toy Workshop (*).mdb 10 (Moving Data) (Start and Finish versions) Part Three.zip Categories.xls 13 (Working with Excel) Customers.xls 13 (Working with Excel) Excel Data Exchange.mdb 13 (Working with Excel) Non-Office Data Exchange.mdb 14 (Working outside of Office) Outlook Data Exchange with 12 (Working with Outlook) Redemption.mdb Outlook Data Exchange.mdb 12 (Working with Outlook) Word Data Exchange.mdb 11 (Working with Word) Word Docs.zip 11 (Working with Word)

Errata We make every effort to ensure that there are no errors in the text or in the code. However, no one is per- fect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata you may save another reader hours of frustration and at the same time you will be helping us provide even higher quality information.

To find the errata page for this book, go to http://www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can all errata that has been submitted for this book and posted by Wrox editors. A complete book list including links to each’s book’s errata is also available at www.wrox.com/misc-pages/ booklist.shtml.

If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/ techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.

xxii 559044 FM.qxd 3/30/04 2:42 PM Page xxiii

Introduction p2p.wrox.com For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based sys- tem for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At http://p2p.wrox.com you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

1. Go to p2p.wrox.com and click the Register link. 2. Read the terms of use and click Agree. 3. Complete the required information to join as well as any optional information you wish to pro- vide and click Submit. 4. You will receive an e-mail with information describing how to verify your account and com- plete the joining process.

You can read messages in the forums without joining P2P but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages other users post. You can read mes- sages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to ques- tions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

xxiii 559044 FM.qxd 3/30/04 2:42 PM Page xxiv 02 559044 PP01.qxd 3/29/04 11:59 AM Page 1

Part 1: Creating an Access Application

Chapter 1: Creating a Database for an Application

Chapter 2: Using Forms to Work with Data

Chapter 3: Selecting the Right Controls for Forms

Chapter 4: Sorting and Filtering Data with Queries

Chapter 5: Using PivotTables and PivotCharts to Interact with Data

Chapter 6: Printing Data with Reports

Chapter 7: Writing VBA Code in Modules 02 559044 PP01.qxd 3/29/04 11:59 AM Page 2 03 559044 Ch01.qxd 3/29/04 11:57 AM Page 3

Creating a Database for an Application

An application is more than just a database. Anybody with Access can create a database, but a database with a bunch of disconnected tables, queries, forms, and reports is not an application. An application consists of a database—or possibly several databases—containing normalized tables with appropriate relationships between them; queries that filter and sort data; forms to add and edit data; reports to display the data; and possibly PivotTables or PivotCharts to analyze the data, with all of these components connected into an efficiently functioning and coherent whole by Visual Basic for Applications (VBA) code. This chapter covers preparation for creating an application (getting and analyzing the information you need from the client), and creating tables to hold the application’s data.

Most Access books give you lots of information about Access database tables (and other database objects), but don’t necessarily tell you the stuff you really need to know: how to divide up the raw data you receive from a client into separate tables, how to decide what field type to use for each field in a table, and what relationships to set up between tables to create an efficient and well- integrated application. Through a series of developer-client Q&A sessions, I’ll show you how to extract the information you need to create the right tables for your application and link them into appropriate relationships.

I have always found it easier to understand a process by watching somebody do it, as opposed to reading abstract technical information about it, so in this chapter, I will explain what I am doing as I walk you through the preparation for creating an application and then the creation of its tables. Succeeding chapters will deal with creating the application’s forms, queries, and reports. Some technical information is necessary, of course, but it will be interspersed with demonstrations of what you need to do, and the explanations will tend to follow the actions, rather than precede them. Sometimes, if you can see how something is done correctly, that is all you need to know in order to do it right yourself, while an abstract technical explanation by itself is rarely adequate to teach you how to do something correctly. 03 559044 Ch01.qxd 3/29/04 11:57 AM Page 4

Chapter 1

There won’t be lots of step-by-step walk-throughs illustrating how to create database objects in this book, or long lists of properties and other attributes. I am assuming that you already know how to create tables, forms, and other database objects (and if you need detailed information, you know how to get it from Help), and that instead, what you need is help in making decisions on what kind of data goes into which table, how the tables should be related, and what types of forms, queries, and reports are best for working with the data in your application.

Although code is crucial to binding an application into a coherent whole, there won’t be any code in this chapter, because in Access, code runs from event procedures, and tables don’t have event procedures. Before writing event procedures, we need to create tables to hold the data, and that is what this chapter covers.

Gathering Data To start creating an Access application, you need two things: a clear idea of what tasks the application should perform and the output it should produce, and an adequate quantity of realistic data. Rather than just asking the client for a list of the tasks the application needs to perform, I usually ask a series of questions designed to elicit the required information. A typical Q&A session is presented in the next sec- tion of this chapter. However, if there is already a functioning database, printouts of its reports and screen shots of its forms can be helpful as an indication of what tasks are currently being done.

For best results, there is no substitute for large quantities of real data, such as the ebook data in the sam- ple EBook Companion database used in Chapter 9, Reworking an Existing Application. But if you have a reasonable quantity of representative data and a client who is willing to answer your questions, that should be sufficient to set up tables with the correct fields. With this information, you can create tables with the necessary fields; set up relationships between them; and proceed to create the queries, forms, reports, and VBA code that will let you create an application that does what the client wants.

Curiously, I’m often asked to start working on an application for a client without any data at all. It may be difficult to convey this concept to a client, but it is important to get real-life data (either in electronic or paper form) in order to set up tables and fields correctly. If you (the developer) have to create dummy data to have something to work with when creating tables and other database objects, you will probably end up having to make changes—possibly major changes—to tables later on, and find that further changes are needed to other database components; so, it really helps to have a substantial amount of rep- resentative data to work with.

However, realistically there are cases where you won’t be able to get data from the client. There are two cases where data isn’t available: a brand-new business (or other enterprise) that doesn’t have any data yet, and a business whose data is confidential. In these cases, you just have to do the best you can, creating dummy data after questioning the client about what data needs to be stored in the application’s tables.

Once you have obtained the data in electronic or paper form, it’s best to just use it as raw material to help you determine what fields you need when designing tables and other components, rather than as ready- made components to plug into your application. Looking at real data, you can see (for example) whether or not there are unique IDs for products. If there is a unique Product ID, that field should be the key field of the Products table; otherwise, you will need to create an AutoNumber field. If you see multiple addresses for customers or clients, you will need to create linked tables for address data, for purposes of normalization; if there is only one address per customer or client, address data can be stored directly in the Customers or Clients table. In most cases, even if you are given a database with Access tables, you will

4