ODBC - 调用失败

时间:2014-02-22 20:41:52

标签: sql sql-server ms-access odbc

我正在使用MS Access 2007并使用链接表连接到SQL Server,并且当我使用Access表时,以前工作正常的SQL查询现在正在获取上述错误消息,因为我正在使用链接表。我发现的代码如下:

Dim rst As Recordset
Dim db As Database
Dim sql As String


If Not (ToDate.Value = "" And FromDate.Value = "") Then

    Set db = CurrentDb()

    sql = "SELECT dbo_tblSurvey.SurveyID " & _
"FROM dbo_tblSurvey INNER JOIN (dbo_tblInvestigType INNER JOIN dbo_tblInvestigator " &_
"ON dbo_tblInvestigType.InvestigNum = dbo_tblInvestigator.InvestigNum)" & _
"ON dbo_tblSurvey.SurveyID = dbo_tblInvestigType.SurveyID " & _
"WHERE ((" & FromDate & " <= dbo_tblSurvey.RegisDate <= " & ToDate & ")" & _
"AND (dbo_tblInvestigType.InvestigType = 'Primary'))" & _
"ORDER BY dbo_tblSurvey.SurveyID"


    Set rst = db.OpenRecordset(sql, dbOpenDynaset, dbSeeChanges)

我玩过SQL查询的格式,发现如果我只是从我的查询中取出“ToDate&#39;控制,以便我的代码如下所示,然后它工作得很好:

Dim rst As Recordset
Dim db As Database
Dim sql As String


If Not (ToDate.Value = "" And FromDate.Value = "") Then

    Set db = CurrentDb()

    sql = "SELECT dbo_tblSurvey.SurveyID " & _
"FROM dbo_tblSurvey INNER JOIN (dbo_tblInvestigType INNER JOIN dbo_tblInvestigator " &_
"ON dbo_tblInvestigType.InvestigNum = dbo_tblInvestigator.InvestigNum)" & _
"ON dbo_tblSurvey.SurveyID = dbo_tblInvestigType.SurveyID " & _
"WHERE ((" & FromDate & " <= dbo_tblSurvey.RegisDate)" & _
"AND (dbo_tblInvestigType.InvestigType = 'Primary'))" & _
"ORDER BY dbo_tblSurvey.SurveyID"


    Set rst = db.OpenRecordset(sql, dbOpenDynaset, dbSeeChanges)

&#39; ToDate&#39; control是一个文本输入控件,用于输入日期,并且与“FromDate”相同。以各种方式控制,除了它们的名称不同。 关于为什么会发生这种情况的任何想法?感谢任何人都能提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

我认为该代码构建了一个WHERE子句,类似于:

WHERE [one date] <= dbo_tblSurvey.RegisDate <= [another date]

相反,目标是类似于...

WHERE [one date] <= dbo_tblSurvey.RegisDate
      AND dbo_tblSurvey.RegisDate <= [another date]

或......

WHERE dbo_tblSurvey.RegisDate BETWEEN [one date] AND [another date]