Solved

Duplicate a Record and retrive ID

Posted on 2011-09-20
4
363 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
  • 2
4 Comments
 
LVL 84

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

749 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