Solved

Copy record from one table to another

Posted on 2000-02-16
8
1,271 Views
Last Modified: 2008-07-03
Hi,

It has been a long time ago, I have seen a command in Ms Access
that able to copy 1 row of data and paste that entire row to another table. I wonder how to do that, instead to use something like this :

pRecordSet_1("field1") = pRecordSet_2("field1")
pRecordSet_1("field2") = pRecordSet_2("field2")
pRecordSet_1("field3") = pRecordSet_2("field3")

It not flexible to program that way, because each timethe field name change, I have to modify the program.

Thank a lot

Add comment 1  on 23 feb 2000 :

The 2 tables will have differents numbers of fields and we don't have to know those fields name.
So that the reason I need a command to able me copy record by record and paste to another table.

0
Comment
Question by:woaini
8 Comments
 
LVL 10

Expert Comment

by:paasky
ID: 2528440
Hi woaini,

This can be also done with a query. Here's an example how to copy a specific record from table1 to table2:

INSERT INTO Table2 ( ID, fldTable2 )
SELECT Table1.ID, Table1.fldTable1
FROM Table1
WHERE Table1.ID=[Enter ID you want to copy: ];

Regards,
Paasky
0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2528637
paasky's suggestion is by far the best, but if you have to do it one record at a time, you can just refer to the fields within each table by their index:

....
pRecordSet_1(0) = pRecordSet_2(0)
pRecordSet_1(1) = pRecordSet_2(1)
pRecordSet_1(2) = pRecordSet_2(2)
....

and so on.  Then it won't matter is the fields change names, as long as the fields are in the same order in each table.

Wes

 
0
 
LVL 4

Expert Comment

by:srauda
ID: 2529141
Try this:

INSERT INTO Table1
SELECT Table2.*
FROM Table2;

No field names involved here either.
0
 
LVL 10

Expert Comment

by:paasky
ID: 2530010
srauda,

yes, that would do the job well if the two tables are identical and field names in same order.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:woaini
ID: 2552619
Edited text of question.
0
 
LVL 10

Accepted Solution

by:
paasky earned 50 total points
ID: 2553271
Here's a function which copies one record to another table. This is actually what wesleysteward already suggested but I wanted to make a complete code how to use it.

Public Function CopyRecord(SourceTable, TargetTable, StrCriteria) As Boolean
Dim rst1 As Recordset
Dim rst2 As Recordset
Dim iFieldCount As Byte
Dim i As Byte

    Set rst1 = CurrentDb.OpenRecordset("SELECT * FROM " & SourceTable & " " & StrCriteria)
    Set rst2 = CurrentDb.OpenRecordset(TargetTable)
   
    If rst1.BOF And rst1.EOF Then
        ' No Data to copy
        Set rst1 = Nothing
        Set rst2 = Nothing
        CopyRecord = False
        Exit Function
    End If
   
    ' fieldcount is always the number of fields of that table which has less fields
    iFieldCount = IIf(Irst1.Fields.Count < rst2.Fields.Count, rst1.Fields.Count - 1, rst2.Fields.Count - 1)
   
    rst2.AddNew
    For i = 0 To iFieldCount
       
        ' Copy field data only if the data type is same
        If rst1.Fields(i).Type = rst2.Fields(i).Type Then
            rst2.Fields(i).Value = rst1.Fields(i).Value
        End If
       
    Next
    rst2.Update
   
    Set rst1 = Nothing
    Set rst2 = Nothing

End Function

Usage:
CopyRecord ("table1","table2","FieldX=3")

You're looking something like this?

Paasky
0
 

Author Comment

by:woaini
ID: 2562050
Thank a lot Paasky !
0
 
LVL 10

Expert Comment

by:paasky
ID: 2562327
woaini, glad we could help you.

wesleystewart, I posted a question for you because I used your idea for solving woaini's problem and want to share the points with you.

Regards,
Paasky
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

863 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

19 Experts available now in Live!

Get 1:1 Help Now