How to insert into table from subform using SQL statement in VBA with Where clause that has two conditions.

Greetings Experts:

I'm getting an error when the user clicks on the Edit button on the frmEvent subform. I'm trying to insert the eventdate from the subform into another table.
I haven't used sql much in VBA so I'm sure I'm missing something critical. Here is my code and the example is attached.

Private Sub cmdEdit_Click()
On Error GoTo Err_Handler

    Dim strWhere As String
    
    If IsNull(Me.EventID) Or IsNull(Me.InstanceID) Then
        MsgBox "Unable to determine the entry to edit.", vbExclamation, "Cannot edit."
    Else
        strWhere = "(EventID = " & Nz(Me.EventID, 0) & ") AND (InstanceID = " & Nz(Me.InstanceID, 0) & ")"
        DoCmd.RunSQL "Insert INTO tblEventException (OrigEventDate) Values Forms!frmEvent!frmEventSub!EventDate WHERE = strWhere"
        DoCmd.OpenForm "frmEventException", WhereCondition:=strWhere, WindowMode:=acDialog
    End If
    
Exit_Handler:
    Exit Sub
    
Err_Handler:
    Call LogError(Err.Number, Err.Description, conMod & ".cmdEdit_Click")
    Resume Exit_Handler
End Sub

Open in new window

Example.accdb
LVL 1
Gemini GVBA & Database ProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale FyeCommented:
Since you are trying to UPDATE a record, you need an update query, not an INSERT query.

rather than:
DoCmd.RunSQL "Insert INTO tblEventException (OrigEventDate) Values Forms!frmEvent!frmEventSub!EventDate WHERE = strWhere"

Open in new window

Use:
Dim strSQL as string
strSQL = "UPDATE tblEventException " _
       & "SET OrigEventDate = #" & Forms!frmEvent!frmEventSub!EventDate & "# " _
       & "WHERE " & strWhere
debug.print strsql 
currentdb.Execute strsql, dbfailonerror

Open in new window

This will allow you to debug your code and avoid the warning messages that you get with the RunSQL method.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PatHartmanCommented:
Dale also made syntax corrections which he didn't mention.  You would have seen the syntax error if you had used the good technique of building the string into a variable first rather than trying to execute the string.
1
Gemini GVBA  & Database ProgrammerAuthor Commented:
Thanks Dale!
0
Dale FyeCommented:
glad to help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.