I have a form called fPreproject. I have a code to duplicate a record in this form and paste into a new record with a new primary Key and then end with the cursor in the employee id field of the new record. Unfortunately, it does not work. I keep getting a mismatch error on the field copy code. I do not want to copy all the fields, so I specified each one to copy. I know there is an error in my code, but I cannot find it. I am working in Microsoft Access 2016? Am I making my code too complicated?
Code:
Public Function DupeProjRecord()
'called from shortcut menu pProjPopup - 'Dupe'
'called from [fProjectData]Form KeyDown event - if keycode is
'for Ctrl+D or Ctrl+d
Dim frm As Form
Set frm = Forms![fPREPROJECT]![fProjectData].Form
'if activated from new (unsaved) record do not continue
If frm.NewRecord Then Exit Function
'copy data from the record
Dim rstSource As DAO.Recordset
Dim rstInsert As DAO.Recordset
Dim fld As DAO.Field
If frm.NewRecord = True Then Exit Function
Set rstInsert = frm.RecordsetClone
Set rstSource = rstInsert.Clone
With rstSource
If .RecordCount > 0 Then
' Go to the current record.
.Bookmark = frm.Bookmark
With rstInsert
.AddNew
For Each fld In rstSource.Fields
With fld
If .Attributes And dbAutoIncrField Then
' Skip Autonumber or GUID field.
Else
' Copy field content.
!EmployeeNo = frm.EmployeeNo
!ProjType = frm.ProjType
!ProjDesc = frm.ProjDesc
!Time = frm.Time
!DueDate = frm.DueDate
!Prog = frm.Prog
!Mod = frm.Mod
!Improve = frm.Improve
!Workplan = frm.Workplan
!WorkUnits = frm.WorkUnits
!Comment = frm.Comment
End If
End With
Next
.Update
' Go to the new record and sync form.
.MoveLast
frm.Bookmark = .Bookmark
.Close
End With
End If
.Close
End With
Set rstInsert = Nothing
Set rstSource = Nothing
'place cursor in Employee No Field
frm![ipEmployeeNo].SetFocus
End Function