Syntax Error in a SQL string

P

Paulo

Can someone spot the syntax error in this SQL string? I tried, but could not
see where it is.

Thanks in advance,

Paulo

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Set qdfTemp = CurrentDb.CreateQueryDef("Current Orders Set", _
"SELECT IIf([Orders]![Status]='REC',[Orders]![DC Cims Week
Actual],[Orders]![ETA to DC Cims Week]) " & _
"AS RelevantWeek, Sum(Orders.[Order Qty]) AS [SumOfOrder Qty],
Sum(Orders.[Received Qty]) AS [SumOfReceived Qty], " & _

"Sum(IIf([Orders]![Channel]='WHOLESALE',IIf([Orders]![Status]='REC',[Orders]![Received Qty]*[WSale Price], " & _
"[Orders]![Order Qty]*[WSale
Price]),IIf([Orders]![Status]='REC',[Orders]![Received Qty]*[Std Retail
Price], " & _
"[Orders]![Order Qty]*[Std Retail Price]) AS Revenue,
Sum(IIf([Orders]![Status]='REC', " & _
"[Orders]![Received Qty]*[Landed GBP Cost],[Orders]![Order
Qty]*[Landed GBP Cost])) AS Costs, " & _
"Orders.[Col Ssn], Orders.Currency, Orders.Channel, Orders.Supplier
FROM Orders WHERE" & _
strWhere)
 
D

Douglas J. Steele

First error is that you cannot use the bang (!) operator with fields in
tables. For instance, the first field needs to be

IIf([Orders].[Status]='REC',[Orders].[DC Cims Week Actual],[Orders].[ETA to
DC Cims Week])

I stopped looking after that...
 
J

John Spencer

It's a lot easier to debug this problem is you do something like

Dim strSQL as String

strSQL ="SELECT IIf([Orders]![Status]='REC',[Orders]![DC Cims Week
Actual],[Orders]![ETA to DC Cims Week]) " & _
"AS RelevantWeek, Sum(Orders.[Order Qty]) AS [SumOfOrder Qty],
Sum(Orders.[Received Qty]) AS [SumOfReceived Qty], " & _

"Sum(IIf([Orders]![Channel]='WHOLESALE',IIf([Orders]![Status]='REC',[Orders]![Received
Qty]*[WSale Price], " & _
"[Orders]![Order Qty]*[WSale
Price]),IIf([Orders]![Status]='REC',[Orders]![Received Qty]*[Std Retail
Price], " & _
"[Orders]![Order Qty]*[Std Retail Price]) AS Revenue,
Sum(IIf([Orders]![Status]='REC', " & _
"[Orders]![Received Qty]*[Landed GBP Cost],[Orders]![Order
Qty]*[Landed GBP Cost])) AS Costs, " & _
"Orders.[Col Ssn], Orders.Currency, Orders.Channel, Orders.Supplier
FROM Orders WHERE" & _
strWhere

Debug.Print strSQL

Now you can see exactly what the string you have generated looks like. And
even copy and paste it into a new query to execute and see what might be wrong
with the query. Also, the syntax error could be in strWhere. Perhaps all you
need is to add a space after "WHERE" so you don't end up with
WHERESomeField = ... when you want WHERE SomeField = ....

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
R

ryguy7272

I'm not an expert with this stuff (yet) but if you are converting SQL to VBA,
look at this:
http://allenbrowne.com/ser-71.html

I used it recently and had great success with it!!

Thanks Allen!!
--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.


John Spencer said:
It's a lot easier to debug this problem is you do something like

Dim strSQL as String

strSQL ="SELECT IIf([Orders]![Status]='REC',[Orders]![DC Cims Week
Actual],[Orders]![ETA to DC Cims Week]) " & _
"AS RelevantWeek, Sum(Orders.[Order Qty]) AS [SumOfOrder Qty],
Sum(Orders.[Received Qty]) AS [SumOfReceived Qty], " & _

"Sum(IIf([Orders]![Channel]='WHOLESALE',IIf([Orders]![Status]='REC',[Orders]![Received
Qty]*[WSale Price], " & _
"[Orders]![Order Qty]*[WSale
Price]),IIf([Orders]![Status]='REC',[Orders]![Received Qty]*[Std Retail
Price], " & _
"[Orders]![Order Qty]*[Std Retail Price]) AS Revenue,
Sum(IIf([Orders]![Status]='REC', " & _
"[Orders]![Received Qty]*[Landed GBP Cost],[Orders]![Order
Qty]*[Landed GBP Cost])) AS Costs, " & _
"Orders.[Col Ssn], Orders.Currency, Orders.Channel, Orders.Supplier
FROM Orders WHERE" & _
strWhere

Debug.Print strSQL

Now you can see exactly what the string you have generated looks like. And
even copy and paste it into a new query to execute and see what might be wrong
with the query. Also, the syntax error could be in strWhere. Perhaps all you
need is to add a space after "WHERE" so you don't end up with
WHERESomeField = ... when you want WHERE SomeField = ....

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
Can someone spot the syntax error in this SQL string? I tried, but could not
see where it is.

Thanks in advance
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Totals in Report Footer 1

Top