MS Access - 是否有一种简单的方法来复制表单及其子表单的字段信息? [英] MS Access - Is there an easy way to duplicate field information of a form and its subforms?
问题描述
我想复制一个包含 3 个子表单的表单 - 简单解释一下:想象一个食谱(主表单:一些一般数据;子表单 1:成分列表,子表单 2:说明;子表单 3:价格;有时食谱只更改面粉的类型,所以我不想再次输入所有内容,但只需使用新的唯一 ID 和配料列表中的这一更改即可使用相同的表单)
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)
复制主表单很容易,但子表单是空的.我在网上找到了一些想法,但似乎非常困难(我是编码初学者),例如 Microsoft 的建议:https://support.microsoft.com/en-us/help/208824/acc2000-如何复制主表单及其子表单详细记录
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
我基本上希望具有相同的内容,并且唯一 ID 带有+1".
I basically want to have the same content with a "+1" to the unique ID.
有什么想法吗?
谢谢!
推荐答案
您可以在主窗体上有一个按钮来运行此代码以复制父记录和所有子记录,无需外部查询,也无需重新查询子窗体.
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.
在这里复制两个子表单.只需使用类似的代码将其扩展为Copy child records 3,因为您有三个子表单:
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
请注意,subChildx
代表子表单控件的名称,这可能与子表单本身的名称不同.
Note please, that subChildx
represent the names of the subform controls, which may differ from the names of the subforms themselves.
这篇关于MS Access - 是否有一种简单的方法来复制表单及其子表单的字段信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!