You want to duplicate the main record, and the related records in 2 tables.
You need to know the p.key of the new main record before you can create the
related records for it. Use DAO to duplicate the main record (because it
exposes the key value), and then you can execute append query statements to
duplicate the related records as well.
This example assumes you have a form open, and the user is looking at the
main record to duplicate. (It's an invoice with detail records in this
example):
Private Sub cmdDupe_Click()
Dim sSQL As String
Dim db As DAO.Database
Dim lngID As Long 'The new primary key value.
Set db = DBEngine(0)(0)
If Me.Dirty Then 'Save first
Me.Dirty = False
End If
If Me.NewRecord Then
MsgBox "Select the record to duplicate."
Else
'Duplicate the main record
With Me.RecordsetClone
.AddNew
!InvoiceDate = Date
!ClientID = Me.ClientID
'etc for other fields.
.Update
.Bookmark = .LastModified
lngID = !InvoiceID
'Duplicate the related records.
sSQL = "INSERT INTO tInvoiceDetail ( InvoiceID, Item, Amount ) "
& _
"SELECT " & lngID & " As NewID, Item, Amount " & _
"FROM tInvoiceDetail " & _
"WHERE (tInvoiceDetail.InvoiceID = " & Me.InvoiceID & ");"
db.Execute sSQL, dbFailOnError
'Repeat for your other related table.
'Display the duplicate.
Me.Bookmark = .LastModified
End With
End If
Set db = Nothing
End Sub