2014-02-22 105 views
2

我正在使用MS Access 2007並使用鏈接表連接到SQL Server,並且在我使用Access表時正常工作的SQL查詢現在正在獲取上述錯誤消息,現在我正在使用一個鏈接表。我的代碼,failes低於:ODBC - 調用失敗

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]