Introduction VBA
-
Upload
jwalantbaria -
Category
Documents
-
view
218 -
download
0
Transcript of Introduction VBA
8/22/2019 Introduction VBA
http://slidepdf.com/reader/full/introduction-vba 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 macros — to 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 to — loops, If...Then...Else statements, Select Case statements, functions,
and subroutines — with 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 macrosthat 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 databaseto 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 inyour 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
8/22/2019 Introduction VBA
http://slidepdf.com/reader/full/introduction-vba 2/4
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 alwaysrequire 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) OnlineLibrary.
Create or manipulate objects In most cases, you'll find that it's easiest to create andmodify 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 thespecifications 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, youcan 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, "TheWindows 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 OfficeDeveloper 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 themacro is running. With VBA, however, you can pass arguments to your code at the time
it runs. You can even use variables for arguments — something 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
8/22/2019 Introduction VBA
http://slidepdf.com/reader/full/introduction-vba 3/4
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 easyway 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 globalkey 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 torespond 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.
8/22/2019 Introduction VBA
http://slidepdf.com/reader/full/introduction-vba 4/4