WHERE子句中具有多个条件的SQL语句的语法

时间:2015-05-17 14:44:09

标签: sql ms-access access-vba ms-access-2010

我在下面的SQL字符串上遇到运行时3075问题。我有可能错过括号吗?

    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

1 个答案:

答案 0 :(得分:3)

检查代码创建的语句的WHERE子句。

这是一个立即窗口会话:

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

请注意,每种情况下只有一个双引号字符:<>"

如果你想在字符串中加双引号,可以用两个来得到一个...

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

但是我认为在字符串中使用单引号不那么容易混淆且不易出错...

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