Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Copying records from one RecordSet to another RecordSet

Posted on 2008-10-02
16
Medium Priority
?
2,081 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 46

Expert Comment

by:aikimark
ID: 22638363
What do you want to do with RS2?
0
 
LVL 46

Accepted Solution

by:
aikimark earned 375 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 46

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 46

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 46

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 46

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

688 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