OpenRecordset Method
Creates a new Recordset object and appends it to the Recordsets collection.
Syntax
For Connection and Database objects:
Set recordset = object.OpenRecordset (source, type, options, lockedits)
For QueryDef, Recordset, and TableDef objects:
Set recordset = object.OpenRecordset (type, options, lockedits)
The OpenRecordset method syntax has these parts.
Part | Description |
---|---|
recordset | An object variable that represents the Recordset object you want to open. |
object | An object variable that represents an existing object from which you want to create the new Recordset. |
source | A String specifying the source of the records for the new Recordset. The source can be a table name, a query name, or an SQL statement that returns records. For table-type Recordset objects in Microsoft Jet databases, the source can only be a table name. |
type | Optional. A constant that indicates the type of Recordset to open, as specified in Settings. |
options | Optional. A combination of constants that specify characteristics of the new Recordset, as listed in Settings. |
lockedits | Optional. A constant that determines the locking for the Recordset, as specified in Settings. |
Settings
You can use one of the following constants for the type argument.
Constant | Description |
---|---|
dbOpenTable | Opens a table-type Recordset object (Microsoft Jet workspaces only). |
dbOpenDynamic | Opens a dynamic-type Recordset object, which is similar to an ODBC dynamic cursor. (ODBCDirect workspaces only) |
dbOpenDynaset | Opens a dynaset-type Recordset object, which is similar to an ODBC keyset cursor. |
dbOpenSnapshot | Opens a snapshot-type Recordset object, which is similar to an ODBC static cursor. |
dbOpenForwardOnly | Opens a forward-only-type Recordset object. |
Note If you open a Recordset in a Microsoft Jet workspace and you don't specify a type, OpenRecordset creates a table-type Recordset, if possible. If you specify a linked table or query, OpenRecordset creates a dynaset-type Recordset. In an ODBCDirect workspace, the default setting is dbOpenForwardOnly.
You can use a combination of the following constants for the options argument.
Constant | Description |
---|---|
dbAppendOnly | Allows users to append new records to the Recordset, but prevents them from editing or deleting existing records (Microsoft Jet dynaset-type Recordset only). |
dbSQLPassThrough | Passes an SQL statement to a Microsoft Jet-connected ODBC data source for processing (Microsoft Jet snapshot-type Recordset only). |
dbSeeChanges | Generates a run-time error if one user is changing data that another user is editing (Microsoft Jet dynaset-type Recordset only). This is useful in applications where multiple users have simultaneous read/write access to the same data. |
dbDenyWrite | Prevents other users from modifying or adding records (Microsoft Jet Recordset objects only). |
dbDenyRead | Prevents other users from reading data in a table (Microsoft Jet table-type Recordset only). |
dbForwardOnly | Creates a forward-only Recordset (Microsoft Jet snapshot-type Recordset only). It is provided only for backward compatibility, and you should use the dbOpenForwardOnly constant in the type argument instead of using this option. |
dbReadOnly | Prevents users from making changes to the Recordset (Microsoft Jet only). The dbReadOnly constant in the lockedits argument replaces this option, which is provided only for backward compatibility. |
dbRunAsync | Runs an asynchronous query (ODBCDirect workspaces only). |
dbExecDirect | Runs a query by skipping SQLPrepare and directly calling SQLExecDirect (ODBCDirect workspaces only). Use this option only when you’re not opening a Recordset based on a parameter query. For more information, see the "Microsoft ODBC 3.0 Programmer’s Reference." |
dbInconsistent | Allows inconsistent updates (Microsoft Jet dynaset-type and snapshot-type Recordset objects only). |
dbConsistent | Allows only consistent updates (Microsoft Jet dynaset-type and snapshot-type Recordset objects only). |
Note The constants dbConsistent and dbInconsistent are mutually exclusive, and using both causes an error. Supplying a lockedits argument when options uses the dbReadOnly constant also causes an error.
You can use the following constants for the lockedits argument.
Constant | Description |
---|---|
dbReadOnly | Prevents users from making changes to the Recordset (default for ODBCDirect workspaces). You can use dbReadOnly in either the options argument or the lockedits argument, but not both. If you use it for both arguments, a run-time error occurs. |
dbPessimistic | Uses pessimistic locking to determine how changes are made to the Recordset in a multiuser environment. The page containing the record you're editing is locked as soon as you use the Edit method (default for Microsoft Jet workspaces). |
dbOptimistic | Uses optimistic locking to determine how changes are made to the Recordset in a multiuser environment. The page containing the record is not locked until the Update method is executed. |
dbOptimisticValue | Uses optimistic concurrency based on row values (ODBCDirect workspaces only). |
dbOptimisticBatch | Enables batch optimistic updating (ODBCDirect workspaces only). |
Remarks
In a Microsoft Jet workspace, if object refers to a QueryDef object, or a dynaset- or snapshot-type Recordset, or if source refers to an SQL statement or a TableDef that represents a linked table, you can't use dbOpenTable for the type argument; if you do, a run-time error occurs. If you want to use an SQL pass-through query on a linked table in a Microsoft Jet-connected ODBC data source, you must first set the Connect property of the linked table's database to a valid ODBC connection string. If you only need to make a single pass through a Recordset opened from a Microsoft Jet-connected ODBC data source, you can improve performance by using dbOpenForwardOnly for the type argument.
If object refers to a dynaset- or snapshot-type Recordset, the new Recordset is of the same type object. If object refers to a table-type Recordset object, the type of the new object is a dynaset-type Recordset. You can't open new Recordset objects from forward-only–type or ODBCDirect Recordset objects.
In an ODBCDirect workspace, you can open a Recordset containing more than one select query in the source argument, such as
"SELECT LastName, FirstName FROM Authors
WHERE LastName = 'Smith';
SELECT Title, ISBN FROM Titles
WHERE ISBN Like '1-55615-*'"
The returned Recordset will open with the results of the first query. To obtain the result sets of records from subsequent queries, use the NextRecordset method.
Note You can send DAO queries to a variety of different database servers with ODBCDirect, and different servers will recognize slightly different dialects of SQL. Therefore, context-sensitive Help is no longer provided for Microsoft Jet SQL, although online Help for Microsoft Jet SQL is still included through the Help menu. Be sure to check the appropriate reference documentation for the SQL dialect of your database server when using either ODBCDirect connections or pass-through queries in Microsoft Jet-connected client/server applications.
Use the dbSeeChanges constant in a Microsoft Jet workspace if you want to trap changes while two or more users are editing or deleting the same record. For example, if two users start editing the same record, the first user to execute the Update method succeeds. When the second user invokes the Update method, a run-time error occurs. Similarly, if the second user tries to use the Delete method to delete the record, and the first user has already changed it, a run-time error occurs.
Typically, if the user gets this error while updating a record, your code should refresh the contents of the fields and retrieve the newly modified values. If the error occurs while deleting a record, your code could display the new record data to the user and a message indicating that the data has recently changed. At this point, your code can request a confirmation that the user still wants to delete the record.
You should also use the dbSeeChanges constant if you open a Recordset in a Microsoft Jet-connected ODBC workspace against a Microsoft SQL Server 6.0 (or later) table that has an IDENTITY column, otherwise an error may result.
In an ODBCDirect workspace, you can execute asynchronous queries by setting the dbRunAsync constant in the options argument. This allows your application to continue processing other statements while the query runs in the background. But, you cannot access the Recordset data until the query has completed. To determine whether the query has finished executing, check the StillExecuting property of the new Recordset. If the query takes longer to complete than you anticipated, you can terminate execution of the query with the Cancel method.
Opening more than one Recordset on an ODBC data source may fail because the connection is busy with a prior OpenRecordset call. One way around this is to use a server-side cursor and ODBCDirect, if the server supports this. Another solution is to fully populate the Recordset by using the MoveLast method as soon as the Recordset is opened.
If you open a Connection object with DefaultCursorDriver set to dbUseClientBatchCursor, you can open a Recordset to cache changes to the data (known as batch updating) in an ODBCDirect workspace. Include dbOptimisticBatch in the lockedits argument to enable update caching. See the Update method topic for details about how to write changes to disk immediately, or to cache changes and write them to disk as a batch.
Closing a Recordset with the Close method automatically deletes it from the Recordsets collection.
Note If source refers to an SQL statement composed of a string concatenated with a non-integer value, and the system parameters specify a non-U.S. decimal character such as a comma (for example, strSQL = "PRICE > " & lngPrice
, and lngPrice = 125,50
), an error occurs when you try to open the Recordset. This is because during concatenation, the number will be converted to a string using your system's default decimal character, and SQL only accepts U.S. decimal characters.