错误描述:
    实时错误'3075'
    字符串的语法错误 在查询表达式'tableName='C:\Program Files\Zontec\Synergy2000Engineer Demo\tutor1.ztb' and ID1='Lot # 78'中
出错处:
    Set dbrecordset = db.OpenRecordset("select tableName,averageObs from tableVariableValue where tableName=" + "'" + dataTableName + "'" + "and ID1=" + "'" + SID1 + "'")
部分代码是:
Function countAveValue(ByVal dataTableName As String, ByVal SID1 As String, ByVal SID2 As String, ByVal SID3 As String)
averageValue = 0
If Trim(SID1) <> "ID1" Then
   Set dbrecordset = db.OpenRecordset("select tableName,averageObs from tableVariableValue where tableName=" + "'" + dataTableName + "'" + "and ID1=" + "'" + SID1 + "'")
   If Trim(SID2) <> "ID2" Then
   Set dbrecordset = db.OpenRecordset("select tableName,averageObs from tableVariableValue where tableName=" + "'" + dataTableName + "'" + "and ID1=" + "'" + SID1 + "'" + " and ID2=" + "'" + SID2 + "'")
   End If
ElseIf Trim(SID1) = "ID1" Then
   If Trim(SID2) <> "ID2" Then
   Set dbrecordset = db.OpenRecordset("select tableName,averageObs from tableVariableValue where tableName=" + "'" + dataTableName + "'" + "and ID2=" + "'" + SID2 + "'")
   End If
End If
end function我用的是dao连接access数据库,我是个新手,查来查去,查不出哪里错了,请高手们帮帮忙,不胜感激!

解决方案 »

  1.   

    where 条件中的and 前面都没加空格
      

  2.   

    Set dbrecordset = db.OpenRecordset("select tableName,averageObs from tableVariableValue where tableName=" + "'" + dataTableName + "'" + "and ID1=" + "'" + SID1 + "'")
    and之前加空格tableVariableValue:这个是你的table名称么?还是VariableValue是你的table名另外你检查的时候把datatablename和id1都换成比较普通的值,比如tb1与hello。看看还是不是出现字符串错误
      

  3.   

    "select tableName,averageObs "& _
    "from tableVariableValue "& _
    "where tableName='"& trim(dataTableName) &"' and ID1='"& trim(SID1) &"'"
      

  4.   

    tableVariableValue 是table名称
      

  5.   

    将tableName 和 sid 换成普通值(如:t1,id1)之后,果然可以了!谢谢
      

  6.   

    tableName 不换也是可以的,问题就出在sid上。
    我的tableVariableValue中的数据是通过dll从其它程序导出来的,其中ID1,ID2,ID3的长度是固定20,值是这样的,如:"Lot # 78            ",(Lot # 78后面还有个小下标)
    所以我在用ID号作为条件进行查询时出现了困难,怎样设定SID的值使它能与ID相匹配,不致于查询为空呢?