简体   繁体   中英

Syntax on SQL statement with multiple conditions in WHERE clause

I am getting a run time 3075 issue on the following SQL string below. Is it possible I am missing parentheses?

    sql_get = 
"SELECT [tblCompetency02].[HighLevelObjective], 
[tblCompetency04].[Self], 
[tblCompetency04].[SelfSpecialLanguage], 
[tblCompetency04].[SelfChecklist], 
[tblCompetency04].[Team], 
[tblCompetency04].[TeamSpecialLanguage], 
[tblCompetency04].[TeamChecklist],
 [tblCompetency04].[Organisation],
 [tblCompetency04].[OrganisationSpecialLanguage], 
[tblCompetency04].[OrganisationChecklist],
 [tblCompetency02].[Competency] 
FROM [tblCompetency04] 
INNER JOIN [tblCompetency02] 
ON [tblCompetency04].[HighLevelObjective] = [tblCompetency02].[ID] 
WHERE [tblcompetency04].[self]<>"" or [tblcompetency04].[team]<>"" or [tblcompetency04].[organisation]<>"""
        Form_frmStaticDataSkills02.Form.RecordSource = sql_get

Examine the WHERE clause of the statement your code creates.

Here's an Immediate window session:

sql_get = "WHERE [tblcompetency04].[self]<>"" or [tblcompetency04].[team]<>"" or [tblcompetency04].[organisation]<>"""
Debug.Print sql_get
WHERE [tblcompetency04].[self]<>" or [tblcompetency04].[team]<>" or [tblcompetency04].[organisation]<>"

Notice there is just one double quote character in each of these cases: <>"

If you want to have double quotes inside the string, use two to get one ...

sql_get = "WHERE [tblcompetency04].[self]<>"""" or [tblcompetency04].[team]<>"""" or [tblcompetency04].[organisation]<>"""""
Debug.Print sql_get
WHERE [tblcompetency04].[self]<>"" or [tblcompetency04].[team]<>"" or [tblcompetency04].[organisation]<>""

But I think it is less confusing and less error-prone to use single quotes inside the string ...

sql_get = "WHERE [tblcompetency04].[self]<>'' or [tblcompetency04].[team]<>'' or [tblcompetency04].[organisation]<>''"
Debug.Print sql_get
WHERE [tblcompetency04].[self]<>'' or [tblcompetency04].[team]<>'' or [tblcompetency04].[organisation]<>''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM