What Is VBA?: When To Use VBA Instead of Macros
What Is VBA?: When To Use VBA Instead of Macros
Visual Basic for Applications (VBA) is the programming language for Microsoft Office and its associated applications. You use it for the same reason you use macrosto tie the objects in your application together into a coherent system. The difference is that VBA provides more power and a finer degree of control than you get by using macros alone. Some Familiar Territory for the Seasoned Programmer VBA is a modern programming language that strongly resembles most of the popular, structured programming languages. If you're a Pascal or C programmer, you'll find all the program structures you're used toloops, If...Then...Else statements, Select Case statements, functions, and subroutineswith only superficial differences. With all its improvements from earlier versions of Basic, VBA retains its English-like flavor and ease of use.
Make your application easier to maintain Because macros are separate objects from the forms and reports that use them, an application containing a large number of macros that respond to events on forms and reports can become difficult for you, the application developer, to maintain. In contrast, when you use VBA to respond to events, your code is built into the form or report's definition. If you move a form or report from one database to another, the VBA code built into the form or report moves with it. (Code is a general term for the statements you write in a programming language.) Create your own functions Access includes many built-in, or intrinsic, functions such as the IPmt function that calculates an interest payment. You can use these functions to perform calculations without having to create complicated expressions. Using VBA, you can also create your own functions to either perform calculations that exceed the capability of an expression or replace complex expressions you've written in your application. See Also For more information about creating a function, see "Creating Your First Function" later in this chapter.
Mask error messages When something unexpected happens in your application and Access displays an error message, the message can be quite mysterious to your
application's users, especially if they aren't familiar with Access. Using VBA, you can detect the error when it occurs and display your own message, or you can have your application do something else. Applications used by a variety of people almost always require some VBA code for handling errors. See Also For more information about handling errors in your application, see Chapter 8, "Error Handling and Debugging," in the Microsoft Office 2000/Visual Basic Programmer's Guide (Microsoft Press, 1999). You can also find this guide in the Office Developer Documentation section of the Microsoft Developer Network (MSDN) Online Library.
Create or manipulate objects In most cases, you'll find that it's easiest to create and modify an object in that object's Design view. In some situations, however, you may want to manipulate the definition of an object in code. Using VBA, you can manipulate all the objects in a database, including the database itself. An Access wizard is a good example of an application that creates and modifies objects using code. For example, the Form Wizard is a collection of VBA functions that creates a form according to the specifications supplied by the user. Perform system-level actions You can use the RunApp action in a macro to run another Microsoft Windows-based or MS-DOS-based application from your Access application, but you can't use a macro to do much else outside Access. Using VBA, you can check to see if a file exists on the system, use Automation or dynamic data exchange (DDE) to communicate with other Windows-based applications such as Microsoft Excel, and call functions in Windows dynamic-link libraries (DLLs). See Also For more information, see Chapter 4, "Working with Objects and Collections." For more information about using DLLs in your application, see Chapter 10, "The Windows API and Other Dynamic-Link Libraries," and Chapter 11, "Add-Ins, Templates, Wizards, and Libraries," in the Microsoft Office 2000/Visual Basic Programmer's Guide (Microsoft Press, 1999). You can also find this guide in the Office Developer Documentation section of the Microsoft Developer Network (MSDN) Online Library.
Manipulate records one at a time You can use VBA to step through a set of records one record at a time and perform an operation on each record. In contrast, macros work with entire sets of records at once. Pass arguments to your code An argument is a value that supplies the additional information that some actions require. You set arguments for macro actions in the lower part of the Macro window when you create the macro; you can't change them when the macro is running. With VBA, however, you can pass arguments to your code at the time it runs. You can even use variables for argumentssomething you can't do in macros. This gives you a great deal of flexibility in how your code runs. Tip Although you can have both macros and VBA code in your application, you may find it easier to use VBA exclusively once you get started programming. If you have
macros in your application, Access can automatically convert them to event procedures or modules that perform all the equivalent actions in VBA code. In form or report Design view, use the Convert Macros To Visual Basic command (Tools menu, Macro submenu). For global macros that aren't attached to a specific form or report, use the Save As command (File menu) to save the macro as a module. For more information, type convert macros in the Office Assistant or on the Answer Wizard tab in the Help window, and then click Search.