Code:
Private Sub cmdDuplicateContract_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
!VendorID = Me.cboResellerID
!Reference = Me.txtReference
!OrderSummary = Me.txtOrderSummary
!RenewalIntent = Me.chkRenewalIntent
!ContractStart = Me.txtContractStart
!TerminationNotice = Me.txtTerminationNotice
!ContractEnd = Me.txtContractEnd
!InvoicePeriodID = Me.cboInvoicePeriodID
.Update
'Save the primary key value, to use as the foreign key for the related records.
.Bookmark = .LastModified
lngID = !OrderID
'Duplicate the related records: append query.
If Me.[subProduct].Form.RecordsetClone.RecordCount > 0 Then
strSql = "INSERT INTO [OrderProduct] ( OrderID, VendorID, ProductID, Serial, Quantity, UnitCost, DepartmentID, SubAccountID, CapexBudgetID ) " & _
"SELECT " & lngID & " As NewID, VendorID, ProductID, Serial, Quantity, UnitCost, DepartmentID, SubAccountID, CapexBudgetID " & _
"FROM [OrderProduct] WHERE OrderID = " & Me.OrderID & ";"
DBEngine(0)(0).Execute strSql, dbFailOnError
Else
MsgBox "Contract duplicated, but no products found."
End If
' If Me.[subServiceCatalogAllocation].Form.RecordsetClone.RecordCount > 0 Then
' strSql = "INSERT INTO [OrderServiceCatalog] ( OrderServiceID, OrderProductID, ServiceID, Percentage ) " & _
' "SELECT " & lngID & " As NewID, NEWORDERPRODUCTID, ServiceID, Percentage " & _
' "FROM [OrderServiceCatalog] WHERE OrderProductID = " & Me.[EACHORDERPRODUCTID] from av & ";"
' DBEngine(0)(0).Execute strSql, dbFailOnError
' Else
' MsgBox "Contract duplicated, but no service catalog allocation found."
' End If
' If Me.[subServiceLineAllocation].Form.RecordsetClone.RecordCount > 0 Then
' strSql = "INSERT INTO [OrderServiceLine] ( OrderServiceLineID, OrderProductID, ServiceLineID, Percentage ) " & _
' "SELECT " & lngID & " As NewID, NEWORDERPRODUCTID, ServiceLineID, Percentage " & _
' "FROM [OrderServiceLineID] WHERE OrderProductID = " & Me.[EACHORDERPRODUCTID] & ";"
' DBEngine(0)(0).Execute strSql, dbFailOnError
' Else
' MsgBox "Contract duplicated, but no service line allocation found."
' End If
' If Me.[subOtherAllocation].Form.RecordsetClone.RecordCount > 0 Then
' strSql = "INSERT INTO [OrderMinorAllocation] ( OrderMinorAllocationID,OrderProductID, MinorAllocationID, Percentage ) " & _
' "SELECT " & lngID & " As NewID, NEWORDERPRODUCTID, MinorAllocationID, Percentage " & _
' "FROM [OrderMinorAllocationID] WHERE OrderProductID = " & Me.[EACHORDERPRODUCTID] & ";"
' DBEngine(0)(0).Execute strSql, dbFailOnError
' Else
' MsgBox "Contract duplicated, but no other allocation found."
' 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
Thanks for any ideas. I'm certain I haven't explained this well...