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? -
'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
-
!EnteredBy = Me.EnteredBy
-
!ApprovedBy = Me.ApprovedBy
-
!BudgetCode = Me.BudgetCode
-
!ExpenseCode = Me.ExpenseCode
-
.Update
-
-
'Save the primary key value, to use as the foreign key for the related records.
-
.Bookmark = .LastModified
-
lngID = Me.TravelAuthorizationID
-
-
-
'Duplicate the related records: append query.
-
If Me.[TravelAuthorizations Subform].Form.RecordsetClone.RecordCount > 0 Then
-
strSql = "INSERT INTO [Trips] ( TravelID, TripTypeID, TravellerID, TripStartDate, TripEndDate, TripStatus ) " & _
-
"SELECT " & lngID & " As NewID, TripID, TripTypeID, TravellerID, TripStartDate, TripEndate, TripStatus " & _
-
"FROM [Trips] WHERE TravelID = " & Me.TravelID & ";"
-
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
1 7225
I too seem to have trouble implementing Allen Browne's code, so I drastically changed the logic involved - sorry Allen! (LOL)! - Instead of using the Recordset exposed by the Form.RecordsetClone Property, I created an External Recordset based on the Orders Table (Ref#1).
- I used a smaller Data set for the code testing, but it can easily be expanded.
- 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).
- 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).
- 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).
- Any questions, feel free to ask.
- Dim MyDB As DAO.Database
-
Dim rstDup As DAO.Recordset
-
Dim lngNewPK As Long
-
Dim lngOldPK As Long
-
Dim strSQL As String
-
-
Set MyDB = CurrentDb
-
Set rstDup = MyDB.OpenRecordset("Orders", dbOpenDynaset) 'Ref #1
-
-
With rstDup
-
.AddNew
-
'The Primary Key [Order ID] will Auto Generate 'Ref #2
-
![Order Name] = Me![Order Name]
-
.Update
-
.Bookmark = .LastModified 'Ref #3
-
lngNewPK = ![Order ID]
-
End With
-
-
'Retrieve the Value of the Old, Original Primary Key in order
-
'to populate the New Main Record's Sub-Form
-
lngOldPK = Me![Order ID] 'Ref #4
-
-
If Me.[Order Details].Form.RecordsetClone.RecordCount > 0 Then
-
strSQL = "INSERT INTO [Order Details] ([Order ID], [Section]) " & _
-
"SELECT " & lngNewPK & " As NewID,[Section] " & _
-
"FROM [Order Details] WHERE [Order ID] = " & lngOldPK & ";"
-
CurrentDb.Execute strSQL, dbFailOnError 'Ref #5
-
Else
-
MsgBox "Main record duplicated, but there were no related records."
-
End If
-
-
rstDup.Close
-
Set rstDup = Nothing
-
-
Me.Requery 'Ref #6
-
DoCmd.GoToRecord , , acLast 'Ref #7
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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 #. ...
|
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"...
|
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...
|
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...
|
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
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| | |