Home > Articles > Data > Access

Taking More Control of Access

  • Print
  • + Share This
By using macros and creating Visual Basic for Applications (VBA) code, you can take more control of Access capabilities. This chapter shows how VBA code can overcome the limitations of using macros in Access and illustrates some of the useful things you can do with VBA code.
This chapter was selected from Creating Microsoft Access 2000 Solutions: A Power User's Guide.

By "Taking More Control of Access," I'm referring to the ability to do much more with Access than you can by such techniques as choosing from menus, responding to dialog boxes, and dragging objects from one place to another (such as dragging an icon from the toolbox onto a form or report). Access provides two ways for you to take more control: by using macros and by creating Visual Basic for Applications (VBA) code.

After taking a quick look at some of the limitations of using macros, this chapter shows how VBA code can overcome those limitations and illustrates some of the useful things you can do with VBA code.


This book doesn't attempt to teach you how to become a versatile VBA programmer. Instead, the book focuses on showing you how you can begin to use VBA to extend what you can do with Access.

In the first section of this chapter, you'll learn some of the advantages and disadvantages of using Access macros and how to convert macros into VBA code. The bulk of this chapter, however, teaches you how to create VBA code that responds to form and command button events, and how to create bulletproof and efficient Access applications.

Understanding Macros in Access

If you've used macros in another Office application such as Excel or Word, you probably think of a macro as a way of automatically repeating keystrokes and selections. In Excel and Word, you can turn on the macro recorder and then perform a task that can involve choosing menu items, responding to dialog boxes, and the like. After you've completed the task, you turn off the macro recorder. At that time, all your actions are automatically saved as a sequence of VBA statements. Subsequently, you can replay what the macro recorder saved to repeat the recorded actions. You can also edit the recorded VBA statements to modify the actions performed by the macro.

Macros in Access are quite different from those in Excel and Word. Access doesn't have a macro recorder; you can't record a sequence of actions as you can in Excel and Word. Instead, follow these steps to create an Access macro:

  • In the Database window, select Macros in the Objects bar, and then choose New to display the Macro window.

  • Open the drop-down list of Actions and select which action you want to perform. In most cases, the Action Arguments pane at the bottom displays a list of arguments.

  • NOTE

    While you have the drop-down list of Actions open, take a few minutes to glance through the list. By doing so, you'll gain an understanding of the actions that can be incorporated in macros.

  • Either select from a drop-down list of values for each argument or enter a value for each argument.

  • Choose File, Save to save the macro with a specific name.

Access doesn't save a macro as VBA code in the way that Excel and Word do. The only way to edit an existing macro is to reopen it and change the selections you previously made or the values you entered.

After you've created an Access macro, you can run it in the same way you run an Excel or Word macro. Choose Tools, move the pointer onto Macro, and choose Run Macro.

Perhaps the most significant problem with macros in Access is that they have no way of trapping errors. If something goes wrong, all you see is an error message—there's no way to correct the problem other than to cancel, make whatever changes you think are necessary, and try running the macro again. Another problem with Access macros is they don't always let you do what you want to do. You can overcome both these possible problems by using VBA code. One more disadvantage of macros is that they run more slowly than the equivalent VBA code.

Some writers advise avoiding the use of Access macros because of their limitations and also because of the possibility that future versions of Access might not support macros. My take on this is that you should not use macros in major applications that you expect to have in use when a new version of Access becomes available. Nonetheless, Access macros are useful in at least two ways.

If you need to develop an Access-based application quickly and don't have the time to create optimized VBA code, you might find that a few well-chosen macros can come to your rescue. Granted, an application that uses macros may sometimes crash because it lacks error trapping, but, in the real world, it's sometimes better to have an application today that isn't bulletproof than not to have the application when it's needed.

Another great use for Access macros is to develop prototype VBA code. Didn't you just read that Access doesn't save macros as VBA code? It's true that Access doesn't save macros as VBA code. However, something first introduced in Access 97 is the built-in capability to convert a macro to VBA code. After you've done that, you have a VBA procedure, not a macro. You can edit the VBA procedure created from the macro to tailor it for your needs. Let's see how this works.

  1. Create a simple macro, such as one that displays the hourglass pointer.

  2. Save the macro.

  3. In the Database window, select the macro.

  4. Choose Tools, move the pointer onto Macro, and choose Convert Macros to Visual Basic. Access displays the dialog box shown in Figure 1.

  5. Figure 1 The Convert Macro dialog box offers two choices.

  6. You probably want to leave the two check boxes checked, so choose Convert. After a brief delay, a message box appears declaring Conversion Finished. Choose OK.

  7. In the Database window's Objects bar, select Modules. The list of modules includes one named Converted Macro.

  8. Select the Converted Macro module and choose Design. The Visual Basic Editor (VBE) window opens, displaying the VBA function that the conversion process has created, as shown in Figure 2.

Figure 2 The conversion process creates a VBA function that includes error trapping.


To quickly gain an understanding of VBA code, create several macros, convert them to VBA code, and examine the code.

You can edit the VBA code created by converting macros to tailor that code to suit your specific needs.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.