如何将访问表单中编辑的数据记录保存为新记录?

时间:2017-08-28 03:40:12

标签: vba ms-access access-vba

我现在面临一个小问题。我的问题是,目前我编辑了子表单中的当前记录,然后单击"更新"它会覆盖我以前不想要的记录。 相反,我想从子表单中添加我编辑的数据记录,并将其作为新记录插入子表单,其中包含与我正在编辑的PO号相同的PO号。

以下是我的代码:

lib

1 个答案:

答案 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