RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB6
Expertise: Intermediate
Feb 19, 2000

Quickly create a copy of an ADO Recordset

When you want to process the data in a Recordset without affecting the actual values in the database, often the Clone method isn't what you need. For example, if you delete the records in a cloned Recordset, the original data in the database is also affected.

It seems that the only way to create a copy of a Recordset that is completely unrelated to the original Recordset is to create a disconnected Recordset that duplicates the structure and the data of the original one. However, if the original Recordset has its CursorLocation property set to adUseClient, there is a much more efficient shortcut:

Dim rsSource As New ADODB.Recordset
Dim rsFiltered As ADODB.Recordset
Dim rsSorted As ADODB.Recordset
Dim pb As New PropertyBag

' open a client-side Recordset    
rsSource.CursorLocation = adUseClient
rsSource.Open "Authors", "DSN=Pubs", adOpenStatic

' apply a filter 
rsSource.Filter = "author like 'J*'"

' create a copy of the filtered recordset
pb.WriteProperty "filtered", rsSource
Set rsFiltered = pb.ReadProperty("filtered")

' sort the recordset
rsSource.Filter = ""
rsSource.Sort = "Author"

' create a copy of the sorted recordset
pb.WriteProperty "sorted", rsSource
Set rsSorted = pb.ReadProperty("sorted")

' release the memory
Set pb = Nothing

Francesco Balena
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date