我现在面临一个小问题。我的问题是,目前我编辑了子表单中的当前记录,然后单击"更新"它会覆盖我以前不想要的记录。 相反,我想从子表单中添加我编辑的数据记录,并将其作为新记录插入子表单,其中包含与我正在编辑的PO号相同的PO号。
以下是我的代码:
lib
答案 0 :(得分:0)
您可以使用 RecordsetClone 创建当前记录的欺骗。更快更清洁,无需标签:
Private Sub btnCopy_Click()
Dim rstSource As DAO.Recordset
Dim rstInsert As DAO.Recordset
Dim fld As DAO.Field
If Me.NewRecord = True Then Exit Sub
Set rstInsert = Me.RecordsetClone
Set rstSource = rstInsert.Clone
With rstSource
If .RecordCount > 0 Then
' Go to the current record.
.Bookmark = Me.Bookmark
With rstInsert
.AddNew
For Each fld In rstSource.Fields
With fld
If .Attributes And dbAutoIncrField Then
' Skip Autonumber or GUID field.
ElseIf .Name = "SomeFieldToExclude" Then
' Leave field blank.
ElseIf .Name = "SomeOtherFieldToExclude" Then
' Leave field blank.
Else
' Copy field content.
rstInsert.Fields(.Name).Value = .Value
End If
End With
Next
.Update
' Go to the new record and sync form.
.MoveLast
Me.Bookmark = .Bookmark
.Close
End With
End If
.Close
End With
Set rstInsert = Nothing
Set rstSource = Nothing
End Sub
当然,如果您将按钮放在主窗体上,请在代码Me
中替换为子窗体的引用:Me!NameOfYourSubformControl.Form