>
Using Data Access Objects
Data access objects provide a way to control your database from Visual Basic. If you are working in Microsoft Access, you can do many of the same things in Visual Basic code that you do from the Microsoft Access interface. Many data access objects have graphical representations in Microsoft Access. For instance, a TableDef object corresponds to a Microsoft Access table.
Programming with data access objects gives you a finer degree of control over your application than you have from the Microsoft Access interface, and enables you to adapt your application to changing environments. You don't have to create an entire database from Visual Basic. To speed development, you can create objects in Microsoft Access and write Visual Basic code later to enhance and complete your application.
If you are working in Microsoft Excel, you must use data access objects to manipulate your database, because Microsoft Excel does not have graphical representations of these objects. If you need to control a database from Microsoft Excel, you can create that database graphically in Microsoft Access and write code later to manipulate it from Microsoft Excel. Because both Microsoft Access and Microsoft Excel include the Jet database engine, both applications can use the same database files. Database files are saved with the .MDB filename extension.
Data access objects represent the different components of a database. The objects have a hierarchical relationship to one another; one type of object or collection contains or is contained by another type of object or collection. For a graphical representation of the data access object hierarchy, see "Data Access Object Hierarchy" in the Appendix or in online Help.
DBEngine Object
At the top of the hierarchy is the DBEngine object. This object represents the Jet database engine. The DBEngine object exists whenever you launch Microsoft Access or reference the Microsoft DAO 3.0 Object Library in Microsoft Excel. You don't have to create it explicitly.
Workspace Object
The Jet database engine supports multiple sessions, each of which begins when a particular user logs on and ends when that user logs off. Each session is represented by a Workspace object. Unless you're programming security into your application, you're usually using the default Workspace object implicitly.
Database Object
Within a session, you can open one or more databases, each of which is represented by a Database object. A Database object corresponds to a saved .MDB file. A Database object contains all the other objects and the data in your database. If you're working in Microsoft Access, the database that is currently open can be represented by a Database object.
TableDef, QueryDef Objects
Within a Database object are the tables that contain data, represented by TableDef objects. QueryDef objects represent saved queries of any type and contain combined data from one or more tables. These objects are stored in the database file, so you don't have to recreate them each time you use them. The data in a table or query is organized into fields, represented by Field objects.
Index Object
An Index object corresponds to an index on a table. An index is a cross-reference on one or more fields in a table and enables faster access to data. An index is also useful for defining some rules about data, such as whether the combination of data entered in certain fields must be unique.
Parameter Object
A Parameter object represents any parameters defined for a query. You can use Parameter objects to change the values of query parameters from code before you run the query.
Recordset Object
A Recordset object represents a set of data, or recordset. A recordset is created using data in a table or query. Unlike tables and queries, however, recordsets are not stored to disk and must be re-created each time you use them. Data in Recordset objects is organized into Field objects representing fields in the underlying table or query.
You can think of a TableDef or QueryDef object as an object that defines the structure of a table or query, and a Recordset object as the object that represents the data stored in the table or query. To add, delete, or change data in your database, you must create a Recordset. You can manipulate data by applying the methods of the Recordset and setting and returning its properties.
There are three types of Recordset objects: table-type, dynaset-type, and snapshot-type. Not all Recordset types are updatable in all situations. To determine when you should use each one, see the topics discussing these objects.
Relation Object
A Relation object represents a relationship between fields in two tables in a database. Relationships are a key structural component of your database, in that they link like fields in your tables. They also determine how data in one table is affected when corresponding data in another table is changed or deleted.
Container Object
A Container object represents a type of object in your database. Both Microsoft Access and Microsoft Excel provide the Databases, Tables, and Relations Container objects. Microsoft Access also provides Forms, Modules, Reports, and Scripts Container objects.
Document Object
A Document object represents an object of the type defined by its corresponding Container object. For instance, each database has at least one Table Document object within the Tables Container object. You use Container and Document objects together with User and Group objects to program security into your application.
User, Group Objects
A User object represents a user account with certain access permissions. A Group object represents a group of user accounts with common access permissions. You use User and Group objects together with Container and Document objects to program security into your application.
Error Object
An Error object stores information about data access errors that have occurred. Several data access errors can occur at the same time; information about each is stored in an Error object in the Errors collection. The Errors collection is cleared each time a new error occurs.
Property Object
Every data access object has a set of properties, which are represented by Property objects and stored in the Properties collection associated with that object.