MS Access - Is there an easy way to duplicate field information of a form and its subforms?

余生颓废 提交于 2021-01-28 11:14:34

问题


I want to duplicate a form with 3 subforms - to explain it simple: imagine a recipe (main form: some general data; sub form 1: list of ingredients, sub form 2: instructions; sub form 3: prices; sometimes the recipes change only the type of flour, so I don't want to type everything again but just have the same form with a new uniqe ID and this one change in the ingredients list)

duplicating the main form is easy, but the sub forms are empty. there are some ideas i found online, but it seems incredibly difficult (i am coding beginner), see for example Microsofts suggestion: https://support.microsoft.com/en-us/help/208824/acc2000-how-to-duplicate-a-main-form-and-its-subform-detail-records

I basically want to have the same content with a "+1" to the unique ID.

Any ideas?

Thanks!


回答1:


You can have a button on the main form to run this code to copy parent record and all child records without external queries, nor a requery of the subforms.

Here you copy two subforms. Just extend it with similar code to Copy child records 3 as you have three subforms:

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

Note please, that subChildx represent the names of the subform controls, which may differ from the names of the subforms themselves.



来源:https://stackoverflow.com/questions/55392894/ms-access-is-there-an-easy-way-to-duplicate-field-information-of-a-form-and-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!