首页 文章

MS Access VBA - 语法错误,截断更新sql语句

提问于
浏览
0

我在数据表视图中有一个子表单,其recordource是一个临时表 . 因此,每当用户在表单上插入新事务时,她就会将新记录插入临时表 . 子窗体已设置,因此用户无需输入事务日期,这意味着子窗体中的数据表不显示日期字段 . 日期通过主窗体中的文本框插入 .

每当插入记录时,我都使用子表单的AfterUpdate sub和SQL UPDATE语句将日期插入临时表 . 但是我收到了语法错误 . 这是我正在使用的代码:

Dim db As Database
Dim currentID As Integer
Dim transactionDate As Date
Dim strSQL As String

Private Sub Form_AfterUpdate()
    Set db = CurrentDb
    currentID = Me!txtID
    transactionDate = Me.Parent!txtDate

    strSQL = "UPDATE tblTempTable " _
    & "SET transactionDate = " & transactionDate _
    & " WHERE ID = " & currentID & ";"

Debug.Print (strSQL)
db.Execute strSQL

End Sub

正如您所看到的那样,我在执行之前正在引用SQL语句并打印出来: UPDATE tblTempTable SET transactionDate = 17.10.2016 WHERE ID = 55; 但是我收到了一个sytnax错误: Syntax error in number in query expression '17.10.201' . 当我在使用 & "SET transactionDate = #" & transactionDate & "#" _ 的hashtags里面的日期时,我也得到语法错误: Syntax error in date in query expression '#17.10.2016' . 同时Debug.Print输出: UPDATE tblTempTable SET transactionDate = #17.10.2016# WHERE ID = 56;

所以似乎语句的结尾被截断了,但我不明白为什么 . 有人能帮助我吗?

2 回答

  • 0

    尝试以ISO格式提供日期:

    UPDATE tblTempTable SET transactionDate = #2016-10-17# WHERE ID = 55;
    
  • 1

    如果今天的日期,请使用日期:

    strSQL = "UPDATE tblTempTable " & _ 
    "SET transactionDate = Date() " & _
    "WHERE ID = " & currentID & ";"
    

    或使用格式:

    strSQL = "UPDATE tblTempTable " & _ 
    "SET transactionDate = #" & Format(transactionDate, "yyyy\/mm\/dd") & "# " & _
    "WHERE ID = " & currentID & ";"
    

相关问题