473,396 Members | 2,151 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,396 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 7225
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.