I seem to continually run in to problems when setting a string as and SQL statement I had pre-written in SSMS.
I have 2 questions which are both related. First, I only seem to run in to problems when dealing with a bit more complex queries (several joins, where conditions, correlated sub queries, and case statements) as a opposed to simple queries. Anyone have any good resources where I can read about syntax 'rules' when writing queries in SSMS to VBA?
Second, below is an example of a query I wrote in the AdventureWorks sample DB that gives me the error listed in the title of this question. My T-SQL statement is:
sSQL = "SELECT ba.BusinessEntityID,ba.AddressID
,(CASE WHEN ba.BusinessEntityID > 5
THEN
(SELECT pp.FirstName FROM [Person].[Person] pp
WHERE pp.BusinessEntityID = ba.BusinessEntityID)
Else 'AA' END) AS 'TEST'
FROM Person.BusinessEntityAddress ba
INNER JOIN person.AddressType at ON at.AddressTypeID = ba.AddressTypeID
WHERE ba.BusinessEntityID < 11;"
Error occurs on line "Set rs = db.OpenRecordset(sSQL, dbOpenDynaset, dbSeeChanges)" which says:
"Run-Time Error 3075 Syntax Error (missing operator) in query expression '(CASE WHEN ba.BusinessEntityID > 5 THEN (SELECT pp.FirstName FROM [Person].[Person] pp WHERE pp.BusinessEntityID = ba.BusinessEntityID) Else 'AA' END) AS 'TEST'"