Re: Copy Recordset
- From: Chris Anderson <tgmsn@xxxxxxxxxxx>
- Date: Wed, 2 Aug 2006 22:15:04 +0000 (UTC)
Hello Lou,
You mean as in two, separate, distinctly different copies of the same recordset? Or do you want two pointers to the same recordset?
If what you need are two copies of the same recordset that are independant of each other, then give this function a try:
Private Function DisconnectedClone(ByVal rstData As ADODB.Recordset) As ADODB.Recordset
Dim fld As ADODB.Field
Dim rst As ADODB.Recordset
On Error GoTo errHandler
'Create a recordset object
Set rst = New ADODB.Recordset
'Copy the field definition
For Each fld In rstData.Fields
rst.Fields.Append fld.Name, fld.Type, fld.DefinedSize, fld.Attributes
If fld.Precision > 0 Then
rst.Fields(fld.Name).Precision = fld.Precision
End If
If fld.NumericScale > 0 Then
rst.Fields(fld.Name).NumericScale = fld.NumericScale
End If
Next
'Use a client cursor
rst.CursorLocation = adUseClient
'Open the recordset
rst.Open , , adOpenKeyset
'loop through the source recordset and copy the data
Do While Not rstData.EOF
'Add a new records
rst.AddNew
'Copy the field values
For Each fld In rstData.Fields
rst.Fields(fld.Name).Value = rstData.Fields(fld.Name).Value
Next
'Next record
rstData.MoveNext
Loop
'If there was data to roll through,
If rst.RecordCount > 0 Then
'move to the begining of the source recordset
rst.MoveFirst
End If
'Return the clone
Set DisconnectedClone = rst
'Release objects
Set rst = Nothing
Set fld = Nothing
Exit Function
errHandler:
On Error GoTo 0
Err.Raise Err.Number, Err.Source, Err.Description
End Function
Usage:
Set rstCopyTwo = DisconnectedClone(rstOriginal)
It copies the structure, then the data. End result are two recordsets that are identical, but independant of each other.
-Chris Anderson [MVP-VB]
How can I make a copy, NOT a clone, of a recordset?
-Lou
.
- Follow-Ups:
- Re: Copy Recordset
- From: Terry Kreft
- Re: Copy Recordset
- References:
- Copy Recordset
- From: Lou
- Copy Recordset
- Prev by Date: Copy Recordset
- Next by Date: Re: Copy Recordset
- Previous by thread: Copy Recordset
- Next by thread: Re: Copy Recordset
- Index(es):
Relevant Pages
|