MS Access-是否有简单的方法来复制表单及其子表单的字段信息?

时间:2019-03-28 08:17:07

标签: ms-access duplicates

我想复制一个包含3个子表单的表单-解释起来很简单:想象一个食谱(主表单:一些常规数据;子表单1:配料清单,子表单2:说明;子表单3:价格;有时食谱只改变了面粉的类型,所以我不想再次键入所有内容,而只是具有相同的形式并带有一个新的uniqe ID,并且配料表中也有这种变化)

复制主表单很容易,但是子表单是空的。我在网上找到了一些想法,但似乎很难(我正在编写初学者),例如,请参见Microsoft的建议:https://support.microsoft.com/en-us/help/208824/acc2000-how-to-duplicate-a-main-form-and-its-subform-detail-records

我基本上希望拥有唯一ID带有“ +1”的相同内容。

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以在主表单上有一个按钮来运行此代码,以复制父记录和所有子记录,而无需外部查询,也无需重新查询子表单。

在这里复制两个子表单。只要使用三个子表单,只需使用与复制子记录3 类似的代码对其进行扩展即可:

Private Sub CopyButton_Click()

    Dim rst         As DAO.Recordset
    Dim rstAdd      As DAO.Recordset
    Dim fld         As DAO.Field
    Dim Count       As Integer
    Dim Item        As Integer
    Dim Bookmark    As Variant
    Dim OldId       As Long
    Dim NewId       As Long

    ' Copy parent record.
    Set rstAdd = Me.RecordsetClone
    Set rst = rstAdd.Clone

    ' Move to current record.
    rst.Bookmark = Me.Bookmark
    OldId = rst!Id.Value
    With rstAdd
        .AddNew
        For Each fld In .Fields
            With fld
                If .Attributes And dbAutoIncrField Then
                    ' Skip Autonumber or GUID field.
                Else
                    .Value = rst.Fields(.Name).Value
                End If
            End With
        Next
        .Update
        ' Pick Id of the new record.
        .MoveLast
        NewId = !Id.Value
    End With
    ' Store location of new record.
    Bookmark = rstAdd.Bookmark

    ' Copy child records 1.
    Set rstAdd = Me!subChild1.Form.RecordsetClone
    Set rst = rstAdd.Clone

    If rstAdd.RecordCount > 0 Then
        rstAdd.MoveLast
        rstAdd.MoveFirst
    End If
    Count = rstAdd.RecordCount
    For Item = 1 To Count
        With rstAdd
            .AddNew
            For Each fld In .Fields
                With fld
                    If .Attributes And dbAutoIncrField Then
                        ' Skip Autonumber or GUID field.
                    ElseIf .Name = "FK" Then
                        ' Skip master/child field.
                        .Value = NewId
                    Else
                        .Value = rst.Fields(.Name).Value
                    End If
                End With
            Next
            .Update
        End With
        rst.MoveNext
    Next

    ' Copy child records 2.
    Set rstAdd = Me!subChild2.Form.RecordsetClone
    Set rst = rstAdd.Clone

    If rstAdd.RecordCount > 0 Then
        rstAdd.MoveLast
        rstAdd.MoveFirst
    End If
    Count = rstAdd.RecordCount
    For Item = 1 To Count
        With rstAdd
            .AddNew
            For Each fld In .Fields
                With fld
                    If .Attributes And dbAutoIncrField Then
                        ' Skip Autonumber or GUID field.
                    ElseIf .Name = "FK" Then
                        ' Skip master/child field.
                        .Value = NewId
                    Else
                        .Value = rst.Fields(.Name).Value
                    End If
                End With
            Next
            .Update
        End With
        rst.MoveNext
    Next

    rst.Close
    rstAdd.Close

    ' Move to the new recordcopy.
    Me.Bookmark = Bookmark

    Set fld = Nothing
    Set rstAdd = Nothing
    Set rst = Nothing

End Sub

请注意,subChildx代表子表单 controls 的名称,该名称可能与子表单本身的名称不同。