I have some experience with sql, but limited experience with access. I am trying to create duplicate parent and child records
using a form. I have added the following VB code and I keep getting a syntax erro in insert into statement. I'm hoping
(praying) it's something simple that I am just not seeing.
The Parent record is created correctly.
Here is the code:
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 String 'Primary key value of the new record.
'Save any edits first
If Me.Dirty Then
Me.Dirty = False
'Make sure there is a record to duplicate.
If Me.NewRecord Then
MsgBox "Select the record to duplicate."
'Pass in new Key
myValue = InputBox("Enter Forecast Date", "Inputbox")
'Duplicate the main record: add to form's clone.
!PPod = Me.PPod
!Engineer = Me.Engineer
!Current_Product = Me.Current_Product
!Bag_size_kg = Me.Bag_size_kg
!Ppod_downtime = Me.Ppod_downtime
!Date = Me.Date
!Key = Me.PPod & "-" & myValue
'Save the primary key value, to use as the foreign key for the related records.
.Bookmark = .LastModified
lngID = !Key
'Duplicate the related records: append query.
If Me.[RE_Forecast_Inj subform].Form.RecordsetClone.RecordCount > 0 Then
strSql = "INSERT INTO [RE_Forecast_Inj] ( Key, Pattern, Ppod, Date, Qinj_m3d, Cppm, Bags_Week ) " & _
"SELECT " & lngID & " As NewID, Pattern, Ppod, Date, Qinj_m3d, Cppm, Bags_Week " & _
"FROM [RE_Forecast_Inj] WHERE Key = " & Me.Key & ";"
DBEngine(0)(0).Execute strSql, dbFailOnError
MsgBox "Main record duplicated, but there were no related records."
'Display the new duplicate.
Me.Bookmark = .LastModified
MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdDupe_Click()"