You are making it way too hard for yourself. Here is how to copy a record from a button click. No locks, no queries, and auto update of the form:
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 = "SomeFieldToHandle" Then
rstInsert.Fields(.Name).Value = SomeSpecialValue
ElseIf .Name = "SomeOtherFieldToHandle" Then
rstInsert.Fields(.Name).Value = SomeOtherSpecialValue
ElseIf .Name = "SomeFieldToSkip" Then
' Leave empty or with default value.
ElseIf .Name = "SomeFieldToBlank" Then
rstInsert.Fields(.Name).Value = Null
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
If the button is placed on the parent form, replace Me
with:
Me!NameOfYourSubformControl.Form
Between AddNew
and Update
you can modify and insert code to adjust the value of some fields that should not just be copied.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…