Referring to Objects from VBA

VBA is able to control objects in Access (and other programs in Microsoft Office) because of Automation (with a capital A) technology. The idea behind Automation is this: A program, database, document, or some special capability exposes (makes available) its objects through an object library. The object library contains an organized set of names that VBA can refer to when it wants to manipulate some object.

Think of an object library as sort of a steering wheel that's sticking out of some database or some program. When the steering wheel isn't available, VBA can't manipulate objects in the program. However, when the steering wheel is exposed, VBA can manipulate objects inside that program. As I discuss in a moment, you control which steering wheels are available by setting references to object libraries.

Figure 2-7 shows a hypothetical example where the Access and Excel object models (steering wheels) are exposed. VBA can therefore manipulate objects in those programs. In that same figure, Word and PowerPoint aren't exposing their objects, so VBA can't manipulate objects in those programs.

Object libraries

Figure 2-7:

Object libraries expose objects to VBA.

Object libraries

Microsoft PowerPoint

Microsoft Word

Not all object libraries expose objects in specific Office programs. Some object libraries expose programs; some object libraries expose documents. Still others expose technologies that simply help you bridge the gaps between programs. Access, by itself, offers several object models. The important point is, though, that before you start writing VBA code, you need to know what object libraries are available to you.

0 0

Post a comment