Hi All,
I'm having some pesky problems with an UPDATE SQL statement that I have written inside a loop. The really pesky problem is that, when I copy and paste the specific SQL statement into a blank query in Access, it returns the correct values when viewed in datasheet view. When I try to run it in VBA I get Run-time error '3075' - Syntax error in date in query expression '#'. The offending SQL Statement is shown below:
UPDATE All_MIPRS SET FY = 'FY12',DocNum = 'DWAM20812', Program = 'SMART', typeSMART = '', SMARTID = '', facRec = '', DateGen = ##, Committed = 16467, Obligated = 16467, Executed = 16170.84, Notes = '', [High Priority] = False WHERE DocNum = 'DWAM20812'
Now I'm assuming it is complaining about the "DateGen = ##" portion but as I said, when I run this in an Access query it doesn't present a problem. Do I need to add some sort of Null value check for the DateGen field in order to avoid this problem? I don't want to enter an inaccurate date. If we don't have a value for DateGen, the correct value is Null. Since this is coded inside a loop where DateGen mostly does have date values but occasionally does not, I need my code to handle both situations.
The code I'm using to run this loop is shown below. Thanks for any and all insights!
Code:
Do Until rst.EOF
varX = DLookup("[ID]", "All_MIPRS", "[DocNum] = '" & rst![DocNum] & "'")
If IsNull(varX) = True Then
strSQL = "INSERT INTO All_MIPRS (FY, DocNum, Program, typeSMART, " & _
"SMARTID, facRec, DateGen, Committed, Obligated, Executed, " & _
"Notes, [High Priority]) VALUES ('" & rst![FY] & "', '" & _
rst![DocNum] & " ', '" & rst![Program] & "','" & _
rst![typeSMART] & "', '" & rst![SMARTID] & "'," & rst![facRec] & ", #" & _
rst![DateGen] & "#, " & rst![Committed] & ", " & rst![Obligated] & ", " & _
rst![Executed] & ",'" & _
rst![Notes] & "', " & rst![High Priority] & ")"
ElseIf IsNull(varX) = False Then
strSQL = "UPDATE All_MIPRS SET FY = '" & rst![FY] & "'," & _
"DocNum = '" & rst![DocNum] & "', " & _
"Program = '" & rst![Program] & "', typeSMART = '" & rst![typeSMART] & "', " & _
"SMARTID = '" & rst![SMARTID] & "', facRec = '" & rst![facRec] & "', " & _
"DateGen = #" & rst![DateGen] & "#, Committed = " & rst![Committed] & ", " & _
"Obligated = " & rst![Obligated] & ", Executed = " & rst![Executed] & ", " & _
"Notes = '" & rst![Notes] & "', " & _
"[High Priority] = " & rst![High Priority] & " " & _
"WHERE DocNum = '" & rst![DocNum] & "'"
End If
Debug.Print strSQL
CurrentDb.Execute (strSQL)
rst.MoveNext
Loop