Access VBA SQL Syntax Error in Where Clause

I am getting a syntax error on the following:

strSQL = "UPDATE tblRateCharges INNER JOIN tblRateDetails " & _
"ON tblRateCharges.DetailID = tblRateDetails.DetailID " & _
"SET tblRateDetails.AddlFuelChargeable = " & Nz(DSum("Charge","tblRateCharges","FuelChargeable= -1"),0) " & _
"WHERE (((tblRateDetails.DetailID)=[Forms]![frmRateTool].[DetailID]));"

The following without the WHERE clause works but returns the sum for all records instead of the current form record.  
strSQL = "UPDATE tblRateCharges INNER JOIN tblRateDetails " & _
"ON tblRateCharges.DetailID = tblRateDetails.DetailID " & _
"SET tblRateDetails.AddlFuelChargeable = " & Nz(DSum("Charge","tblRateCharges","FuelChargeable= -1"),0)

Tried using a variable set to the DetailID on the current form in the WHERE clause
  "WHERE (((tblRateDetails.DetailID)= " & intDetailID & "));"   but still get syntax error.
ExpressMan1Asked:
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.

Jeff DarlingDeveloper AnalystCommented:
try this

Dim strSQL

strSQL = "UPDATE tblRateCharges INNER JOIN tblRateDetails " & vbCrLf
strSQL = strSQL & "ON tblRateCharges.DetailID = tblRateDetails.DetailID " & vbCrLf
strSQL = strSQL & "SET tblRateDetails.AddlFuelChargeable = Nz(DSum(" & Chr(34) & "Charge" & Chr(34) & "," & Chr(34) & "tblRateCharges" & Chr(34) & "," & Chr(34) & "FuelChargeable= -1" & Chr(34) & "), 0)" & vbCrLf
strSQL = strSQL & "WHERE (((tblRateDetails.DetailID)=" & Chr(34) & [Forms]![frmRateTool].[DetailID] & Chr(34) & "));"

Debug.Print strSQL

Open in new window

0
Rey Obrero (Capricorn1)Commented:
change this
"WHERE (((tblRateDetails.DetailID)=[Forms]![frmRateTool].[DetailID]));"

with If DetailID is number

"WHERE (tblRateDetails.DetailID)=" & [Forms]![frmRateTool].[DetailID]

if DetailID is text

"WHERE (tblRateDetails.DetailID)='" & [Forms]![frmRateTool].[DetailID] & "'"
0
ExpressMan1Author Commented:
Jeff, Data Type mismatch using,

strSQLFuelChargeable = "UPDATE tblRateCharges INNER JOIN tblRateDetails " & vbCrLf
strSQLFuelChargeable = strSQLFuelChargeable & "ON tblRateCharges.DetailID = tblRateDetails.DetailID " & vbCrLf
strSQLFuelChargeable = strSQLFuelChargeable & "SET tblRateDetails.AddlFuelChargeable = Nz(DSum(" & Chr(34) & "Charge" & Chr(34) & "," & Chr(34) & "tblRateCharges" & Chr(34) & "," & Chr(34) & "FuelChargeable= -1" & Chr(34) & "), 0)" & vbCrLf
strSQLFuelChargeable = strSQLFuelChargeable & "WHERE (((tblRateDetails.DetailID)=" & Chr(34) & [Forms]![frmRateTool].[DetailID] & Chr(34) & "));"

No error using,
strSQLFuelChargeable = strSQLFuelChargeable & "WHERE (((tblRateDetails.DetailID)=" & intDetailID & "));"
But the field AddlFuelChargeable not updating.

Rey,
Still get syntax error on,
strSQLFuelChargeable = "UPDATE tblRateCharges INNER JOIN tblRateDetails " & _
"ON tblRateCharges.DetailID = tblRateDetails.DetailID " & _
"SET tblRateDetails.AddlFuelChargeable = " & Nz(DSum("Charge","tblRateCharges","FuelChargeable= -1"),0) " & _
"WHERE (tblRateDetails.DetailID)=" & [Forms]![frmRateTool].[DetailID]
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Jeff DarlingDeveloper AnalystCommented:
Maybe the SQL conditions are not being met.

Can you try querying the data manually to see if you have a detailID in both tables?

replace 123456 with the actual ID

select * from tblRateCharges 
INNER JOIN tblRateDetails 
ON tblRateCharges.DetailID = tblRateDetails.DetailID 
WHERE (((tblRateDetails.DetailID)="123456"));

Open in new window

0
Rey Obrero (Capricorn1)Commented:
try this

strSQLFuelChargeable = "UPDATE tblRateCharges INNER JOIN tblRateDetails " & _
"ON tblRateCharges.DetailID = tblRateDetails.DetailID " & _
"SET tblRateDetails.AddlFuelChargeable = " & Nz(DSum("Charge", "tblRateCharges", "FuelChargeable= -1"), 0) & _
"WHERE (tblRateDetails.DetailID)=" & [Forms]![frmRateTool].[DetailID]
0
ExpressMan1Author Commented:
Syntax error (missing operator) in query expression '24.75WHERE
(tblRateDetails.DetailID)=2'.

24.75 is the Sum of all records "Charge" in tblCharges, instead of just the sum of the current form DetailID 2

DetailID is linked and showing 2 in both Main form and Subform. Main form is based on tblRateDetails and Subform on tblRateCharges.
0
Jeff DarlingDeveloper AnalystCommented:
You need a space before the WHERE clause.

" WHERE (tblRateDetails.DetailID)=" & [Forms]![frmRateTool].[DetailID]

Open in new window

0
Rey Obrero (Capricorn1)Commented:
try this

strSQLFuelChargeable = "UPDATE tblRateCharges INNER JOIN tblRateDetails " & _
"ON tblRateCharges.DetailID = tblRateDetails.DetailID " & _
"SET tblRateDetails.AddlFuelChargeable = " & Nz(DSum("Charge", "tblRateCharges", "FuelChargeable= -1"), 0) & _
" WHERE (tblRateDetails.DetailID)=" & [Forms]![frmRateTool].[DetailID]
0
ExpressMan1Author Commented:
No error with space before the WHERE but still totaling all records instead of just the current record.

Should I be referencing the DetailID on the subform (sfrmRateAdditional) instead of tblRateDetails?

The syntax seems to be fixed just not getting the correct result.
0
Jeff DarlingDeveloper AnalystCommented:
This needs changing.

 Nz(DSum("Charge", "tblRateCharges", "FuelChargeable= -1"), 0)

Open in new window


suspect that you want to also include DetailID on this Dsum
0

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
ExpressMan1Author Commented:
Thanks very much to both of you.

Going to work on the Dsum as suggested.
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
Microsoft Access

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.