访问VBA:查询输入必须至少包含一个表或查询

时间:2015-11-04 17:49:38

标签: vba sql-update ms-access-2010

我尝试根据用户输入的ID编码按钮来更新行。 ID是表生成的自动编号字段。用户可以使用另一个按钮创建行,但是当他们需要修复错误时,我希望他们能够使用正确的信息更新行。但是,我的代码在标题中给出了错误。

这是"更新"按钮:

Private Sub Command133_Click()

    Dim dbsCurrent As Database
    Set dbsCurrent = CurrentDb

    dbsCurrent.Execute " INSERT INTO tbl_Downtime " _
    & "(job, suffix, production_date, reason, downtime_minutes, comment) VALUES " _
    & "('" & Me.Text116 & "','" & Me.Text118 & "','" & Me.Text126 & "','" & Me.Text121 & "','" & Me.Text123 & "','" & Me.Text128 & "') WHERE ID='" & Me.Text135 & "';"

    Call ClearControl(Me.Text116)
    Call ClearControl(Me.Text118)
    Call ClearControl(Me.Text126)
    Call ClearControl(Me.Text121)
    Call ClearControl(Me.Text123)
    Call ClearControl(Me.Text128)

End Sub

1 个答案:

答案 0 :(得分:0)

看起来问题与您在SQL语句末尾的WHERE语句有关。使用INSERT INTO ... VALUES ...方法用于插入一行提供的值,因此使用WHERE子句是没有意义的。

相反,如果您尝试在现有表中插入一行或多行的值,那么您希望编写类似以下内容的SQL:

 "INSERT INTO tbl_Downtime (job, suffix, production_date, reason, downtime_minutes, comment)
 SELECT '" & Me.Text116 & "', '" ... [your other fields go here] ... "'
 FROM [src_table] As t
 WHERE t.ID = '" & Me.Text135 & "';"

修改 如果您希望更新现有行中的值而不是尝试使用新值添加新行,那么您需要这样的内容,而不是:

"UPDATE tbl_Downtime SET job = '" & Me.Text116 & "', suffix = '" & Me.Text118 & "', production_date = #" & Me.Text126 & "#, reason = '" & Me.Text121 & "', downtime_minutes = " & Me.Text123 & ", comment = '" & Me.Text128 & "' WHERE t.ID = '" & Me.Text135 & "';"