This is where I got to, but it won't work. I only really workm in tables
queries forms and reports. I think I may have got out of my depth?
Private Sub Command56_Click()
'On Error GoTo Err_Handler
'Purpose: Duplicate the Version record and related records in
Assemblies and Components.
Dim strSql As String 'SQL statement.
Dim VerID As Long 'Primary key value of the new Version record.
Dim AssID As Long 'Primary key value of the new Assembly record.
Dim OldAssID As Long
Dim CompURN As Long
Dim Qty As Long
Dim assQry As Recordset
'Save and edits first
If Me.Dirty Then
Me.Dirty = False
End If
'Make sure there is a record to duplicate.
' If Me.NewRecord Then
' MsgBox "Select the record to duplicate."
'Else
'Duplicate the main record: add to form's clone.
With Me.RecordsetClone
.AddNew
!ProjectURN = Me.ProjectURN
!Description = Me.Description
!Finish = Me.Finish
!Jointing = Me.Jointing
!Category = Me.Category
' !Furniture = Me.Furniture
'etc for other fields.
.Update
'Save the primary key value, to use as the foreign key for the
related records.
.Bookmark = .LastModified
VerID = !VersionURN
' End If
Set assQry = OpenRecordset("SELECT AssemblyURN, Description,
ProjectURN, Finish, Jointing, Category, VersionURN) from [Assemblies]
where
VersionURN = me.VersionURN")
Do While Not EOF("assQry")
'Get old Assembly Details
Set OldAssID = AssemblyURN
Set CompURN = ComponentURN
Set Qty = Quantity
'Make new Assembly record and add details
With Assemblies
.AddNew
.Fields("ComponentURN") = CompURN
.Fields("AssemblyURN") = OldAssID
.Fields("Quantity") = Qty
.Update
.Bookmark = .LastModified
Set NewAssID = AssemblyURN 'Set new AssemblyURN as variable
End With
'Select component records that have old AssemblyURN and copy
'into Component table with new AssemblyURN
strSql = "INSERT INTO [Components] ( AssemblyURN,
CatalogueURN, Quantity) " & _
"SELECT " & VerID & " As NewID, CatalogueURN, Quantity" & _
"FROM [Components] WHERE AssemblyURN = " & OldAssID & ";"
DBEngine(0)(0).Execute strSql, dbFailOnError
Loop
'Display the new duplicate.
Me.Bookmark = .LastModified
End With
End If
Exit_Handler:
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description, ,
"Command56_Click"
Resume Exit_Handler
End Sub
Allen Browne said:
Yes.
The middle stage involves OpenRecordset() on a query statement that just
brings in the Assemblies for the record in the form, plus OpenRecordset()
in
the same table to append the new records to. Loop through the source
records, with AddNew and Update on the target Recordset.
For each one, you will then execute a SQL statement to duplicate the
grandchild records (Components.)
Thanks Allen
I've used your code to successfully duplicate the Version and related
Assemblies, but I don't quite understand your suggestion about how to
deal
with the Components records. I'm guessing that it's some iterative
process,
going through each assembly and running a similar append query, but I'm
not
sure and when I think about it my head starts to spin!
:
My database contains several related entities: Versions are made up of
Assemblies which are made up of Components.
I want to select a Version on a form, then make a similar new Version
(only
the URN will change) I then want to copy each Assembly related to the
old
Version, relating the new Assembly records to the new Version.
Similarly,
I
want to copy each Component record relating to each old Assembly
record
and
relate it to the new Assembly record.
I know I can't do it with temporary tables, append and update queries,
because I've tried. Does anyone have some sample code I could amend
and
attach to a command button on the form?