Good afternoon,
I am new to Microsoft 2016 and am trying to write a VBA code to duplicate a record and only specific fields to a new record with a new primary key number. The primary key is a field called ProjID. Currently, I know the VBA is incorrect because under my general modules, this is a Public Function and the Me is misused. Does anyone know how I can re-write the code to duplicate a record without over-writing any other records? Here is the current code. I think Me should be defined as frm, but I keep getting "variable not defined" errors when I run the code to test it. I also keep getting errors saying I am not using "end if" and "end with" commands correctly.
I am new to Microsoft 2016 and am trying to write a VBA code to duplicate a record and only specific fields to a new record with a new primary key number. The primary key is a field called ProjID. Currently, I know the VBA is incorrect because under my general modules, this is a Public Function and the Me is misused. Does anyone know how I can re-write the code to duplicate a record without over-writing any other records? Here is the current code. I think Me should be defined as frm, but I keep getting "variable not defined" errors when I run the code to test it. I also keep getting errors saying I am not using "end if" and "end with" commands correctly.
PHP:
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 rstSource As DAO.Recordset
Dim rstInsert As DAO.Recordset
Dim fld As DAO.Field
If frm.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.
Else
' Copy field content.
!EmployeeNo = Me.EmployeeNo
!ProjType = Me.ProjType
!ProjDesc = Me.ProjDesc
!Time = Me.Time
!DueDate = Me.DueDate
!Prog = Me.Prog
!Mod = Me.Mod
!Improve = Me.Improve
!Workplan = Me.Workplan
!WorkUnits = Me.WorkUnits
!Comment = Me.Comment
End If
End With
Next
.Update
' Go to the new record and sync form.
.MoveLast
Me.Bookmark = .Bookmark
.Close
End With
End If
End Function