Am trying to clone an existing record in a MS Access database by a the click of a button on a form. Currently experiencing two issues with my code.
ISSUE 1:
The below code has an issue and advises that it cannot clone as it will cause a duplicate ID in the table where the Reference field is a primary key Autonumber indexed for No Duplicates.
When I investigated I found that the database next Reference number being assigned is a number already assigned in the database. Eg. The last autonumber assigned to the Reference field in the table was 861, however when I run the below code it is generating an autonumber of 190.
Is there any way around this error, am I doing something wrong in the code below?
Corruption issue?
ISSUE 2:
To progress past issue 1, I have removed the primary key indexing and now allowing the Reference field to allow duplicates.
The main form is cloning fine but it also has a subform (which is a continuous form) of whos records I am also trying to clone but I am getting a "SYNTAX ERROR IN INSERT INTO STATEMENT" error message when it gets to the "DoCmd.RunSQL strSql" below. The main form and subform are linked by the 'Reference' field.
I would like the code to clone the current records (may be more than one) on the subform to the clone subform.
FYI - The database is also relationshiped.
Can anyone tell me where I am going wrong.
Private Sub cmdDupe_Click()
'On Error GoTo Err_Handler
'Purpose: Duplicate the main form record and related records in the subform.
Dim strSql As String 'SQL statement.
Dim lngID As Long 'Primary key value of the new record.
Dim myString As String
'Save and edits first
If Me.Dirty Then
Me.Dirty = False
End If
'Make sure there is a record to duplicate.
If Me.NewRecord Then
MsgBox "You must select a record to duplicate."
Else
'Duplicate the main record: add to form's clone.
With Me.RecordsetClone
.AddNew
!FileHeldBy = Me.FileHeldBy
!DateReceived = Me.DateReceived
!FileName = Me.FileName
!CustID = Me.CustID
!TOWID = Me.TOWID
!BusOwnerID = Me.BusOwnerID
'etc for other fields.
.Update
'Save the primary key value, to use as the foreign key for the related records.
.Bookmark = .LastModified
lngID = !Reference
'Ask the user if they wish to copy the records notes as well
myString = MsgBox("Would you like to copy the notes of this record across to the duplicate record?", vbYesNo)
If myString = vbYes Then
'DoCmd.SetWarnings False
'Duplicate the related records: append query.
If Me.[frmDAS subform(frmWorkRequest2005)].Form.RecordsetClone.RecordCount > 0 Then
strSql = "INSERT INTO [tblDAS] (Reference, Date, Description) " & _
"SELECT " & lngID & " As Reference , Date, Description " & _
"FROM [tblDAS] WHERE Reference = " & Me.Reference & ";"
DoCmd.RunSQL strSql
Else
MsgBox "Main record duplicated, but there were no related records."
End If
Else
MsgBox "Main record has been duplicated, please complete remaining fields."
End If
'Display the new duplicate.
Me.Bookmark = .LastModified
End With
End If
Me.FileHeldBy.Value = ""
Exit_Handler:
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdDupe_Click"
Resume Exit_Handler
End Sub
Any help would be much appreciated.
ISSUE 1:
The below code has an issue and advises that it cannot clone as it will cause a duplicate ID in the table where the Reference field is a primary key Autonumber indexed for No Duplicates.
When I investigated I found that the database next Reference number being assigned is a number already assigned in the database. Eg. The last autonumber assigned to the Reference field in the table was 861, however when I run the below code it is generating an autonumber of 190.
Is there any way around this error, am I doing something wrong in the code below?
Corruption issue?
ISSUE 2:
To progress past issue 1, I have removed the primary key indexing and now allowing the Reference field to allow duplicates.
The main form is cloning fine but it also has a subform (which is a continuous form) of whos records I am also trying to clone but I am getting a "SYNTAX ERROR IN INSERT INTO STATEMENT" error message when it gets to the "DoCmd.RunSQL strSql" below. The main form and subform are linked by the 'Reference' field.
I would like the code to clone the current records (may be more than one) on the subform to the clone subform.
FYI - The database is also relationshiped.
Can anyone tell me where I am going wrong.
Private Sub cmdDupe_Click()
'On Error GoTo Err_Handler
'Purpose: Duplicate the main form record and related records in the subform.
Dim strSql As String 'SQL statement.
Dim lngID As Long 'Primary key value of the new record.
Dim myString As String
'Save and edits first
If Me.Dirty Then
Me.Dirty = False
End If
'Make sure there is a record to duplicate.
If Me.NewRecord Then
MsgBox "You must select a record to duplicate."
Else
'Duplicate the main record: add to form's clone.
With Me.RecordsetClone
.AddNew
!FileHeldBy = Me.FileHeldBy
!DateReceived = Me.DateReceived
!FileName = Me.FileName
!CustID = Me.CustID
!TOWID = Me.TOWID
!BusOwnerID = Me.BusOwnerID
'etc for other fields.
.Update
'Save the primary key value, to use as the foreign key for the related records.
.Bookmark = .LastModified
lngID = !Reference
'Ask the user if they wish to copy the records notes as well
myString = MsgBox("Would you like to copy the notes of this record across to the duplicate record?", vbYesNo)
If myString = vbYes Then
'DoCmd.SetWarnings False
'Duplicate the related records: append query.
If Me.[frmDAS subform(frmWorkRequest2005)].Form.RecordsetClone.RecordCount > 0 Then
strSql = "INSERT INTO [tblDAS] (Reference, Date, Description) " & _
"SELECT " & lngID & " As Reference , Date, Description " & _
"FROM [tblDAS] WHERE Reference = " & Me.Reference & ";"
DoCmd.RunSQL strSql
Else
MsgBox "Main record duplicated, but there were no related records."
End If
Else
MsgBox "Main record has been duplicated, please complete remaining fields."
End If
'Display the new duplicate.
Me.Bookmark = .LastModified
End With
End If
Me.FileHeldBy.Value = ""
Exit_Handler:
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdDupe_Click"
Resume Exit_Handler
End Sub
Any help would be much appreciated.