xJustin8
xJustin8

Reputation: 11

Access VBA Error 3075 Syntax Error (missing operator)

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'"

Upvotes: 0

Views: 2111

Answers (2)

Jericho Johnson
Jericho Johnson

Reputation: 759

The issue here is that you are trying to use a TSQL statement where an Access SQL statement is expected. Access SQL does not support CASE WHEN, or many of the constructs that TSQL supports.

The OpenRecordset method expects a table name, saved query name, or Access SQL statement.

You can solve this by creating and saving a Pass Through Query in Microsoft Access. When you create a Pass Through Query, you can then use your TSQL statement, and you set the connection string to your SQL database in the properties of the Pass Through Query.

As the name implies, Access passes the parsing and execution of a Pass Through Query over to SQL Server.

Once you have saved your Pass Through Query and tested it, you can use your OpenRecordset command as-is, just replace the first parameter with the name of the Pass Through Query.

Upvotes: 0

Utsav
Utsav

Reputation: 8143

Try this

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;"

No need to using quotes in alias name. If you must, then there are other ways (like drop AS or use Double quotes) but it is not suggested.

Upvotes: 0

Related Questions