Friday, December 30, 2011

Cloning a Recordset

You can use Clone() method of ADODB.Recordset object to create a duplicate recordset object from an existing recordset object. An important advantage of this method is that instead of creating two independent recordsets, it creates two recordset objects that point to the same recordset. This feature allows you to change the current record in the clone, while keeping the current record same in the original recordset object or vice versa.
Using this method is more efficient than creating a new recordset with the same source definition: It saves an extra trip to the database server. You should note that you can only clone a Recordset object that supports bookmarks. If you make changes to a recordset, then those changes will be visible in all clones. If you execute Requery on the original Recordset, the clones will no longer be synchronized to the original. Similarly if you close the original recordset then it won't close the clones or vice versa. Here is a sample method that makes use of Clone method:
 
Private Sub CloneRecordset(ByVal SourceRecordset As 
ADODB.Recordset, ByRef 
TargetRecordset As ADODB.Recordset)

    ' Only boomarkable recordsets support Clone method
    If (SourceRecordset.Supports(adBookmark)) Then
        Set TargetRecordset = SourceRecordset.Clone()
    End If

End Sub

No comments: