0% found this document useful (0 votes)
69 views4 pages

What Is VBA?: When To Use VBA Instead of Macros

The document discusses Visual Basic for Applications (VBA), explaining what it is, when to use it instead of macros, and how event-driven applications using VBA work. VBA is the programming language for Microsoft Office applications and provides more power than macros alone. Some reasons to use VBA instead of macros include making applications easier to maintain, creating custom functions, and handling errors.

Uploaded by

jwalant.baria
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views4 pages

What Is VBA?: When To Use VBA Instead of Macros

The document discusses Visual Basic for Applications (VBA), explaining what it is, when to use it instead of macros, and how event-driven applications using VBA work. VBA is the programming language for Microsoft Office applications and provides more power than macros alone. Some reasons to use VBA instead of macros include making applications easier to maintain, creating custom functions, and handling errors.

Uploaded by

jwalant.baria
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

What Is VBA?

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.

When to Use VBA Instead of Macros


With Access, you can accomplish many tasks with macros or through the user interface that require programming in other database systems. So, when do you turn to VBA? It depends on what you want to do.

Why Use VBA?


You'll want to use VBA instead of macros if you want to do any of the following:

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.

Why Use Macros?


After reading all the reasons for using VBA, you may wonder if there are any reasons left for using macros. However, macros do have their place in many applications. Macros are an easy way to take care of simple details such as opening and closing forms, showing and hiding toolbars, and running reports. Because you specify options for each action in the lower part of the Macro window, there's little syntax to remember, and developing applications can often be faster than with VBA. In addition to the ease of use macros provide, creating a macro is the only way to make global key assignments. See Also For information about assigning keys with an AutoKeys macro, see Chapter 1, "Creating an Application."

How an Event-Driven Application Works


An event is an action recognized by a form, report, or control. Each type of object in Access automatically recognizes a predefined set of events. When you want a form, report, or control to respond to an event in a particular way, you can write a VBA event procedure for that event. Here's what happens in a typical event-driven application: 1. A user starts the application and Access automatically opens the startup form specified in the Startup dialog box. 2. The startup form, or a control on the startup form, receives an event. The event can be caused by the user (for example, a keystroke or mouse click), or by your code (for example, an Open event when your code opens a form). 3. If there is an event procedure corresponding to that event, it runs. 4. The application waits for the next event. Note Some events automatically trigger other events. For example, when the MouseDown event occurs, the MouseUp and Click events immediately follow.

You might also like