Private Function DisconnectedCloneEx(ByVal rstData As ADODB.Recordset, Optional ByRef FieldList As ADODB.Recordset = Nothing, Optional ByVal PostPend As Boolean = True) As ADODB.Recordset
Dim fld As ADODB.Field
Dim rst As ADODB.Recordset
Dim lngFldCount As Long
On Error GoTo errHandler
'Create a recordset object
Set rst = New ADODB.Recordset
'If a Field collection was passed in and it is to be pre-pended to the recordset....
If (Not PostPend) And Not (FieldList Is Nothing) Then
'Copy the field definitions
For Each fld In FieldList.Fields
'We have to make sure the field is nullable
If (fld.Attributes And adFldIsNullable) <> adFldIsNullable Then
fld.Attributes = fld.Attributes + adFldIsNullable
End If
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
End If
'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
'If a Field collection was passed in and it is to be post-pended to the recordset....
If (PostPend) And Not (FieldList Is Nothing) Then
'Copy the field definition
For Each fld In FieldList.Fields
'We have to make sure the field is nullable
If (fld.Attributes And adFldIsNullable) <> adFldIsNullable Then
fld.Attributes = fld.Attributes + adFldIsNullable
End If
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
End If
'Use a client cursor
rst.CursorLocation = adUseClient
'Open the recordset
rst.Open , , adOpenKeyset
If Not (rstData.EOF And rstData.BOF) Then
rstData.MoveFirst
End If
'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 DisconnectedCloneEx = 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