Controls on a form in Design view
Contents at a Glance
Chapter 1: What the Heck Is VBA?
Finding VBA Code
Enabling VBA Code
Using the Visual Basic Editor
Discovering Code as You Go
Chapter 2: Writing Code
How VBA Works
VBA Syntax
Declaring Module Options
Writing Your Own VBA Procedures
Typing and Editing in the Code Window
Testing and Running Your Code
Chapter 3: Writing Smarter Code
Creating Variables and Constants
Making Decisions in VBA Code
Executing the Same Code Repeatedly
Using Custom Functions
Chapter 4: Controlling Forms with VBA
Displaying Custom Messages
Opening Forms with DoCmd
Changing Form Controls with VBA
Understanding Objects and Collections
Chapter 5: Using SQL and Recordsets
Recordsets and Object Models
SQL and Recordsets
Running Action Queries from VBA
Chapter 6: Debugging Your Code
Considering Types of Program Errors
Fixing Compiler Errors
Trapping Runtime Errors
Dealing with Logical Errors
Chapter 1: What the Heck Is VBA?
In This Chapter
Understanding Visual Basic for Applications (VBA)
Using the Visual Basic Editor
Discovering code as you go
Visual Basic for Applications — often abbreviated VBA — is a programming language that you can use to extend the functionality of Microsoft Access and other products in the Microsoft Office suite of programs. A programming language is a means of writing instructions for the computer to execute (perform). Programmers often refer to the written instructions as code because the instructions aren’t in plain English. Rather, they’re in a code that the computer can interpret and execute.
You can create sophisticated Access databases without using VBA at all. In most cases, the other objects offered by Access — tables, queries, forms, reports, and macros — offer more than enough flexibility and power to create just about any database imaginable. But once in a while, you come across a situation where you want to do something that none of those other objects can do. That’s where VBA comes in. If you can find no other way to accomplish some goal in Access, writing VBA code is usually the solution.
Finding VBA Code
So what the heck is VBA code, anyway? To the untrained eye, VBA code looks like gibberish — perhaps some secret code written by aliens from another planet. But to Access, the code represents very specific instructions on how to perform some task.
Within any given database, Access stores code in two places:
♦ Class modules (code-behind forms): Every form and report you create automatically contains a class module (also called a code-behind form), as ...