Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Duplicate an existing records data into a new record 1

Status
Not open for further replies.

gd

Programmer
Jan 27, 2000
9
CA
I need to know if there is any way to take the data from an existing record and create a new record with the exact same data.&nbsp;&nbsp;The only info that would change is the primary key which is an autonumber field.<br><br>
 
yes, do you want to put it into a new table or just a new record in the same table? <p>Brian Famous<br><a href=mailto:bfamous@ncdoi.net>bfamous@ncdoi.net</a><br><a href= > </a><br>
 
This works:<br><br>Dim db As DAO.Database<br>Dim rst As DAO.Recordset<br>Dim fld As DAO.Field<br>Dim myarray() As String<br>Dim i As Integer<br><br>Set db = CurrentDb<br>Set rst = db.OpenRecordset(&quot;yourrecordsourcetableorquery&quot;)<br><br>For Each fld In rst.Fields<br>&nbsp;&nbsp;&nbsp;&nbsp;ReDim Preserve myarray(i)<br>&nbsp;&nbsp;&nbsp;&nbsp;myarray(i) = fld.Value<br>&nbsp;&nbsp;&nbsp;&nbsp;i = i + 1<br>Next<br>i = 0<br>rst.AddNew<br>For Each fld In rst.Fields<br>&nbsp;&nbsp;&nbsp;&nbsp;rst.Fields(i) = myarray(i)<br>&nbsp;&nbsp;&nbsp;&nbsp;i = i + 1<br>Next<br>'right here put code to set your primary field to<br>'something via a formula or input box or something.<br><br>rst.Update<br>Me.Requery<br>
 
Since you are using an autonumber field for the primary key then as you loop through the fields test to see if it is the primary key field and then just don't touch it with the code. Disregard my 2 commented lines in the previous post since you are autonumbering.
 
Ok I see how this could work and I'll try it but before I do, one question.&nbsp;&nbsp;The recordset that I open it should be a query that selects only the record I want &quot;copied&quot; yes?<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top