Recordset Object
Access Developer Reference |
A Recordset object represents the records in a base table or the records that result from running a query.
Remarks
You use Recordset objects to manipulate data in a database at the record level. When you use DAO objects, you manipulate data almost entirely using Recordset objects. All Recordset objects are constructed using records (rows) and fields (columns). There are five types of Recordset objects:
- Table-type Recordset— representation in code of a base table that you can use to add, change, or delete records from a single database table (Microsoft Access workspaces only).
- Dynaset-type Recordset— the result of a query that can have updatable records. A dynaset-type Recordset object is a dynamic set of records that you can use to add, change, or delete records from an underlying database table or tables. A dynaset-type Recordset object can contain fields from one or more tables in a database. This type corresponds to an ODBC keyset cursor.
- Snapshot-type Recordset— a static copy of a set of records that you can use to find data or generate reports. A snapshot-type Recordset object can contain fields from one or more tables in a database but can't be updated. This type corresponds to an ODBC static cursor.
- Forward-only-type Recordset— identical to a snapshot except that no cursor is provided. You can only scroll forward through records. This improves performance in situations where you only need to make a single pass through a result set. This type corresponds to an ODBC forward-only cursor.
- Dynamic-type Recordset— a query result set from one or more base tables in which you can add, change, or delete records from a row-returning query. Further, records other users add, delete, or edit in the base tables also appear in your Recordset. This type corresponds to an ODBC dynamic cursor (ODBCDirect workspaces only).
Note ODBCDirect workspaces are not supported in Microsoft Office Access 2007. Use ADO if you want to access external data sources without using the Microsoft Access database engine.
You can choose the type of Recordset object you want to create using the type argument of the OpenRecordset method.
In a Microsoft Access workspace, if you don't specify a type, DAO attempts to create the type of Recordset with the most functionality available, starting with table. If this type isn't available, DAO attempts a dynaset, then a snapshot, and finally a forward-only type Recordset object.
In an ODBCDirect workspace, if you don't specify a type, DAO attempts to create the type of Recordset with the fastest query response, starting with forward-only. If this type isn't available, DAO attempts a snapshot, then a dynaset, and finally a dynamic- type Recordset object.
When creating a Recordset object using a non-linked TableDef object in a Microsoft Access workspace, table-type Recordset objects are created. Only dynaset-type or snapshot-type Recordset objects can be created with linked tables or tables in Microsoft Access database engine-connected ODBC databases.
A new Recordset object is automatically added to the Recordsets collection when you open the object, and is automatically removed when you close it.
Note |
---|
If you use variables to represent a Recordset object and the Database object that contains the Recordset, make sure the variables have the same scope, or lifetime. For example, if you declare a public variable that represents a Recordset object, make sure the variable that represents the Database containing the Recordset is also public, or is declared in a Sub or Function procedure using the Static keyword. |
You can create as many Recordset object variables as needed. Different Recordset objects can access the same tables, queries, and fields without conflicting.
Dynaset–, snapshot–, and forward–only–type Recordset objects are stored in local memory. If there isn't enough space in local memory to store the data, the Microsoft Access database engine saves the additional data to TEMP disk space. If this space is exhausted, a trappable error occurs.
The default collection of a Recordset object is the Fields collection, and the default property of a Field object is the Value property. Use these defaults to simplify your code.
When you create a Recordset object, the current record is positioned to the first record if there are any records. If there are no records, the RecordCount property setting is 0, and the BOF and EOF property settings are True.
You can use the MoveNext, MovePrevious, MoveFirst, and MoveLast methods to reposition the current record. Forward–only–type Recordset objects support only the MoveNext method. When using the Move methods to visit each record (or "walk" through the Recordset), you can use the BOF and EOF properties to check for the beginning or end of the Recordset object.
With dynaset- and snapshot-type Recordset objects in a Microsoft Access workspace, you can also use the Find methods, such as FindFirst, to locate a specific record based on criteria. If the record isn't found, the NoMatch property is set to True. For table-type Recordset objects, you can scan records using the Seek method.
The Type property indicates the type of Recordset object created, and the Updatable property indicates whether you can change the object's records.
Information about the structure of a base table, such as the names and data types of each Field object and any Index objects, is stored in a TableDef object.
To refer to a Recordset object in a collection by its ordinal number or by its Name property setting, use any of the following syntax forms:
Recordsets(0)
Recordsets("name")
Recordsets![name]
Note |
---|
You can open a Recordset object from the same data source or database more than once, creating duplicate names in the Recordsets collection. You should assign Recordset objects to object variables and refer to them by variable name. |
Example
This example demonstrates Recordset objects and the Recordsets collection by opening four different types of Recordsets, enumerating the Recordsets collection of the current Database, and enumerating the Properties collection of each Recordset.
Visual Basic for Applications |
---|
|
This example uses the OpenRecordset method to open five different Recordset objects and display their contents. The OpenRecordsetOutput procedure is required for this procedure to run.
Visual Basic for Applications |
---|
|
This example opens a dynamic-type Recordset object and enumerates its records.
Visual Basic for Applications |
---|
|
This example opens a dynaset-type Recordset and shows the extent to which its fields are updatable.
Visual Basic for Applications |
---|
|
This example opens a forward-only-type Recordset, demonstrates its read-only characteristics, and steps through the Recordset with the MoveNext method.
Visual Basic for Applications |
---|
|
This example opens a snapshot-type Recordset and demonstrates its read-only characteristics.
Visual Basic for Applications |
---|
|
This example opens a table-type Recordset, sets its Index property, and enumerates its records.
Visual Basic for Applications |
---|
|
See Also
- How to: Add a Record to a DAO Recordset
- How to: Change Tables Involved in a One-to-Many Relationship in a DAO Recordset
- How to: Count the Number of Records in a DAO Recordset
- How to: Create a DAO Recordset From a Query
- How to: Create a DAO Recordset From a Table In the Current Database
- How to: Delete a Record From a DAO Recordset
- How to: Detect the Limits of a DAO Recordset
- How to: Extract Data from a Record in a DAO Recordset
- How to: Find a Record in a Dynaset-Type or Snapshot-Type DAO Recordset
- How to: Find a Record in a Table-Type DAO Recordset
- How to: Find the Current Position in a DAO Recordset
- How to: Make Bulk Changes to a DAO Recordset
- How to: Mark a Position in a DAO Recordset
- How to: Modify a Query from a DAO Recordset
- How to: Modify an Existing Record in a DAO Recordset
- How to: Move Through a DAO Recordset
- How to: Read From and Write To a Field in a DAO Recordset
- How to: Return a Random Record from a DAO Recordset
- How to: Sort Data in a DAO Recordset
- How to: Synchronize a DAO Recordset's Record with a Form's Current Record
- How to: Track Design Changes to a Table-Type DAO Recordset
- How to: Use Transactions in a DAO Recordset