ODBC——调用失败

Posted

技术标签:

【中文标题】ODBC——调用失败【英文标题】:ODBC -- call failed 【发布时间】:2014-02-22 20:41:52 【问题描述】:

我正在使用 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”控件,使我的代码如下所示,那么它就可以正常工作:

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)

“ToDate”控件是用于输入日期的文本输入控件,除了名称不同外,其他方面都与“FromDate”控件相同。 关于为什么会发生这种情况的任何想法?感谢任何人都可以提供的任何帮助。

【问题讨论】:

【参考方案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]

【讨论】:

以上是关于ODBC——调用失败的主要内容,如果未能解决你的问题,请参考以下文章

ODBC--调用失败

ODBC 调用失败 - 错误 3151

为啥我通过 ODBC 调用的存储过程在同一位置失败? [关闭]

从链接表访问 ODBC 调用失败

ODBC 调用失败 - 从 32 位 ODBC 5.1 移动到 64 位 5.3 后出现随机日期溢出错误

MS Access 错误“ODBC--调用失败。转换规范 (#0) 的字符值无效”