MicrosoftВ® Office Access(TM) 2007 Inside Out (Microsoft Office Access Inside Out)

Now that you’ve learned the fundamentals of using Microsoft Visual Basic, it’s time to put this knowledge into practice. In this chapter, you’ll learn how to create the Visual Basic code you need to automate many common tasks.

You can find dozens of examples of automation in the Conrad Systems Contacts, Housing Reservations, and Wedding List sample databases. As you explore the databases, whenever you see something interesting, open the form or report in Design view and take a look at the Visual Basic code behind the form or report. This chapter walks you through a few of the more interesting examples in these databases.

Note 

You can find the code explained in this chapter in the Conrad Systems Contacts (Contacts.accdb), Housing Reservations (Housing.accdb), and Wedding List (WeddingList.accdb) sample applications on the companion CD.

Why Aren’t We Using Macros?

Although you can certainly use macros to automate simple applications, macros have certain limitations. For example, as you might have noticed when examining the list of available events in Chapter 17, “Understanding Event Processing,” many events require or return parameters that can be passed to or read from a Visual Basic procedure but not a macro. Also, even though you can write a macro to handle general errors in forms and reports, you can’t really analyze errors effectively within a macro nor do much to recover from an error. And as you saw in Chapter 18, “Automating Your Application with Macros,” the debugging facilities for macros are very simplistic.

When to Use Macros

Use macros in your application in any of the following circumstances:

In fact, we can think of only two reasons you’ll ever need to use a macro:

When to Use Visual Basic

Although macros can be useful, a number of tasks cannot be carried out with macros, and there are others that are better implemented using a Visual Basic procedure. Use a Visual Basic procedure instead of a macro in any of the following circumstances:

Although you can now trap errors in macros in an application written using Office Access 2007, you don’t have complex evaluation structures such as Select Case to help you decipher and handle the error. Quite frankly, the only reason we might recommend using macros instead of Visual Basic is when you need to write an application that can run in a database that is not trusted. But the instant that you need to use a macro action that is not trusted (our favorites are SetValue and Quit), then you might as well automate your application with Visual Basic.

Категории