Re: Copy Recordset
- From: "Terry Kreft" <terry.kreft@xxxxxxxxx>
- Date: Wed, 2 Aug 2006 23:31:24 +0100
"Mark" pointed out to me a quicker (and simpler) way of doing this.
The code below asumes that rs has already been instantiated and opened.
Dim xml As MSXML2.DOMDocument,
Dim rsCopy as ADODB.Recordset
Set xml = New MSXML2.DOMDocument
Set rsCopy = New ADODB.Recordset
rs.Save xml, adPersistXML ' save to IStream exposed by xml object
rsCopy.Open xml
--
Terry Kreft
"Chris Anderson" <tgmsn@xxxxxxxxxxx> wrote in message
news:a216bb975a68c8846e54b69d5d@xxxxxxxxxxxxxxxxxxxxx
Hello Lou,recordset?
You mean as in two, separate, distinctly different copies of the same
Or do you want two pointers to the same recordset?ADODB.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
fld.Attributes
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,
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: argusy
- Re: Copy Recordset
- References:
- Copy Recordset
- From: Lou
- Re: Copy Recordset
- From: Chris Anderson
- Copy Recordset
- Prev by Date: Re: Copy Recordset
- Next by Date: Re: Copy Recordset
- Previous by thread: Re: Copy Recordset
- Next by thread: Re: Copy Recordset
- Index(es):
Relevant Pages
|