Question

I am using MS Access 2007 and connecting to SQL Server using linked tables, and a SQL query that used to work fine when I was using Access tables is now getting the above error message now that I am using a linked table. My code that failes is below:

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)

I have played around with the format of the SQL query and found that if I simply take out from my query the 'ToDate' control so that my code looks like the following, then it works just fine:

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)

The 'ToDate' control is a text entry control for the purpose of entering a date, and is identical to the 'FromDate' control in every way except that they are named differently. Any ideas out there as to why this is happening? Thanks for any help that anyone can provide.

No correct solution

OTHER TIPS

I think that code builds a WHERE clause similar to:

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

Instead, aim for something similar to ...

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

or ...

WHERE dbo_tblSurvey.RegisDate BETWEEN [one date] AND [another date]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top