I’m using this code for an Access 2010 database, and I seem to have a problem with the syntax of my SQL statement, but I can’t for the life of me figure out how to correctly format the statement. Thank you in advance for any help!
qdf = db.CreateQueryDef("Company_State_Q")
Dim strSQLSearch As String
strSQLSearch = "SELECT [Company Information].Company_Name, " & _
"[Company Information].Industry" & _
"FROM Company Information" & _
"WHERE [Company Information].State ='" & stateV & "'" & _
"ORDER BY [Company Information].Company_Name;"
qdf.SQL = strSQLSearch
You should use …
… in your code after you build the
strSQLSearchstring. You can then view the string in the Immediate window (go there with Ctrl+g).From the Immediate window I set the value of
stateVto “CA” and tested your string like this:Once you actually see the completed
strSQLSearchstring, it’s easy to spot multiple problems:IndustryFROMshould beIndustry FROMFROM Company Informationshould beFROM [Company Information]InformationWHEREshould beInformation WHERE'CA'ORDERshould be'CA' ORDERConsider using line breaks between the sections of your
SELECTstatement. The db engine is perfectly happy with line breaks instead of spaces. And you may find it easier to read the statement as several short lines rather than as one long line.Also at the beginning of your code you have …
It seems
qdfmust be a DAO.QueryDef object, so I think you should use theSetkeyword to assign to it.