www.GetPedia.com *More than 150,000 articles in the search database *Learn how almost everything works Microsoft Access ® VBA Programming for the Absolute Beginner Second Edition MICHAEL VINE © 2005 by Thomson Course Technology PTR. All rights reserved. No Publisher and GM of Course PTR: part of this book may be reproduced or transmitted in any form or by Stacy L. Hiquet any means, electronic or mechanical, including photocopying, record- Associate Director of Marketing: ing, or by any information storage or retrieval system without written Sarah O’Donnell permission from Thomson Course Technology PTR, except for the inclusion of brief quotations in a review. Marketingg Manager: Heather Hurley The Premier Press and Thomson Course Technology PTR logo and related trade dress are trademarks of Thomson Course Technology PTR and Manager of Editorial Services: may not be used without written permission. Heather Talbot Microsoft and Access 2003 are registered trademarks of Microsoft Cor- Acquisitions Editor: poration in the United States and/or other countries. Mitzi Koontz All other trademarks are the property of their respective owners. Senior Editor: Mark Garvey Important: Thomson Course Technology PTR cannot provide software support. Please contact the appropriate software manufacturer’s tech- Marketing Coordinator: nical support line or Web site for assistance. Jordan Casey Thomson Course Technology PTR and the author have attempted Project Editor: throughout this book to distinguish proprietary trademarks from Scott Harris/Argosy Publishing descriptive terms by following the capitalization style used by the man- ufacturer. Technical Reviewer: Eric Olson Information contained in this book has been obtained by Thomson Course Technology PTR from sources believed to be reliable. However, PTR Editorial Services Coordinator: because of the possibility of human or mechanical error by our Elizabeth Furbish sources, Thomson Course Technology PTR, or others, the Publisher Copy Editor: does not guarantee the accuracy, adequacy, or completeness of any Tonya Cupp information and is not responsible for any errors or omissions or the results obtained from use of such information. Readers should be par- Interior Layout Tech: ticularly aware of the fact that the Internet is an ever-changing entity. Shawn Morningstar Some facts may have changed since this book went to press. Cover Designer: Educational facilities, companies, and organizations interested in mul- Mike Tanamachi tiple copies or licensing of this book should contact the publisher for CD-ROM Producer: quantity discount information. Training manuals, CD-ROMs, and por- Keith Davenport tions of this book are also available individually or can be tailored for specific needs. Indexer: Nancy Fulton ISBN: 1-59200-723-6 Library of Congress Catalog Card Number: 2004114910 Proofreader: Printed in the United States of America Jan Cocker 05 06 07 08 09 BH 10 9 8 7 6 5 4 3 2 1 Thomson Course Technology PTR, a division of Thomson Course Technology 25 Thomson Place Boston, MA 02210 http://www.courseptr.com To Sheila: 143 Acknowledgments W riting any book is not easy, especially a technical programming book. It takes many great, patient, and talented people to write, edit, design, market, finance, and produce a book and accompanying CD-ROM. Without the publishing assistance of Mitzi Koontz, Scott Harris, and Tonya Cupp, it would be impossible for me to share with you my knowledge of programming in such a professional and fun manner. I’d like to thank a good friend, Eric Olson, who ensured the technical accuracy of this book. Thanks Eric! Now that the book is done, let’s go fishing! Beyond the technical and business workings of creating a book, the Author must be fed, loved, and encouraged. Without the support of my beautiful wife Sheila, this would never have happened. Thanks baby: I love you! About the Author M ichael Vinehas taught computer programming, Web design, and database classes at Indiana University/Purdue University in Indianapolis, IN, and at MTI College of Business and Technology in Sacramento, CA. Michael has over 13 years of experience in the information technology profession. He currently works full time at a Fortune 100 company as an IT Project Manager overseeing the development of enterprise data warehouses. This page intentionally left blank Contents Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Access Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Chapter 1 Getting Started with Access 2003 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 System Requirements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Migrating to Access 2003. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Your First Access Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Creating an Access Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Tables and Fields. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Relationships. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Common Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Hungarian Notation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Bound Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Startup Forms and Dashboards. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Getting Help with Access 2003. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Chapter Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Introduction to Access VBA . . . . . . . . . . . . . . . . . . .35 Chapter 2 The Event-Driven Paradigm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Object-Based Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 The VBA IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Introduction to Event Procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Introduction to VBA Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Accessing Objects and Their Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 The Forms Collection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 The Me Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Assignment Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Command and Label Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Getting User Input with Text Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Variables and Beginning Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Variable Naming Conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Variable Scope. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Option Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 viii Access VBA Programming for the Absolute Beginner, Second Edition VBA Arithmetic and Order of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Chapter Program: Fruit Stand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Chapter Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67 Chapter 3 If Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Nested If Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Compound If Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Select Case Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Message Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Input Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Common Controls Continued . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Option Group. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Option Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Toggle Buttons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Chapter Program: Hangman. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Chapter Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Looping Structures . . . . . . . . . . . . . . . . . . . . . . . . . . .97 Chapter 4 Introduction to Looping Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Do While. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Do Until. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Loop While. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Loop Until. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 List and Combo Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Adding Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Removing Items. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Managing Columns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Chapter Program: Math Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Chapter Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Functions Continued . . . . . . . . . . . . . . . . . . . . . . . . .121 Chapter 5 String-Based Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 UCase. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 LCase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Len . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Description: