I am trying to duplicate a record on a form and change one field based on a user input.
I have based the code on this from Allen Browne:
http://allenbrowne.com/ser-57.html
I am having trouble with the where clause in the SQL. I am not very familiar with SQL.
I get a Error Syntax error (missing operator) in the query expression...
The database is attached.
Here is the code:
I have based the code on this from Allen Browne:
http://allenbrowne.com/ser-57.html
I am having trouble with the where clause in the SQL. I am not very familiar with SQL.
I get a Error Syntax error (missing operator) in the query expression...
The database is attached.
Here is the code:
Code:
Private Sub cmdDuplicate_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 NewArea As String
'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.
NewArea = InputBox("Enter the Name of the New Area", "Enter New Area")
With Me.RecordsetClone
.AddNew
!Area = "ADA East"
!Job = Me.Job
.Update
'Save the primary key value, to use as the foreign key for the related records.
.Bookmark = .LastModified
'lngID = !OrderID
'Duplicate the related records: append query.
If Me.frmDurationCalulationItems.Form.RecordsetClone.RecordCount > 0 Then
strSql = "INSERT INTO [tblDurationAnalysis] ( Job, item, Area ) " & _
"SELECT " & NewArea & " As Area, Job, item " & _
"FROM [tblDurationAnalysis] WHERE Area = " & NewArea & " And WHERE Job = " & Me.Job & ";"
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