Copying records from one RecordSet to another RecordSet

i am having two recordsets, first one RS1 is associated with a table using "Select * from TableName" and  is Opened.
2nd one RS2, is not yet opened.
i want to copy few records from RS1 which a criteria, say FieldName1 > Value1, to RS2.
RS2 is not associated with any table.
how can i do this, Please?

Thanks.
GupiAsked:
Who is Participating?
 
aikimarkCommented:
you can save the RS1 field values from the eligible rows in a VB collection object.  see snippet selecting one of every 1500 records
Sub main()
    Dim db As Database
    Dim rs1 As Recordset
    Dim lngRec As Long
    Dim colRecs As New Collection
    Dim varRec As Variant
    Dim colFields As Collection
    Dim fldThing As Field
    Dim varField As Variant
    
    Set db = OpenDatabase("~~~~~~~~~~~~~~.mdb")
    Set rs1 = db.OpenRecordset("select * from ~~~~~~")
    
    Do Until rs1.EOF
        If (lngRec Mod 1500) = 0 Then
            Set colFields = New Collection
            For Each fldThing In rs1.Fields
                colFields.Add fldThing.Value
            Next
            colRecs.Add colFields
        End If
        rs1.MoveNext
        lngRec = lngRec + 1
    Loop
    db.Close
    For Each varRec In colRecs
        For Each varField In varRec
            Debug.Print varField, ;
        Next
        Debug.Print vbCr; "_____"
    Next
    
    End
End Sub

Open in new window

0
 
fluglashCommented:
you`d better use SQL:

select * from table_name
where field1 > value1

or you still need to copy it?
0
 
GupiAuthor Commented:
Need to copy from RS1 to RS1.
cant use SQL on RS2.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
fluglashCommented:
use SQL to open rs1, or open rs2 with that SQL
0
 
GupiAuthor Commented:
please read again.
 i need to copy from rs1 to rs2.
i cant use sql to open rs2.
0
 
fluglashCommented:
Dim p As New PropertyBag

p.WriteProperty "Tmp", rs1
Set rs2 = p.ReadProperty("Tmp")

Presto! A genuine copy.

A slightly better solution is to loop through the Fields collection, and
assign values to columns that way. You can't use For Each with multiple
collections, unfortunately.

Dim i As Long
With rsSource
Do While Not .EOF
With .Fields
For i = 0 To (.Count - 1)
rsDest.Fields(i).Value = .Item(i).Value
Next
End With
.MoveNext
Loop
End With

Notes:
- This is much faster than referencing each field by name
- It requires the two tables to have the same columns, in the same order
- using With makes a big difference

You could also experiment with using Field objects, which some people say is
the fastest way of all:

Dim fldSrcCustomerNo As ADODB.Field
Dim fldDstCustomerNo As ADODB.Field
'etc...

Set fldSrcCustomerNo = rsSource.Fields("Customer_No")
Set fldDstCustomerNo = rsDest.Fields("Customer_No")

Do While Not rsSource.EOF
fldDstCustomerNo.Value = fldSrcCustomerNo.Value
Loop
0
 
GupiAuthor Commented:
Thanks.
This is better and easy to understand.
>> Dim i As Long
With rsSource
Do While Not .EOF
With .Fields
For i = 0 To (.Count - 1)
rsDest.Fields(i).Value = .Item(i).Value
Next
End With
.MoveNext
Loop
End With

but before this, i know, i will open rssource using
Rssource.open SqlQuery, Conn.
but do i need to open RsDest also.
how ?
0
 
fluglashCommented:
0
 
aikimarkCommented:
What do you want to do with RS2?
0
 
aikimarkCommented:
You might also add the RS1 (eligible) rows' bookmarks to a collection.  This would allow you to quickly revisit those rows for later processing.
0
 
GupiAuthor Commented:
acutally my i am working with Oracle as Backend.
i want to fetch some records from table in RS1 using Qry1, some in RS2 using Qry2 and still some in RS3 using Qry3, then from these RS1, RS2 and RS3  i want to Some RANDOM records in RS4.

how to do this ?
Please help.
thanks.

0
 
aikimarkCommented:
@Gupi

Again I ask you *WHAT* you need to do with these 'copied' records.  It makes a difference.
0
 
GupiAuthor Commented:
from original recordsets RS1, Rs2, Rs3, i want to copy random records to RS4.
actually RS1 has All questions on Sub1, RS2 has All questions on Sub2, RS3 has All questions on Sub3
from Questions Table.
Now i want to show the user RANDOM questions on EACH of THREE  subjects.

so first i collected questions on each subject seperately in differnt RecordSet.
from here i will fetch random question in another recordset RS4. (Before fetching RS4 i was adding them to a Temporay Table from where there are shown to user) but now i want to do it without creating Temp table. i want to directly fetch them in RS4 to show user.
so i need to know how can i copy records from a record set to another recordst without first sending them in any temporary table.
i hope it is clear now.
please, ask me more if needed.
Please, look into it and reply.
Thanks.
0
 
aikimarkCommented:
What do you mean by "show user"?

What fields from these recordsets are required to be shown to the user?

Once the user has seen these questions, what happens?  For instance, is the information in the recordsets updated with an answer?
0
 
GupiAuthor Commented:
>>What do you mean by "show user"?
to present records to the user on the form.

>>What fields from these recordsets are required to be shown to the user?
A subset of fileld will be shown, rest will be used in some other processing.(Like to compare  user's reply)

>>Once the user has seen these questions, what happens?  For instance, is the information in the recordsets updated with an answer?
No. recordset will not be updated.

Thanks.
0
 
aikimarkCommented:
HOW are you presenting data to the user on the form?

WHY do you need to use a recordset to cache your eligible records?

================
While answering the above questions, please give details about your program.  We need context to give you the best answer.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.