Solved

Copying records from one RecordSet to another RecordSet

Posted on 2008-10-02
16
1,393 Views
Last Modified: 2013-12-25
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.
0
Comment
Question by:Gupi
  • 6
  • 6
  • 4
16 Comments
 
LVL 7

Expert Comment

by:fluglash
ID: 22631815
you`d better use SQL:

select * from table_name
where field1 > value1

or you still need to copy it?
0
 

Author Comment

by:Gupi
ID: 22631836
Need to copy from RS1 to RS1.
cant use SQL on RS2.
0
 
LVL 7

Expert Comment

by:fluglash
ID: 22631848
use SQL to open rs1, or open rs2 with that SQL
0
 

Author Comment

by:Gupi
ID: 22631860
please read again.
 i need to copy from rs1 to rs2.
i cant use sql to open rs2.
0
 
LVL 7

Expert Comment

by:fluglash
ID: 22631915
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
 

Author Comment

by:Gupi
ID: 22632033
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
 
LVL 7

Expert Comment

by:fluglash
ID: 22632123
0
 
LVL 45

Expert Comment

by:aikimark
ID: 22638363
What do you want to do with RS2?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 45

Accepted Solution

by:
aikimark earned 125 total points
ID: 22638904
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
 
LVL 45

Expert Comment

by:aikimark
ID: 22638929
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
 

Author Comment

by:Gupi
ID: 22640668
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
 
LVL 45

Expert Comment

by:aikimark
ID: 22641638
@Gupi

Again I ask you *WHAT* you need to do with these 'copied' records.  It makes a difference.
0
 

Author Comment

by:Gupi
ID: 22643145
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
 
LVL 45

Expert Comment

by:aikimark
ID: 22644310
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
 

Author Comment

by:Gupi
ID: 22647075
>>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
 
LVL 45

Expert Comment

by:aikimark
ID: 22649380
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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now