Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / January 2006

Tip: Looking for answers? Try searching our database.

make a copy of record

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ron Carr - 28 Jan 2006 15:34 GMT
In XP I want to make a copy of a record, in DAO, with a new Autonumber key.
I know how to do this on a field by field basis, but wonder if there is any
way to just copy the entire record without doing field by field. This is a
lot of fields, and I also need to do it for a bunch of related tables.
Thanks!
Ron
Allen Browne - 28 Jan 2006 16:04 GMT
If you are trying to copy a record and also its related records, a starting
point might be:
   Duplicate the record in form and subform
at:
   http://allenbrowne.com/ser-57.html

The article does suggest naming each field, as that is the safest approach.
However, you could try looping through the Fields collection instead of
supplying their names. For example, if you have 2 recordsets (say rsSource
and rsTarget):
   For i = 0 to rs.Fields.Count - 1
       rsTarget(i) = rsSource(i)
   Next

Similarly, you will probably use an Append query statement to duplicate the
records in the related table. You can use the wildcard instead of listing
fields. The string will be something like this:
   INSERT INTO MyTargetTable
   SELECT MySourceTable.*
   FROM MySourceTable
   WHERE ...

Signature

Allen Browne - Microsoft MVP.  Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

> In XP I want to make a copy of a record, in DAO, with a new Autonumber
> key.
[quoted text clipped - 4 lines]
> Thanks!
> Ron
Ron Carr - 28 Jan 2006 16:26 GMT
Thank you! The record loop looks good!
Should have remembered that...

> If you are trying to copy a record and also its related records, a starting
> point might be:
[quoted text clipped - 26 lines]
> > Thanks!
> > Ron
John Nurick - 28 Jan 2006 16:35 GMT
If you have a table with "a lot of fields" and you want multiple records
that differ only in the value of one field, there's probably something
wrong with your data structure. Almost certainly you can normalise this
"wide" table into two or more taller, narrower ones.

To do what you ask, you have to work one field at a time. Either build
and execute an append query that lists all the fields *except* the
autonumber, e.g. (air code)

 strSQL = "INSERT INTO MyTable (F2, F3, F4) " _
   & "SELECT F2, F3, F4 FROM MyTable WHERE KeyField=" _
   & Me.KeyField.Value & ";"
 CurrentDB.Execute strSQL, dbFailOnError

or (more air code)
 Dim rsFrom As DAO.Recordset, rsTo As DAO.Recordset
 Dim dbD as DAO.Database
 Dim j As Long
   
 Set dbD = CurrentDB()   
 Set rsFrom = dbD.OpenRecordset("SELECT * FROM MyTable " _
   & " WHERE KeyField=" & Me.KeyField.Value & ";", dbOpenSnapshot
 Set rsTo = dbD.OpenRecordset("SELECT * FROM MyTable " _
   & " WHERE FALSE"; 'recordset with no records
 rsTo.AddNew
   For j = 0 To rsFrom.Fields.Count - 1
     If rsFrom.Fields(j).Name <> "KeyField" Then
       rsTo.Fields(j).Value = rsFrom.Fields(j).Value
     End If
   Next
 rsTo.Update
 rsFrom.Close
 rsTo.Close
 
   
 

>In XP I want to make a copy of a record, in DAO, with a new Autonumber key.
>I know how to do this on a field by field basis, but wonder if there is any
>way to just copy the entire record without doing field by field. This is a
>lot of fields, and I also need to do it for a bunch of related tables.
>Thanks!
>Ron

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.