By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,759 Members | 1,712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,759 IT Pros & Developers. It's quick & easy.

Duplicate Form, Subform Records

P: 30
I have a problem with this. Currently I am trying Allen's code and i am not successful.

Current Design

Table1 (Main Form)
TravelID (PK)
ApprovedBY
EntreredBy
BudgetCode
ExpenseCode

Table2 (Subform)
TripID (PK)
TripTypeID
TravellerID
TripStartDate
TripEndDate
TripStatus

Can you help with this?

Expand|Select|Wrap|Line Numbers
  1. 'On Error GoTo Err_Handler
  2.     'Purpose:   Duplicate the main form record and related records in the subform.
  3.     Dim strSql As String    'SQL statement.
  4.     Dim lngID As Long       'Primary key value of the new record.
  5.  
  6.  
  7.     'Save and edits first
  8.     If Me.Dirty Then
  9.         Me.Dirty = False
  10.     End If
  11.  
  12.     'Make sure there is a record to duplicate.
  13.     If Me.NewRecord Then
  14.         MsgBox "Select the record to duplicate."
  15.     Else
  16.         'Duplicate the main record: add to form's clone.
  17.         With Me.RecordsetClone
  18.             .AddNew
  19.                 !EnteredBy = Me.EnteredBy
  20.                 !ApprovedBy = Me.ApprovedBy
  21.                 !BudgetCode = Me.BudgetCode
  22.                 !ExpenseCode = Me.ExpenseCode
  23.             .Update
  24.  
  25.             'Save the primary key value, to use as the foreign key for the related records.
  26.             .Bookmark = .LastModified
  27.             lngID = Me.TravelAuthorizationID
  28.  
  29.  
  30.             'Duplicate the related records: append query.
  31.                     If Me.[TravelAuthorizations Subform].Form.RecordsetClone.RecordCount > 0 Then
  32.                 strSql = "INSERT INTO [Trips] ( TravelID, TripTypeID, TravellerID, TripStartDate, TripEndDate, TripStatus ) " & _
  33.                     "SELECT " & lngID & " As NewID, TripID, TripTypeID, TravellerID, TripStartDate, TripEndate, TripStatus " & _
  34.                     "FROM [Trips] WHERE TravelID = " & Me.TravelID & ";"
  35.                 DBEngine(0)(0).Execute strSql, dbFailOnError
  36.             Else
  37.                 MsgBox "Main record duplicated, but there were no related records."
  38.             End If
  39.  
  40.             'Display the new duplicate.
  41.             Me.Bookmark = .LastModified
  42.         End With
  43.     End If
Jun 13 '09 #1
Share this Question
Share on Google+
1 Reply


ADezii
Expert 5K+
P: 8,627
I too seem to have trouble implementing Allen Browne's code, so I drastically changed the logic involved - sorry Allen! (LOL)!
  1. Instead of using the Recordset exposed by the Form.RecordsetClone Property, I created an External Recordset based on the Orders Table (Ref#1).
  2. I used a smaller Data set for the code testing, but it can easily be expanded.
  3. I allowed the Primary Key in the Main Table (Orders) to Auto-Generate when adding a New Record (Ref #2), then retrieved its value by setting a Bookmark to the LastModified Property of the Recordset (Ref #3).
  4. Retrieved the Value of the Foreign Key for the Records to be added to the Sub-Form (Order Details), which is the same as the Primary Key in the Main Form (Ref #4).
  5. Populated the Sub-Form (Ref #5), Requeried the Main Form (Ref #6), then Navigated to the newly created Main/Sub-Form Record(s) (Ref #7).
  6. Any questions, feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rstDup As DAO.Recordset
  3. Dim lngNewPK As Long
  4. Dim lngOldPK As Long
  5. Dim strSQL As String
  6.  
  7. Set MyDB = CurrentDb
  8. Set rstDup = MyDB.OpenRecordset("Orders", dbOpenDynaset)    'Ref #1
  9.  
  10. With rstDup
  11.   .AddNew
  12.     'The Primary Key [Order ID] will Auto Generate          'Ref #2
  13.     ![Order Name] = Me![Order Name]
  14.   .Update
  15.   .Bookmark = .LastModified         'Ref #3
  16.     lngNewPK = ![Order ID]
  17. End With
  18.  
  19. 'Retrieve the Value of the Old, Original Primary Key in order
  20. 'to populate the New Main Record's Sub-Form
  21. lngOldPK = Me![Order ID]            'Ref #4
  22.  
  23. If Me.[Order Details].Form.RecordsetClone.RecordCount > 0 Then
  24.   strSQL = "INSERT INTO [Order Details] ([Order ID], [Section]) " & _
  25.            "SELECT " & lngNewPK & " As NewID,[Section] " & _
  26.            "FROM [Order Details] WHERE [Order ID] = " & lngOldPK & ";"
  27.                CurrentDb.Execute strSQL, dbFailOnError      'Ref #5
  28. Else
  29.   MsgBox "Main record duplicated, but there were no related records."
  30. End If
  31.  
  32. rstDup.Close
  33. Set rstDup = Nothing
  34.  
  35. Me.Requery                          'Ref #6
  36. DoCmd.GoToRecord , , acLast         'Ref #7
Jun 14 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.