Understanding Objects and Collections

As you’ve seen in earlier chapters of this book, you can produce powerful applications in Microsoft Access without programming. However, when you need a more sophisticated application, writing Visual Basic code gives you a greater degree of control over what your application does. When you program in Visual Basic, you work with objects that correspond to different aspects of your database. Collections are sets of objects of the same type. Programming with objects and collections gives you added flexibility in that you can design your application to respond to user actions and input in a customized way.

Objects available to you in Microsoft Access come from four different sources:

Many of the objects that you work with in Visual Basic correspond to specific parts of your Microsoft Access database. For example, the Microsoft Access Form and Report objects correspond to your forms and reports, while the DAO TableDef and QueryDef objects correspond to your tables and queries. The Microsoft Office CommandBar object corresponds to the toolbars, menu bars, menus, and shortcut menus you see in Microsoft Access.

Other objects you may work with in Visual Basic are more abstract. For example, the Visual Basic Err object contains information about errors that occur while Visual Basic code is running, rather than corresponding to a specific part of your database.

Even though the objects available to you in Microsoft Access come from several different sources and perform different functions, you can work with them in similar ways. You’ll find that the concepts you need to understand in order to program with one object apply to most objects.

For example, every object has properties and methods associated with it. Once you understand how to work with one object’s properties and methods, you can use those same concepts to work with any object’s properties and methods. You use properties to determine or change some characteristic of an object. For example, you use a form’s Visible property to determine whether or not the form is visible to the user. You use methods to perform a particular operation on an object. For example, the Repaint method completes any pending screen updates on a specified form.

Organization of Objects and Collections

Each application that supplies objects to Microsoft Access provides an object library. The object library contains information about an application’s objects and their properties and methods. Microsoft Access includes four built-in object libraries, which are described in the following sections.

Microsoft Access objects and DAO objects are organized in object hierarchies. In an object hierarchy, certain objects contain other objects and collections. A collection is a special type of object that is actually a set of all objects of a given type. When you refer to a collection in code, you are referring to all the objects in the set. For example, the Microsoft Access Application object contains a Forms collection, which contains individual Form objects. A Form object contains a Controls collection, which in turn contains individual Control objects. The following illustration shows this relationship.

You can think of a collection as an array of objects that’s already declared by Microsoft Access. Collections, like arrays, have elements, and you refer to the objects in a collection as elements of the collection. You can refer to an element of a collection by its name or by its position within the collection.

See Also   For more information on arrays, see “Arrays” in Chapter 4, “Working with Variables, Data Types, and Constants.”

The following sections describe the four object libraries included in Microsoft Access.

Microsoft Access Objects

You’re probably already familiar with some of the objects in the Microsoft Access 8.0 object library. Microsoft Access Form, Report, and Control objects correspond to your forms, reports, and controls. You use these objects to control the way you display the data in your database. The Application object and the Reference object make it easier to work with objects in other applications. The Module object gives you control over the Visual Basic modules in your database. You use the DoCmd object to include macro actions in your code and the Screen object to refer to the active object on the screen.

The following table describes the objects provided by the Microsoft Access 8.0 object library.

Object Description
Application Active Microsoft Access application
Control Control on a form or report
DoCmd Macro actions used in Visual Basic code
Form Open form, including subforms
Module Open standard module or class module
Reference Reference to an application’s object library
Report Open report, including subreports
Screen Screen that currently has the focus

See Also   For information on an individual object, search the Help index for the name of the object.

Of the objects listed in the preceding table, the Control, Form, Module, Reference, and Report objects belong to collections. The Application object is the top-level object in the hierarchy, and all other objects exist beneath it. The following illustration shows the hierarchical organization of Microsoft Access objects and collections.

DAO Objects

The Microsoft DAO 3.5 object library provides DAO objects, which you can use to work with the Microsoft Jet database engine. Some DAO objects represent the structure of your database and the relationships between the data in it. These objects include the Database, TableDef, QueryDef, Field, Index, Parameter, Property, and Relation objects. Other objects are responsible for the security of your database, including the Container, Document, User, Group, and Workspace objects. The Recordset object provides you with direct access to the data in the database. The DBEngine object gives you control over the Microsoft Jet database engine itself. The Connection object represents a network connection to an Open Database Connectivity (ODBC) database and is available only when you’re working with an ODBCDirect Workspace object.

See Also   For more information on the Connection object and ODBCDirect, search the Help index for “Connection object.”

The following table describes the DAO objects provided by the Microsoft DAO 3.5 object library.

Object Description
Connection Network connection to an ODBC database
Container Security and other information for various types of objects in the database
Database Open database
DBEngine Microsoft Jet database engine itself
Document Security and other information for individual objects in the database
Error Data access error information
Field Field in a table, query, recordset, index, or relation
Group Group account in Microsoft Jet’s current workgroup
Index Table index
Parameter Query parameter
Property Property of an object
QueryDef Saved query in a database
Recordset Set of records defined by a table or query
Relation Relationship between two table or query fields
TableDef Saved table in a database
User User account in Microsoft Jet’s current workgroup
Workspace Active Microsoft Jet session

Every object in the Microsoft DAO 3.5 object library belongs to an associated collection, except the DBEngine object. The DBEngine object is the top-level object that gives you access to all other objects in the collection, like the Microsoft Access Application object in the Microsoft Access object hierarchy. The following illustration shows the hierarchical organization of the DAO objects. For simplicity, it shows only the DBEngine object and the collections for all other objects in the object hierarchy.

Each DAO object also contains a Properties collection. The Properties collection contains Property objects that represent the properties available for each object in the DAO object hierarchy.

See Also   For more information on the Properties collection, see “The Properties Collection” later in this chapter.

Visual Basic Objects

The Visual Basic for Applications object library provides three objects to Microsoft Access, but these objects aren’t organized in an object hierarchy. None of them belong to a collection of other objects. The following table describes the objects provided by the Visual Basic for Applications object library.

Object Description
Collection User-defined collection
Debug Debug window
Err Information about Visual Basic errors

Microsoft Office Objects

The Microsoft Office 8.0 object library provides objects you can use to customize the appearance of your application or to implement some features common to Microsoft Office applications. For example, you can create customized toolbars and menu bars in code by using the CommandBar object. You can perform custom file searches by using the FileSearch object. You can also customize the Office Assistant to respond to the user’s actions.

Note   In order to use objects in the Microsoft Office 8.0 object library from Visual Basic, you must first set a reference to the object library. When you set a reference to an object library, you notify Visual Basic that you may want to use the objects in that library. To set a reference to the Microsoft Office 8.0 object library, open a module and click References on the Tools menu. Then select the Microsoft Office 8.0 Object Library check box in the Available References box.

Not all of the objects in the Microsoft Office 8.0 object library are useful in Microsoft Access. The following table describes some of the objects in the Microsoft Office 8.0 object library which Microsoft Access developers may find useful.

Object Description
Assistant The Office Assistant
Balloon Balloon associated with the Office Assistant
BalloonCheckBox Check box control for the balloon
BalloonLabel Label control for the balloon
CommandBar Toolbar, menu bar, menu, or shortcut menu
CommandBarButton Button on a CommandBar object
CommandBarComboBox Combo box control on a CommandBar object
CommandBarControl Any control on a CommandBar object
CommandBarPopup Pop-up control on a CommandBar object
FileSearch Microsoft Office file searching
FoundFiles Files found through file search operation

Note   The Office Assistant is not available in Microsoft Access run-time applications.