Duplicate Form & Subform for record

W

whitjl143

8.5 hours on this!

I used Allen Browne's code and I am getting the error 3022-changes would
create duplicate values in the index, primary key or relationship.

Here's my code.

Private Sub Command128_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 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
!QuotingCompany = Me.QuotingCompany
!QuoteDate = Me.QuoteDate
!AcutalBidDate = Me.AcutalBidDate
!CustomerName = Me.CustomerName
!ContactName = Me.ContactName
!ContactNumber = Me.ContactNumber
!ContactFax = Me.ContactFax
!EstimatedStart = Me.EstimatedStart
!ProjectName = Me.ProjectName
!ProjectLocation = Me.ProjectLocation
!QuoteStatus = Me.QuoteStatus
!QuotedBy = Me.QuotedBy
!ExpireDays = Me.ExpireDays
!ExpireDate = Me.ExpireDate
!QuoteNotes = Me.QuoteNotes
!PrivateNotes = Me.PrivateNotes
![HourlyTrucking?] = Me.[HourlyTrucking?]
!HourlyTruckingRate = Me.HourlyTruckingRate
![ConveyedTonnage?] = Me.[ConveyedTonnage?]
!ConveyedTonnageRate = Me.ConveyedTonnageRate
![ConveyedHourly?] = Me.[ConveyedHourly?]
!ConveyedHourlyRate = Me.ConveyedHourlyRate
![DumpFeeRequired?] = Me.[DumpFeeRequired?]
!DumpFeeAmount = Me.DumpFeeAmount
!DumpLocation = Me.DumpLocation
!PricesGoodThru = Me.PricesGoodThru
.Update

.Bookmark = .LastModified
lngID = !EstimateID

'Duplicate the related records: append query.
If Me.[EstimateSubform].Form.RecordsetClone.RecordCount > 0 Then
strSql = "INSERT INTO [EstimateDetail] ( DetailID,
EstimateID, Material, LoadAt, OurPrice, Markup, TotalMaterial,
EstimatedTonnage, HaulZone, HaulRate ) " & _
"SELECT " & lngID & " As NewID, EstimateID, Material,
LoadAt, OurPrice, Markup, TotalMaterial, EstimatedTonnage, HaulZone, HaulRate
" & _
"FROM [EstimateDetail] WHERE EstimateID = " &
Me.EstimateID & ";"
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

In my main form - EstimateID is a primary key that is unique
In my subform - DetailID is a primary key that is unique
In my subform - EstimateID is linked how my subform is linked to my main form

I don't understand really which one is doing what and don't know what to do
to fix it!

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top