访问VBA SQL运行时错误'3075'语法错误

时间:2017-10-17 22:07:12

标签: sql vba ms-access

我正在尝试运行此代码,但它给出了错误消息:

  

运行时错误'3075'; “查询表达式中的语法错误(缺少运算符)”

我该如何解决此错误?

我想将 table1 中的 CellNumber 插入变量表中 ERROR 表中< em> DateTime ,字符串和 CellNumber

Dim sqlstr As String

sqlstr = "SELECT CellNumber FROM table1 WHERE CellNumber NOT IN (SELECT CellNumber FROM Variable)"

DoCmd.RunSQL "INSERT INTO ERROR ([DateTime], RowNum, Error) SELECT Now(), 'string' as RowNum, CellNumber FROM(" & sqlstr & ")"

1 个答案:

答案 0 :(得分:1)

 Dim sqlstr As String
    sqlstr = "INSERT INTO Error ( [DateTime],RowNum, Error ) " & _
             "SELECT Now() , 'String' , table1.cellnumber FROM table1 " & _
             "LEFT JOIN Variable ON table1.cellnumber = Variable.cellnumber " & _
             "WHERE  Variable.cellnumber Is Null"
    currentdb.execute sqlstr, dbfailonerror

我就是这样做的

修改------ 我想为后人添加一些评论:

  1. 我个人会将我的DateTime标记字段设置为默认值Now(),并将其从我的sql中省略...所有新记录都会有时间戳,错误空间更小。
  2. Dim sqlstr As String
            sqlstr = "INSERT INTO tblError ( StringField, CellNumber ) " & _
                     "SELECT 'String' , table1.cellnumber FROM table1 " & _
                     "LEFT JOIN table2 ON table1.cellnumber = table2.cellnumber " & _
                     "WHERE  table2.cellnumber Is Null"
            currentdb.execute sqlstr, dbfailonerror
    
    1. 我绝不会将DateTimeErrorVariable用作表格或字段名称,因为即使访问可以处理它(通过分隔保留名称[DateTime] ,对于Instance),在尝试对代码和SQL进行故障排除时可能会造成混淆和误导。