Duplication Code for Main Form and five Sub-Form records

cwats

Registered User.
Local time
Today, 07:28
Joined
Feb 20, 2019
Messages
40
Hi,

I have a main form with five subforms on it.

I need to duplicate the main forms and subforms recordsets at the same time using one command button on the main form.



Thank you.
 
It's looking normal to me, but I'm not the best judge. Does it look like this to you?
 

Attachments

  • AllenSite.png
    AllenSite.png
    57.3 KB · Views: 166
Thank you for the example. I will try that code and see what happens.
 
if your FK key on each subform table is based on Autonumber Key of mainform, allenbrowne's code will fail.
 
Hey i tried the code example out and cant seem to figure it out. i got error number 3022.



Private Sub cmdfrmTopLevelDupe_Click()
'On Error GoTo Err_Handler
'Purpose: Duplicate the main form record and related records in the subform.
Dim strSql As String 'SQL statement.
Dim lngID As Long 'Primary key value of the new record.

'Save any 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
!PRODUCT_ID = Me.PRODUCT_ID
'etc for other fields.
.Update

'Save the primary key value, to use as the foreign key for the related records.
.Bookmark = .LastModified
lngID = !PRODUCT_ID

'Duplicate the related records: append query.
If Me.sfrmQryprodcontainers.Form.RecordsetClone.RecordCount > 0 Then
strSql = "INSERT INTO [tbl_ProductContainerslist] ( Container_ID, ProdContainersList_ID, PRODUCT_ID, Container_ID, ContainerDescription, ContCostOverride, BaseQty, Yield, YieldQty, ExtCost ) " & _
"SELECT " & lngID & " As NewID, Qryprodcontainers.tbl_Containerslist.Container_ID, Qryprodcontainers.ProdContainersList_ID, Qryprodcontainers.PRODUCT_ID, Qryprodcontainers.tbl_ProductContainerslist.Container_ID, Qryprodcontainers.ContainerDescription, Qryprodcontainers.ContCostOverride, Qryprodcontainers.BaseQty, Qryprodcontainers.Yield, Qryprodcontainers.YieldQty, Qryprodcontainers.ExtCost " & _
"FROM Qryprodcontainers WHERE Qryprodcontainers.PRODUCT_ID = " & Me.PRODUCT_ID & ";"

DBEngine(0)(0).Execute strSql, dbFailOnError
Else
MsgBox "Main record duplicated, but there were no related records."
End If

'Display the new duplicate.
Me.Bookmark = .LastModified
End With
End If

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdDupe_Click"
Resume Exit_Handler
End Sub
 
the error means the PK already exists?
 
You can't have...
Code:
!PRODUCT_ID = Me.PRODUCT_ID

That is the PK for the current record, remove that. Are there any fields from the main record you want to carry over? Those are the only ones you should be copying over in that section.
 
if your FK key on each subform table is based on Autonumber Key of mainform, allenbrowne's code will fail.

Unless I'm misunderstanding what you're saying, that's exactly what Allen's code handles. It adds a new main record, grabs the autonumber value from the new record and uses is for the FK in the subform table(s). I use the same method myself, after stealing it from him. ;)
 

Users who are viewing this thread

Back
Top Bottom