Acknowledgments Introduction Chapter 1: Introduction to Access 2007 VBA 1 What Is Access 2007 VBA? 1 What s New in Access 2007 VBA? 2 Access 2007 VBA Programming 101 3 Requirements-Gathering Phase 3 Design Phase 4 Model the Application Design 4 Determine Application Architecture 11 Development Phase 18 The Visual Basic Editor 18 Testing Phase 24 Running and Debugging Code in the Visual Basic Editor 24 Implementation Phase 25 Summary 25 Exercises 26 Chapter 2: The Basics of Writing and Testing VBA Code 27 Creating Modules 27 Standard Modules versus Class Modules 29 Creating and Calling Procedures 33 Sub versus Function Procedure 33 Create and Call a New Sub Procedure 33 Create and Call a New Function Procedure 42 Scope and Lifetime of Sub Procedures and Functions 45 Using Built-In Functions 45 Macros versus Procedures 46 Using Variables and Constants to Store Values 49 Types of Variables 49 Declaring and Using Variables 51 Declaring and Using Arrays 51 COPYRIGHTED MATERIAL ix xix
Constants 53 Declaring and Using Constants 53 Using Built-In Constants 54 Scope and Lifetime of Variables and Constants 54 Naming Conventions 55 Controlling Program Flow 56 Decision Making 56 If...Then 56 IIf 59 Conditional If 59 Select... Case 59 Loops 61 For... Next and For Each... Next 61 Do... Loop 62 While... Wend 63 Documenting Your Code 63 Error Debugging 64 Types of Errors 64 Syntax Errors 64 Compile Errors 64 Runtime Errors 64 Logic Errors 66 Debugging Your Code 66 Using Breakpoints to Step through Code 66 Using the Immediate Window 68 Using the Locals Window 68 Using the Watch Window 69 Using the Call Stack 70 Error Handling 71 Default Error Messages 72 Handling Errors with an On Error Statement 72 On Error Statement 72 Resume Statement 72 The Err Object 74 Raising an Error 74 Using the Errors Collection 75 Creating a Generic Error Handler 75 Summary 78 Exercises 78 xii
Chapter 3: Programming Applications Using Objects 79 Elements of an Object 79 What Is a Property? 80 What Is a Method? 82 What Is an Event? 84 Viewing Objects Using the Object Browser 91 Ways to Refer to Objects 93 Using Access Collections and Objects 94 The Forms Collection and Form Object 94 The Reports Collection and Report Object 96 The CurrentProject Object 97 The DoCmd Object 97 The Screen Object 98 The Printers Collection and Printer Object 98 Other Objects 100 Summary 100 Exercises 100 Chapter 4: Creating Your Own Objects 101 Using Class Modules to Create Your Own Objects 101 Class Diagrams 102 Creating Properties 105 Using Public Variables 105 Using Property Let, Property Get, and Property Set 106 Creating Methods 111 Using Public Sub Procedures and Functions 111 Creating Events 113 Declaring and Raising Events 114 Creating the Event Sub Procedure or Function 114 Using the Class 115 Instantiating the Class 115 Initialize and Terminate Events 115 Advanced Techniques 121 Creating Multiple Instances of the Class 121 Creating Class Hierarchies 122 Working with Enumerated Types 122 Inheritance Using Implements Keyword 123 xiii
Create Custom Properties for Existing Objects 124 Using Existing Tag Property 124 Using Public Variables 124 Using Property Let, Property Get, and Property Set Routines 124 Create Custom Methods for Existing Objects 125 Using Public Sub Procedures and Functions 126 Summary 127 Exercises 127 Chapter 5: Interacting with Data Using ADO and SQL 129 Introduction to Data Access 129 Using ADODB to Work with Data 130 The ADO Object Model 130 The Connection Object 131 Connecting to Access Databases 132 Connecting to Other Databases 132 The Command Object 132 The Recordset Object 132 Creating a Recordset 133 Counting Records in a Recordset 140 Navigating through a Recordset 141 Adding, Editing, and Deleting Records in a Recordset 141 Sorting, Finding, and Filtering Records in a Recordset 156 Introduction to SQL 159 Retrieving Data Using SQL Select Statements 160 Select Clause 160 From Clause 160 Where Clause 161 Order By Clause 161 Subqueries 162 Union 162 Using SQL and ADO to Populate a Recordset 162 Inserting Data Using SQL Insert Statements 164 Inserting Results of a Select into a Table 165 Updating Data Using SQL Statements 165 Deleting Data Using SQL Statements 165 xiv
Using ADOX to Manipulate Data Objects 177 Creating a Table with ADOX 177 Summary 179 Exercises 179 Chapter 6: Building Interactive Forms 181 Form Navigation and Flow 181 Working with Controls 187 Combo Boxes versus List Boxes 188 Tab Controls and Subforms 193 Building User Interaction 195 The MsgBox Function 195 Validating User Input 196 Adding Polish to Your Forms 197 Summary 198 Exercises 198 Chapter 7: Importing, Linking, and Exporting Using External Data Sources 199 Linking, Importing, and Exporting Overview 200 Access and Other Databases 200 Transferring Complete SQL Server Database 203 Spreadsheets 204 Text Files 205 XML Files 208 E-mails and Outlook 210 Other Ways to Export Data 211 Summary 211 Exercises 212 Chapter 8: Using Access with Web Services and SharePoint Lists 213 Introduction to Web Services 214 Using Data from Web Services 214 Introduction to SharePoint Lists 221 Using Data from SharePoint Lists 222 Linking and Importing SharePoint Lists 222 Summary 232 Exercises 233 xv
Chapter 9: Creating Reports and Web-Enabled Output 235 Working with Reports from VBA 235 Setting the Report Data Source 236 Creating Reports Programmatically 237 Creating an Empty Report 238 Adding Controls to the Report 238 Using the Printer Object to Designate Printing Options 246 Working with Report Events 247 Exporting Access Data to Web Formats 248 Creating Static HTML Pages 249 Summary 251 Exercises 251 Chapter 10: Building SQL Server Applications with Access Projects 253 Access Projects Using Access as a Front End to SQL server 253 Setting Up SQL Server 254 SQL Server Versions 255 Obtaining and Installing SQL Server Express Engine 255 The Benefits of SQL Server Express Edition 256 Installing SQL Server Express Edition 256 Understanding What Was Installed 257 Creating Access Projects (ADPs) 259 Using an Existing SQL Server Database 260 Using a New SQL Server Database 262 Working with SQL Server Objects from Access 264 SQL Server Tables 264 SQL Server Stored Procedures 267 SQL Server Views 271 SQL Server Functions 275 Migrating an Existing Access Database to an Access Project 277 Changes to an Existing Database 277 Using the Upsizing Wizard 277 Summary 285 Exercises 285 Chapter 11: Advanced Access Programming 287 Using Dynamic Link Libraries (DLLs) 287 Standard DLLs 287 Using External DLLs 291 xvi
Automation 294 Working with Automation Objects 294 Controlling Microsoft Excel 295 Controlling Microsoft Word 298 Controlling Microsoft Outlook 302 ActiveX Controls 305 Libraries 308 Add-Ins 312 Transactions 313 Summary 314 Exercises 314 Chapter 12: Finishing the Application 315 Multiuser Considerations 315 Record Locking and Update Conflicts 316 Record Locking on Bound Forms 316 Record Locking on Unbound Forms 317 Multiuser Architecture Considerations 318 Optimizing Your Applications 322 Improving Actual Performance 322 General Design Guidelines 322 Optimizing VBA Code 323 Improving Data Access 324 Improving Perceived Performance 325 Running the Performance Analyzer 326 Securing Your Application 327 Adding a Database Password 327 Adding a Password for VBA Code 328 Encrypting a Database 329 Distributing Your Application 329 Distributing the Application to Users with the Full Version of Access 330 Distributing the Application with the Access Runtime 330 Distributing an Execute-Only Application 331 Maintaining the Application 331 Compacting and Repairing the Database 331 Making Backup Copies of the Database 331 Summary 332 Exercises 332 xvii
Chapter 13: Case Study 1: Project Tracker Application 333 Design Specifications 334 Building the Database 340 Building the User Interface 344 Building the Class Modules for the Objects 357 The Project Class 358 The Contact Class 372 Building the Standard Modules 383 Connecting the User Interface to the Code 410 The Projects Form 410 The Contacts Form 440 Touring the Finished Project Tracker Application 452 Summary 459 Chapter 14: Case Study 2: Customer Service Application 461 Design Specifications 461 Building the Database and Database Objects 465 Building the User Interface 475 Building the Class Module for the Objects 481 Building the Standard Modules 488 Connecting the User Interface to the Code 499 The Customer Search Form 500 The View/Manage Customer Accounts Form 504 Touring the Finished Customer Service Application 512 Summary 518 Appendix A: Exercise Answers 519 Index 527 xviii