>
Part | Description |
duplicate | A variable of an object data type identifying the duplicate Recordset object you're creating. |
original | A variable of an object data type identifying the Recordset object you want to duplicate. |
Dim dbsNorthwind As DatabaseDim rstOriginal As Recordset, rstDuplicate _ As RecordsetDim strPosition As String
Set dbsNorthwind = DBEngine.Workspaces(0).Databases("Northwind.mdb") ' Create first Recordset. Set rstOriginal = dbsNorthwind![Orders].OpenRecordset(dbOpenDynaset) strPosition = rstOriginal.Bookmark ' Save current record position. rstDuplicate = rstOriginal.Clone () ' Create duplicate. Recordset.rstDuplicate.Bookmark = strPosition ' Go to same record.Example (Microsoft Access) The following example creates a Recordset object using an SQL statement on an Employees table and then uses the Clone method to create a clone of the Recordset object so that bookmarks can be shared between the two objects. This technique is especially useful when you have to compare the results of a query from more than one point at the same time. The following function creates a dynaset-type Recordset object on an Employees table, then creates a clone of that Recordset object. The function reads and stores in a string variable the value of the Bookmark property for the current record, in this case the second record in the original Recordset object. The Bookmark property of the duplicate Recordset object is set to this string, making the second record the current record in this Recordset object as well. The current values of the LastName field for both recordsets are identical, as you can see once they are printed in the Debug window.
Sub CreateClone() Dim dbs As Database Dim rstEmployees As Recordset, rstDuplicate As Recordset Dim fldName As Field, strBook As String ' Return Database variable that points to current database. Set dbs = CurrentDb ' Open dynaset-type Recordset object. Set rstEmployees = dbs.OpenRecordset("SELECT * FROM Employees " & _ " ORDER by [LastName]") ' Clone Recordset object. Set rstDuplicate = rstEmployees.Clone Set fldName = rstEmployees.Fields!LastName ' Set current record. rstEmployees.MoveFirst ' Move to second record. rstEmployees.MoveNext ' Get Bookmark value and print current field value. If rstEmployees.Bookmarkable Then strBook = rstEmployees.Bookmark Debug.Print fldName.value Else ' If Recordset object doesn't support bookmarks, exit procedure. Exit Sub End If ' Set Bookmark property of clone to obtained value.
rstDuplicate.Bookmark = strBook Debug.Print fldName.value End SubExample (Microsoft Excel) This example displays a custom dialog box containing the lists of data from the CONTACTS and CUSTMR_ID fields in the Customer recordset of the NWINDEX.MDB database. To create the NWINDEX.MDB database, run the Microsoft Excel example for the CreateDatabase method.
Dim db As Database Dim rs1 As Recordset, rs2 As Recordset Set db = Workspaces(0).OpenDatabase(Application.Path & "\NWINDEX.MDB") Set rs1 = db.OpenRecordset("SELECT * FROM Customer" _ & " WHERE [REGION] = 'WA' ORDER BY [CUSTMR_ID];") Set theDialog = DialogSheets.Add Set list1 = theDialog.ListBoxes.Add(78, 42, 84, 80) Set list2 = theDialog.ListBoxes.Add(183, 42, 84, 80) Set rs2 = rs1.Clone() rs2.MoveFirst Do Until rs1.EOF list1.AddItem (rs1.Fields("CONTACT").Value) rs1.MoveNext Loop Do Until rs2.EOF list2.AddItem (rs2.Fields("CUSTMR_ID").Value) rs2.MoveNext Loop rs1.Close rs2.Close db.Close