Solved

Duplicate a Record and retrive ID

Posted on 2011-09-20
4
369 Views
Last Modified: 2012-05-12
Hello Experts,

I have a table with 63 fields, with fldID as Primary Key Auto#, and I'm hoping If any one can tell me how I can:

1) Duplicate a record (repeat order)
2) Retrieve the new fldID

Any help will be much appreciated
0
Comment
Question by:APD_Toronto
[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
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
ID: 36570857
You can try this:

Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
CurrentProject.Connection.Execute "INSERT INTO YourTable (SELECT * FROM YourTAble WHERE YourIDField=YourID)"
rst.Open "SELECT @@IDENTITY AS NewID", CurrentProject.Connection

Msgbox rst("NewID")

Obviously you'd have to change the YourIDField to match your own, and the value of YourID to that of the record you wish to duplicate.
0
 

Author Comment

by:APD_Toronto
ID: 36570989
Can I use the above without the ADODB as I need to deploy the library

Bsides, its Access, we can use

Dim rst As Recordset

and

CurrentDB

Can we combine these 2 instead
0
 

Expert Comment

by:Gene-Math
ID: 36571707
   
Dim DB As DAO.Database
    Dim RS As DAO.Recordset
    Dim lNewID As Long
    
    Set DB = CurrentDb
    
    DB.Execute "INSERT INTO tblSource SELECT Fld1, Fld2, Fld3, Fld4, Fld5 FROM tblSource WHERE ID=5;"
    Set RS = DB.OpenRecordset("SELECT @@IDENTITY AS NewID FROM tblSource;")
    
    If RS.RecordCount <> 0 Then
        RS.MoveFirst
        lNewID = RS!NewID
    End If
    
    Debug.Print lNewID

Open in new window

0
 

Assisted Solution

by:Gene-Math
Gene-Math earned 250 total points
ID: 36571721
I forgot to add.  You will get an error if your ID(Autonumber) field is in your select field list.

Also don't forget to remove your DB and RS

Set RS = Nothing
Set DB = Nothing

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

728 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