主要形式的重复记录
我在下面的问题上挣扎了我几个晚上的谷歌搜索这个问题,但我找到了一个代码,但是需要帮助
该问题是我需要以主形式,sub形式和“ subform”和“ subform”和“ subform”复制3个级别的3个级别,
我在下面的链接
但不幸的是,这本帖子自2008年以来就不幸 想到它是从档案文件中的
无论如何都会 我在数据库中尝试此代码及其作品,用于主要形式和子形式的重复记录,但仅对“ usbubform”重复第一个记录
,并给出运行时错误如下: 运行时间错误3078: Microsoft Office访问数据库引擎找不到输入 表或查询。确保它存在并且名称是拼写的 正确。
我在VBA方面
知识
- 有限
- 的
非常 请找到以下代码
Private Sub cmdDuplicatePHIP_Click()
'Purpose: Duplicate the main form record and related records in the subform
Dim db As DAO.Database
Dim rstT2 As DAO.Recordset 'TRD_RDLog
Dim rstT2A As DAO.Recordset 'TRD_RDLog
Dim rstT3 As DAO.Recordset 'TFP_PHIPDtl
Dim rstT3A As DAO.Recordset 'TFP_PHIPDtl
Dim IngT1PK As Long ' current PK TRD_RDTrial
Dim IngT2PK As Long ' current PK TRD_RDLog
Dim IngT3PK As Long ' current PK TFP_PHIPDtl
Dim IngT1NewFK As Long ' new FK TRD_RDTrial
Dim IngT2NewFK As Long ' new FK TRD_RDLog
Dim IngT3NewFK As Long ' new FK TFP_PHIPDtl
Dim strSql As String 'SQL statement.
Dim strSql_S As String 'SQL statement.
Dim strSql_A As String 'SQL statement.
Dim msg As String
'records added
Dim intRC_CD As Integer 'TRD_RDTrial
Dim intRC_CS As Integer 'TRD_RDLog
Dim intRC_CA As Integer 'TFP_PHIPDtl
'Save and edits first
If Me.Dirty Then
Me.Dirty = False
End If
Set db = CurrentDb
'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.
'in TRD_RDTrial 1st table
IngT1PK = Me.TRPK
With Me.RecordsetClone
.AddNew
!TrialDate = Me.TrialDate
!TrialBy = Me.TrialBy
!QC = Me.QC
'etc for other fields.
.Update
intRC_CD = intRC_CD + 1
'Save the primary key value, to use as the foreign key for the related records.
.Bookmark = .LastModified
IngT1NewFK = !TRPK
End With
'Duplicate the related records in TRD_RDLog 2nd table
'Select all records in TRD_RDLog
strSql_S = " SELECT TDPK, TRPK, RDCode, Kitchen, TrialPurpose, PHIPNetWt, ItemTrialNotes, SampleApproval, SampleApprovalDate, SampleApprovalNotes, RecipeDate, Notes"
strSql_S = strSql_S & " FROM [TRD_RDLog];"
Set rstT2A = db.OpenRecordset(strSql_S)
'Select the records to duplicate
strSql_S = " SELECT TDPK, RDCode, Kitchen, TrialPurpose, PHIPNetWt, ItemTrialNotes, SampleApproval, SampleApprovalDate, SampleApprovalNotes, RecipeDate, Notes"
strSql_S = strSql_S & " FROM [TRD_RDLog]"
strSql_S = strSql_S & " WHERE TRPK = " & IngT1PK & ";"
Set rstT2 = db.OpenRecordset(strSql_S)
'check for empty recordset
If Not rstT2.BOF And Not rstT2.EOF Then
rstT2.MoveLast
rstT2.MoveFirst
Do While Not rstT2.EOF
'save PK
IngT2PK = rstT2!TDPK
'add new record
With rstT2A
.AddNew
!TRPK = IngT1NewFK
!RDCode = Nz(rstT2!RDCode, "")
!Kitchen = Nz(rstT2!Kitchen, "")
!TrialPurpose = Nz(rstT2!TrialPurpose, "")
!PHIPNetWt = Nz(rstT2!PHIPNetWt, "")
!ItemTrialNotes = Nz(rstT2!ItemTrialNotes, "")
!SampleApproval = Nz(rstT2!SampleApproval, "")
!SampleApprovalDate = Nz(rstT2!SampleApprovalDate, "")
!SampleApprovalNotes = Nz(rstT2!SampleApprovalNotes, "")
!RecipeDate = Nz(rstT2!RecipeDate, "")
!Notes = Nz(rstT2!Notes, "")
'etc for other fields.
.Update
intRC_CS = intRC_CS + 1
'get new PK
.Bookmark = .LastModified
IngT2NewFK = !TDPK ' new PK
End With
'Duplicate the related records in TFP_PHIPDtl (3rd table)
strSql_A = "SELECT IRF, TDPK, RawCode, Unit, PQty"
strSql_A = strSql_A & " FROM [TFP_PHIPDtl]"
Set rstT3A = db.OpenRecordset(strSql_A)
'Duplicate the related records in TFP_PHIPDtl (3rd table)
strSql_A = "SELECT IRF, RawCode, Unit, PQty"
strSql_A = strSql_A & " FROM [TFP_PHIPDtl]"
strSql_A = strSql_A & " WHERE TDPK = " & IngT2PK & ";"
Set rstT3 = db.OpenRecordset(strSql_A)
'check for empty recordset
If Not rstT3.BOF And Not rstT3.EOF Then
rstT3.MoveLast
rstT3.MoveFirst
Do While Not rstT3.EOF
'save PK
IngT3PK = rstT3!IRF
'add new record
With rstT3A
.AddNew
!TDPK = IngT2NewFK
!RawCode = Nz(rstT3!RawCode, "")
!Unit = Nz(rstT3!Unit, "")
!PQty = Nz(rstT3!PQty, "")
'etc for other fields.
.Update
intRC_CA = intRC_CA + 1
'Save the primary key value, to use as the foreign key for the related records.
.Bookmark = .LastModified
IngT3NewFK = !IRF
End With
'insert record
db.Execute strSql, dbFailOnError
intRC_CA = intRC_CA + 1
rstT3.MoveNext
Loop
rstT3.Close
rstT3A.Close
End If
rstT2.MoveNext
Loop
rstT2.Close
rstT2A.Close
End If
End If
'Display the new duplicate.
Me.FFP_PHIPLog.Visible = True
Me.Label186.Visible = True
Me.Label193.Visible = True
Me.Label200.Visible = True
Me.TrialDate.Locked = False
Me.TrialBy.Locked = False
Me.QC.Locked = False
Me.TrialDate.Value = Null
Me.TrialBy.Value = Null
Me.QC.Value = Null
'tell me when done
msg = intRC_CD & " record added to TRD_RDTrial"
msg = msg & vbCrLf & vbCrLf
msg = msg & intRC_CS & " record(s) added to TRD_RDLOG"
msg = msg & vbCrLf & vbCrLf
msg = msg & intRC_CA & " record(s) added to TFP_PHIPDTL"
msg = msg & vbCrLf & vbCrLf
msg = msg & "Total records added = " & intRC_CD + intRC_CS + intRC_CA
MsgBox msg
End Sub
I struggle with below issue i googling for this issue several nights and i found a code but need to help
The issue is i need to duplicate records in main form , sub form and "subsubform" 3 levels deep
I found code in below link
but unfortunately this post since 2008 I think its from archive files
anyway
i try this code in my database and its works for duplicate records in main form and subform but duplicate first record for "subsubform" only
and give runtime error as following :
Run time error 3078:
The Microsoft Office Access database engine cannot find the input
table or query. Make sure it exists and that the name is spelled
correctly.
My knowledge in vba very limited i am a very bigger in VBA so that i need your help
what I need
- fix runtime error 3078
- complete duplicate records in "subsubform"
Thanks in advance
kindly find below code
Private Sub cmdDuplicatePHIP_Click()
'Purpose: Duplicate the main form record and related records in the subform
Dim db As DAO.Database
Dim rstT2 As DAO.Recordset 'TRD_RDLog
Dim rstT2A As DAO.Recordset 'TRD_RDLog
Dim rstT3 As DAO.Recordset 'TFP_PHIPDtl
Dim rstT3A As DAO.Recordset 'TFP_PHIPDtl
Dim IngT1PK As Long ' current PK TRD_RDTrial
Dim IngT2PK As Long ' current PK TRD_RDLog
Dim IngT3PK As Long ' current PK TFP_PHIPDtl
Dim IngT1NewFK As Long ' new FK TRD_RDTrial
Dim IngT2NewFK As Long ' new FK TRD_RDLog
Dim IngT3NewFK As Long ' new FK TFP_PHIPDtl
Dim strSql As String 'SQL statement.
Dim strSql_S As String 'SQL statement.
Dim strSql_A As String 'SQL statement.
Dim msg As String
'records added
Dim intRC_CD As Integer 'TRD_RDTrial
Dim intRC_CS As Integer 'TRD_RDLog
Dim intRC_CA As Integer 'TFP_PHIPDtl
'Save and edits first
If Me.Dirty Then
Me.Dirty = False
End If
Set db = CurrentDb
'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.
'in TRD_RDTrial 1st table
IngT1PK = Me.TRPK
With Me.RecordsetClone
.AddNew
!TrialDate = Me.TrialDate
!TrialBy = Me.TrialBy
!QC = Me.QC
'etc for other fields.
.Update
intRC_CD = intRC_CD + 1
'Save the primary key value, to use as the foreign key for the related records.
.Bookmark = .LastModified
IngT1NewFK = !TRPK
End With
'Duplicate the related records in TRD_RDLog 2nd table
'Select all records in TRD_RDLog
strSql_S = " SELECT TDPK, TRPK, RDCode, Kitchen, TrialPurpose, PHIPNetWt, ItemTrialNotes, SampleApproval, SampleApprovalDate, SampleApprovalNotes, RecipeDate, Notes"
strSql_S = strSql_S & " FROM [TRD_RDLog];"
Set rstT2A = db.OpenRecordset(strSql_S)
'Select the records to duplicate
strSql_S = " SELECT TDPK, RDCode, Kitchen, TrialPurpose, PHIPNetWt, ItemTrialNotes, SampleApproval, SampleApprovalDate, SampleApprovalNotes, RecipeDate, Notes"
strSql_S = strSql_S & " FROM [TRD_RDLog]"
strSql_S = strSql_S & " WHERE TRPK = " & IngT1PK & ";"
Set rstT2 = db.OpenRecordset(strSql_S)
'check for empty recordset
If Not rstT2.BOF And Not rstT2.EOF Then
rstT2.MoveLast
rstT2.MoveFirst
Do While Not rstT2.EOF
'save PK
IngT2PK = rstT2!TDPK
'add new record
With rstT2A
.AddNew
!TRPK = IngT1NewFK
!RDCode = Nz(rstT2!RDCode, "")
!Kitchen = Nz(rstT2!Kitchen, "")
!TrialPurpose = Nz(rstT2!TrialPurpose, "")
!PHIPNetWt = Nz(rstT2!PHIPNetWt, "")
!ItemTrialNotes = Nz(rstT2!ItemTrialNotes, "")
!SampleApproval = Nz(rstT2!SampleApproval, "")
!SampleApprovalDate = Nz(rstT2!SampleApprovalDate, "")
!SampleApprovalNotes = Nz(rstT2!SampleApprovalNotes, "")
!RecipeDate = Nz(rstT2!RecipeDate, "")
!Notes = Nz(rstT2!Notes, "")
'etc for other fields.
.Update
intRC_CS = intRC_CS + 1
'get new PK
.Bookmark = .LastModified
IngT2NewFK = !TDPK ' new PK
End With
'Duplicate the related records in TFP_PHIPDtl (3rd table)
strSql_A = "SELECT IRF, TDPK, RawCode, Unit, PQty"
strSql_A = strSql_A & " FROM [TFP_PHIPDtl]"
Set rstT3A = db.OpenRecordset(strSql_A)
'Duplicate the related records in TFP_PHIPDtl (3rd table)
strSql_A = "SELECT IRF, RawCode, Unit, PQty"
strSql_A = strSql_A & " FROM [TFP_PHIPDtl]"
strSql_A = strSql_A & " WHERE TDPK = " & IngT2PK & ";"
Set rstT3 = db.OpenRecordset(strSql_A)
'check for empty recordset
If Not rstT3.BOF And Not rstT3.EOF Then
rstT3.MoveLast
rstT3.MoveFirst
Do While Not rstT3.EOF
'save PK
IngT3PK = rstT3!IRF
'add new record
With rstT3A
.AddNew
!TDPK = IngT2NewFK
!RawCode = Nz(rstT3!RawCode, "")
!Unit = Nz(rstT3!Unit, "")
!PQty = Nz(rstT3!PQty, "")
'etc for other fields.
.Update
intRC_CA = intRC_CA + 1
'Save the primary key value, to use as the foreign key for the related records.
.Bookmark = .LastModified
IngT3NewFK = !IRF
End With
'insert record
db.Execute strSql, dbFailOnError
intRC_CA = intRC_CA + 1
rstT3.MoveNext
Loop
rstT3.Close
rstT3A.Close
End If
rstT2.MoveNext
Loop
rstT2.Close
rstT2A.Close
End If
End If
'Display the new duplicate.
Me.FFP_PHIPLog.Visible = True
Me.Label186.Visible = True
Me.Label193.Visible = True
Me.Label200.Visible = True
Me.TrialDate.Locked = False
Me.TrialBy.Locked = False
Me.QC.Locked = False
Me.TrialDate.Value = Null
Me.TrialBy.Value = Null
Me.QC.Value = Null
'tell me when done
msg = intRC_CD & " record added to TRD_RDTrial"
msg = msg & vbCrLf & vbCrLf
msg = msg & intRC_CS & " record(s) added to TRD_RDLOG"
msg = msg & vbCrLf & vbCrLf
msg = msg & intRC_CA & " record(s) added to TFP_PHIPDTL"
msg = msg & vbCrLf & vbCrLf
msg = msg & "Total records added = " & intRC_CD + intRC_CS + intRC_CA
MsgBox msg
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是通过单击主表单上的按钮来实现此目的的完整代码。
当前的记录以及所有这些儿童记录和子记录都将在带有儿童和儿童记录的新的主要记录中复制,并且表格将显示以下:
主要挑战是,尽管所有子记录都存在于子形式,只有一组子孙记录。因此,必须从SubChild表/查询中检索子孙记录,此处名为 tblchildChild 。
同样,主要的钥匙字段和外键字段分别命名为 id 和 fk 。根据需要进行调整。
复制一组儿童孩子记录:
复制一组父子和所有孩子的记录:
This is the full code to achieve this by clicking a button on the main form.
The current record and all child records and child records of these will be copied in a snap to a new main record with child and childchild records, and the form will display this:
The main challenge is, that while all child records are present in the subform, only one set of subchild records will be present. Thus, the subchild records must be retrieved from the subchild table/query, here named tblChildChild.
Also, primary key fields and foreign key fields are named Id and FK respectively. Adjust as needed.
To copy a single set of child-childchild records:
To copy a single set of parent-child and all childchild records of this: