473,372 Members | 1,047 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Duplicate Form, Subform Records

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
1 7218
ADezii
8,834 Expert 8TB
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

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

Similar topics

9
by: Catherine Jo Morgan | last post by:
Can I set it up so that a certain combination of fields can't contain the same entries, on another record? e.g. a combination of FirstName/LastName/address? Or FirstName/LastName/phone? Or...
25
by: Lyn | last post by:
Hi, I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID. The parent form (frmMainForm) displays the...
1
by: Charles Ledbetter | last post by:
I'm missing the mistake. I have a MainForm called frmResources. I have a Second Form and related table called frmFootnotes. I am trying to use it as a subform to the main form appearing in a...
2
by: mavmavv | last post by:
I have a Form where I have created a duplicate record button, no problem... The subform is where my problem lies. The subform displays data matching the mainform's ID, these two values are...
0
by: claus | last post by:
Hi, I am not a programmer but tries anyway to program a feature in a form where I am able to copy entries for the form and a subform. I have tried follow the guide here...
6
by: Otis492 | last post by:
Hello, I have been struggling with this for a while. I am working on a rather simple database for claims in Access 2003. I have a table called claims that has a primary key field called Claim #. ...
1
by: VinArt | last post by:
MS Acc 2003, XP Thank you in advance for any help. I have tables called "Makeup" and "Lines". Each makeup can have multiple lines. Goal is to create a new "makeup" with identical "lines"...
3
by: 6afraidbecause789 | last post by:
Kudos to anyone who can explain this one--how to duplicate a group of continuous records in a subform for use in a new subform PK ID. The 2 entry fields (combo boxes) in the subform are RoleID and...
1
by: troy_lee | last post by:
I have a table (Table A). It has one field, a PK. It is in a 1:M with another table (Table B). I am having trouble with a form/subform setup to view the 1:M records. On the parent form, there is...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.