Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Line 1: Incorrect syntax near '='. 1

Status
Not open for further replies.

Vika

Programmer
Jul 31, 2000
29
US
When executing the following SQL statment in VB6 I get Runtime Error Line1: Incorrect syntax near '='
I am using SQL 2000 as back end.

Can somebody tell me what am I doing wrong?

conn.Open "Provider=sqloledb; Data Source=acmedc1; Initial Catalog=OrderChecking; Integrated Security=SSPI;"

sql = "SELECT tblHeader.PO, tblHeader.BillName, tblHeader.BillAddress1, tblHeader.BillAddress2, " & _
"tblHeader.BillAddress3, tblHeader.BillCity, tblHeader.BillState, tblHeader.BillZip, " & _
"tblHeader.ShipName, tblHeader.ShipAddress1, tblHeader.ShipAddress2, tblHeader.ShipAddress3, " & _
"tblHeader.ShipCity, tblHeader.ShipState, tblHeader.ShipZip, tblDetails.ItemNo, " & _
"tblHeader.StoreNo, tblDetails.[Desc], tblDetails.Color, tblDetails.Style, 1 AS CartonCount, " & _
"tblDetails.[Size] AS [Size],IIf(tblDetails.[Size] = ""XS"",tblCartons.Qty,"""") AS XSQty, tblDetails.ItemNo as CustItem " & _
"FROM (tblHeader INNER JOIN tblDetails ON tblHeader.SO = tblDetails.SO) " & _
"INNER JOIN tblCartons ON (tblDetails.[LineNo] = tblCartons.[LineNo]) " & _
"AND (tblDetails.SO = tblCartons.SO) AND (tblDetails.ItemNo = tblCartons.ItemNo) " & _
WHERE (((tblHeader.SO) = '" & sOrder & "')) ORDER BY tblCartons.CartonNo"
Set rs = conn.Execute(sql)
 
Looks like you have double quotes. SQL Server does not like double quotes, you need to use " " with single quotes inside. try changing
Code:
IIf(tblDetails.[Size] = ""XS"",tblCartons.Qty,"""")
to
Code:
IIf(tblDetails.[Size] = "'XS'",tblCartons.Qty,"''")
just as you have it farther down
Code:
 WHERE (((tblHeader.SO) = '" & sOrder & "'))
 
CaptainD,

Yes I tried it. When you put ' (single quote) in front like this
Code:
"'XS'"
VB comments everything after the quote out.
But you are right it has something to do with SQL only liking single quotes and not double quotes. But the question is for is the right syntax to write it in VB?

Vika
 
As strongm's post points out, if you're hardcoding the value in (and not inserting a variable into the sql string) you have no need to use the double quotes within at all.

Cheers

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
I tried it with just single quotes and still getting the same runtime error,
"Line1: Incorrect syntax near '='". Any other suggestions?

conn.Open "Provider=sqloledb; Data Source=acmedc1; Initial Catalog=OrderChecking; Integrated Security=SSPI;"

sql = "SELECT tblHeader.PO, tblHeader.BillName, tblHeader.BillAddress1, tblHeader.BillAddress2, " & _
"tblHeader.BillAddress3, tblHeader.BillCity, tblHeader.BillState, tblHeader.BillZip, " & _
"tblHeader.ShipName, tblHeader.ShipAddress1, tblHeader.ShipAddress2, tblHeader.ShipAddress3, " & _
"tblHeader.ShipCity, tblHeader.ShipState, tblHeader.ShipZip, tblDetails.ItemNo, " & _
"tblHeader.StoreNo, tblDetails.[Desc], tblDetails.Color, tblDetails.Style, 1 AS CartonCount, " & _
"tblDetails.[Size] AS [Size],IIf(tblDetails.[Size] = 'XS',tblCartons.Qty,'') AS XSQty, tblDetails.ItemNo as CustItem " & _
"FROM (tblHeader INNER JOIN tblDetails ON tblHeader.SO = tblDetails.SO) " & _
"INNER JOIN tblCartons ON (tblDetails.[LineNo] = tblCartons.[LineNo]) " & _
"AND (tblDetails.SO = tblCartons.SO) AND (tblDetails.ItemNo = tblCartons.ItemNo) " & _
WHERE (((tblHeader.SO) = '" & sOrder & "')) ORDER BY tblCartons.CartonNo"
Set rs = conn.Execute(sql)
 
Provider = sqloledb

Are you using SQL Server?

The IIF syntax does not work for SQL Server.

Replace:
IIf(tblDetails.[Size] = 'XS',tblCartons.Qty,'') AS XSQty
With
Case When tblDetails.Size = 'XS' Then tblCartons.Qty Else '' End As XSQty

Also, if the tblCartons.QTY field is integer, then you should match the data types for example...

Case When tblDetails.Size = 'XS'
Then tblCartons.Qty
Else [!]0[/!] End As XSQty

Hope this help.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I greatly appreciate your help, GMMASTROS!
It worked like a magic!

Thank you very much!
 
You're welcome. I'm glad to have helped.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top