Duplication Code for Main Form and five Sub-Form records (1 Viewer)

cwats

Registered User.
Local time
Today, 00:44
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.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:44
Joined
Feb 19, 2002
Messages
43,484
One command button is fine but you'll need to run 6 append queries. One for each table. Assuming that the five subforms are children of the main form, the simplest solution to get the the PK of the new mainform record is to use the .AddNew method of DAO rather than running an append query for the "parent" table. By using the .AddNew, you will be able to directly access the newly generated autonumber. When you run an append query and the BE is Jet/ACE, you have NO WAY to absolutely determine the ID of the record you just inserted when you are operating in a multi-user environment. With SQL Server, there is a way to get the last ID inserted via the current thread but not when the BE is Jet/ACE. So this is a case where I defer to the "Access" method and use .AddNew. Once you have the PK that you need to use as the FK for the 5 child tables, you can pass that to the 5 append queries.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:44
Joined
Feb 19, 2002
Messages
43,484
wow! What happened to Allen's site. It's really hard to read.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:44
Joined
Aug 30, 2003
Messages
36,133
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: 74

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:44
Joined
Feb 19, 2002
Messages
43,484
Now it does. The first time I clicked it, there was a large banner across the top, the background of the window was black and some of the text was yellow and some was blue. The blue was unreadable. There were also three or four lines between each line of code. I should have taken a screen shot.
 

cwats

Registered User.
Local time
Today, 00:44
Joined
Feb 20, 2019
Messages
40
Thank you for the example. I will try that code and see what happens.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:44
Joined
May 7, 2009
Messages
19,246
if your FK key on each subform table is based on Autonumber Key of mainform, allenbrowne's code will fail.
 

cwats

Registered User.
Local time
Today, 00:44
Joined
Feb 20, 2019
Messages
40
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
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:44
Joined
May 7, 2009
Messages
19,246
the error means the PK already exists?
 

GinaWhipp

AWF VIP
Local time
Today, 03:44
Joined
Jun 21, 2011
Messages
5,899
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.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:44
Joined
Aug 30, 2003
Messages
36,133
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

Top Bottom