Open Method (ADO Recordset)

Opens a cursor.

Syntax

recordset.Open Source, ActiveConnection, CursorType, LockType, Options

Parameters

Source Optional. A Variant that evaluates to a valid Command object variable name, an SQL statement, a table name, a stored procedure call, or the file name of a persisted Recordset.

ActiveConnection Optional. Either a Variant that evaluates to a valid Connection object variable name, or a String containing ConnectionString parameters.

CursorType Optional. A CursorTypeEnum value that determines the type of cursor that the provider should use when opening the Recordset. Can be one of the following constants (see the CursorType property for definitions of these settings).

adOpenForwardOnly (Default) Opens a forward-only-type cursor.
adOpenKeyset Opens a keyset-type cursor.
adOpenDynamic Opens a dynamic-type cursor.
adOpenStatic Opens a static-type cursor.

LockType Optional. A LockTypeEnum value that determines what type of locking (concurrency) the provider should use when opening the Recordset. Can be one of the following constants (see the LockType property for more information).

adLockReadOnly (Default) Read-onlyùyou cannot alter the data.
adLockPessimistic Pessimistic locking, record by recordùthe provider does what is necessary to ensure successful editing of the records, usually by locking records at the data source immediately upon editing.
adLockOptimistic Optimistic locking, record by recordùthe provider uses optimistic locking, locking records only when you call the Update method.
adLockBatchOptimistic Optimistic batch updatesùrequired for batch update mode as opposed to immediate update mode.

Options Optional. A Long value that indicates how the provider should evaluate the Source argument if it represents something other than a Command object, or that the Recordset should be restored from a file where it was previously saved. Can be one of the following constants (see the CommandType property for a more detailed explanation of the first five constants in this list).

adCmdText Indicates that the provider should evaluate Source as a textual definition of a command.
adCmdTable Indicates that ADO should generate an SQL query to return all rows from the table named in Source.
adCmdTableDirect Indicates that the provider should return all rows from the table named in Source.
adCmdStoredProc Indicates that the provider should evaluate Source as a stored procedure.
adCmdUnknown Indicates that the type of command in the Source argument is not known.
adCommandFile Indicates that the persisted (saved) Recordset should be restored from the file named in Source.
adExecuteAsync Indicates that the Source should be executed asynchronously.
adFetchAsync Indicates that after the initial quantity specified in the CacheSize property is fetched, any remaining rows should be fetched asynchronously.

Remarks

Using the Open method on a Recordset object opens a cursor that represents records from a base table, the results of a query, or a previously saved Recordset.

Use the optional Source argument to specify a data source using one of the following: a Command object variable, an SQL statement, a stored procedure, a table name, or a complete file path name.

The ActiveConnection argument corresponds to the ActiveConnection property and specifies in which connection to open the Recordset object. If you pass a connection definition for this argument, ADO opens a new connection using the specified parameters. You can change the value of this property after opening the Recordset to send updates to another provider. Or, you can set this property to Nothing (in Microsoft Visual Basic) to disconnect the Recordset from any provider.

For the other arguments that correspond directly to properties of a Recordset object (Source, CursorType, and LockType), the relationship of the arguments to the properties is as follows:

Note For Recordset objects whose Source property is set to a valid Command object, the ActiveConnection property is read-only, even if the Recordset object isn't open.

If you pass a Command object in the Source argument and also pass an ActiveConnection argument, an error occurs. The ActiveConnection property of the Command object must already be set to a valid Connection object or connection string.

If you pass something other than a Command object in the Source argument, you can use the Options argument to optimize evaluation of the Source argument. If the Options argument is not defined, you may experience diminished performance because ADO must make calls to the provider to determine if the argument is an SQL statement, a stored procedure, or a table name. If you know what Source type you're using, setting the Options argument instructs ADO to jump directly to the relevant code. If the Options argument does not match the Source type, an error occurs.

The default for the Options argument is adCommandFile if no connection is associated with the recordset. This will typically be the case for persisted Recordset objects.

If the data source returns no records, the provider sets both the BOF and EOF properties to True, and the current record position is undefined. You can still add new data to this empty Recordset object if the cursor type allows it.

When you have concluded your operations over an open Recordset object, use the Close method to free any associated system resources. Closing an object does not remove it from memory; you can change its property settings and use the Open method to open it again later. To completely eliminate an object from memory, set the object variable to Nothing.

Call Open with no operands, and before the ActiveConnection property is set, to create an instance of a Recordset created by appending fields to the Recordset Fields collection.