theinviter
Registered User.
- Local time
- Today, 08:49
- Joined
- Aug 14, 2014
- Messages
- 268
hi guys :
i need help, have access data base containing form and subform, i want to creat a button that when i click on it it will duplicate the record in new record within the same form.
the form ( table2) contain following : ID , Field1 , Field2
and subform contain following : Code , S1 , S2
link master field is ID
link child fields is S2
can any one guide me please .
i tried the bellow code but does not work.
Private Sub Command9_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 lngID2 As Long 'Primary key value of the new record.
'Save any edits first
If Me.Dirty Then
Me.Dirty = False
End If
'Make sure there is a record to duplicate.
If Me.NewRecord Then
MsgBox "Select the record to duplicate."
Else
'Duplicate the main record: add to form's clone.
With Me.RecordsetClone
.AddNew
!Field1 = Me.Field1
!Field2 = Me.Field2
'etc for other fields.
.Update
'Save the primary key value, to use as the foreign key for the related records.
.Bookmark = .LastModified
lngID = !ID
' lngID2 = [Field2]
'Duplicate the related records: append query.
If Me.[table2 subform1].Form.RecordsetClone.RecordCount > 0 Then
strSql = "INSERT INTO [table2] ( ID,Field1, Field2 ) " & _
"SELECT " & lngID & " As NewID , Field1, Field2 ,code , s1" & _
"FROM [table2] WHERE ID = " & Me.ID & ";"
Debug.Print strSql
DBEngine(0)(0).Execute strSql, dbFailOnError
Else
MsgBox "Main record duplicated, but there were no related records."
End If
'Display the new duplicate.
Me.Bookmark = .LastModified
End With
End If
Exit_Handler:
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdDupe_Click"
Resume Exit_Handler
End Sub
i need help, have access data base containing form and subform, i want to creat a button that when i click on it it will duplicate the record in new record within the same form.
the form ( table2) contain following : ID , Field1 , Field2
and subform contain following : Code , S1 , S2
link master field is ID
link child fields is S2
can any one guide me please .
i tried the bellow code but does not work.
Private Sub Command9_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 lngID2 As Long 'Primary key value of the new record.
'Save any edits first
If Me.Dirty Then
Me.Dirty = False
End If
'Make sure there is a record to duplicate.
If Me.NewRecord Then
MsgBox "Select the record to duplicate."
Else
'Duplicate the main record: add to form's clone.
With Me.RecordsetClone
.AddNew
!Field1 = Me.Field1
!Field2 = Me.Field2
'etc for other fields.
.Update
'Save the primary key value, to use as the foreign key for the related records.
.Bookmark = .LastModified
lngID = !ID
' lngID2 = [Field2]
'Duplicate the related records: append query.
If Me.[table2 subform1].Form.RecordsetClone.RecordCount > 0 Then
strSql = "INSERT INTO [table2] ( ID,Field1, Field2 ) " & _
"SELECT " & lngID & " As NewID , Field1, Field2 ,code , s1" & _
"FROM [table2] WHERE ID = " & Me.ID & ";"
Debug.Print strSql
DBEngine(0)(0).Execute strSql, dbFailOnError
Else
MsgBox "Main record duplicated, but there were no related records."
End If
'Display the new duplicate.
Me.Bookmark = .LastModified
End With
End If
Exit_Handler:
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdDupe_Click"
Resume Exit_Handler
End Sub