Applies To Connection object, Database object, Dynaset-Type Recordset object, QueryDef object, Recordset object, Snapshot-Type Recordset object, Table-Type Recordset object, TableDef object.
Description
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. |
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. |
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). |
Constant | Description |
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). |
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.See Also Cancel method, Connect property, StillExecuting property, Type property.
Example 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.Sub OpenRecordsetX()
Dim wrkJet As Workspace
Dim wrkODBC As Workspace
Dim dbsNorthwind As Database
Dim conPubs As Connection
Dim rstTemp As Recordset
Dim rstTemp2 As Recordset
' Open Microsoft Jet and ODBCDirect workspaces, Microsoft
' Jet database, and ODBCDirect connection.
Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)
Set wrkODBC = CreateWorkspace("", "admin", "", dbUseODBC)
Set dbsNorthwind = wrkJet.OpenDatabase("Northwind.mdb")
Set conPubs = wrkODBC.OpenConnection("", , , _
"ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Publishers")
' Open five different Recordset objects and display the
' contents of each.
Debug.Print "Opening forward-only-type recordset " & _
"where the source is a QueryDef object..."
Set rstTemp = dbsNorthwind.OpenRecordset( _
"Ten Most Expensive Products", dbOpenForwardOnly)
OpenRecordsetOutput rstTemp
Debug.Print "Opening read-only dynaset-type " & _
"recordset where the source is an SQL statement..."
Set rstTemp = dbsNorthwind.OpenRecordset( _
"SELECT * FROM Employees", dbOpenDynaset, dbReadOnly)
OpenRecordsetOutput rstTemp
' Use the Filter property to retrieve only certain
' records with the next OpenRecordset call.
Debug.Print "Opening recordset from existing " & _
"Recordset object to filter records..."
rstTemp.Filter = "LastName >= 'M'"
Set rstTemp2 = rstTemp.OpenRecordset()
OpenRecordsetOutput rstTemp2
Debug.Print "Opening dynamic-type recordset from " & _
"an ODBC connection..."
Set rstTemp = conPubs.OpenRecordset( _
"SELECT * FROM stores", dbOpenDynamic)
OpenRecordsetOutput rstTemp
' Use the StillExecuting property to determine when the
' Recordset is ready for manipulation.
Debug.Print "Opening snapshot-type recordset based " & _
"on asynchronous query to ODBC connection..."
Set rstTemp = conPubs.OpenRecordset("publishers", _
dbOpenSnapshot, dbRunAsync)
Do While rstTemp.StillExecuting
Debug.Print " [still executing...]"
Loop
OpenRecordsetOutput rstTemp
rstTemp.Close
dbsNorthwind.Close
conPubs.Close
wrkJet.Close
wrkODBC.Close
End Sub
Sub OpenRecordsetOutput(rstOutput As Recordset)
' Enumerate the specified Recordset object.
With rstOutput
Do While Not .EOF
Debug.Print , .Fields(0), .Fields(1)
.MoveNext
Loop
End With
End Sub
Example (Microsoft Access)
The following example opens a dynaset-type Recordset object and prints the number of records in the Recordset object.
Sub UKOrders()
Dim dbs As Database, rst As Recordset
Dim strSQL As String
' Return reference to current database.
Set dbs = CurrentDb
strSQL = "SELECT * FROM Orders WHERE [ShipCountry] = 'UK'"
Set rst = dbs.OpenRecordset(strSQL)
rst.MoveLast
Debug.Print rst.RecordCount
rst.Close
Set dbs = Nothing
End Sub
Example (Microsoft Excel)
This example displays a custom dialog box that contains a list of all the available recordsets in the Nwindex.mdb database. The example opens a new recordset based on the recordset selected by the user and then copies the records Dim db As Database, rs1 As Recordset
Set db = Workspaces(0).OpenDatabase(Application.Path & "\NWINDEX.MDB")
Application.ScreenUpdating = False
Set theDialog = DialogSheets.Add
Set list1 = theDialog.ListBoxes.Add(78, 42, 84, 80)
Set label1 = theDialog.Labels.Add(78, 125, 240, 25)
label1.Text = "Recordsets for " & db.Name
i = 0
Do Until i = db.TableDefs.Count
list1.AddItem (db.TableDefs(i).Name)
i = i + 1
Loop
Sheets("Sheet1").Activate
Application.ScreenUpdating = True
If theDialog.Show = True Then
If list1.Value = 0 Then
MsgBox "You have not selected an item from the list."
Else
Set rs1 = db.OpenRecordset(db.TableDefs(list1.Value - 1).Name)
ActiveCell.CopyFromRecordset rs1
End If
End If
rs1.Close
db.Close