MS Access - 更新 SQL 查询错误 3061
MS Access - UPDATE SQL Query Error 3061
我尝试使用从 InputBox 传送的文本更新 table 中的数据。
Private Sub editname_Click()
Dim GivenNameTB As String
Dim EnterName As String
Dim SQl As String
Dim LocationID As Integer
Me.txt_name.SetFocus
GivenNameTB = Me.txt_name.Text
EnterName = InputBox("Change name", "Change name", GivenNameTB)
LocationID = Me.list_ma.Column(1)
SQl = " Update tWorkers SET GivenName = forms!mainform!EnterName WHERE tWorkers.IDName = forms!mainform!LocationID "
CurrentDb.Execute SQl
End Sub
但是,我收到错误代码 3061 "Too few parameters. Expected 2"
编辑:
tWorkers的table结构:
IDName - auto-increment (primary key)
LastName - text
GivenName - text
我按 SET GivenName = ...
定位 GivenName 列,按 LocationID 定位行。
LocationID 从列表字段 list_ma 中获取其值。列表字段由五列组成,而 IDName 是第 2 列。
我的重点是更新 table 中的字段。我表单中的文本框显示了一个名称,可以通过单击按钮进行编辑。然后弹出一个输入框。输入的字符串应保存在所需的字段中。
我认为你需要 DoCmd.RunSQL
而不是 CurrentDb.Execute
。
Private Sub editname_Click()
Dim GivenNameTB As String
Dim EnterName As String
Dim SQl As String
Dim LocationID As Integer
Me.txt_name.SetFocus
GivenNameTB = Me.txt_name.Text
EnterName = InputBox("Change name", "Change name", GivenNameTB)
LocationID = Me.list_ma.Column(1)
SQL = " Update tWorkers SET GName = " & chr(34) & EnterName & chr(34) & " WHERE tWorkers.IDName = " & LocationID
Debug.Print SQL -- For checking what code we are running.
DoCmd.RunSQL SQL
End Sub
重新阅读您的问题,您的数据存在于 VBA 变量中,而不是表单控件中。所以你不能从表单中读取参数(DoCmd.RunSQL
无济于事)。
您必须动态构建 SQL 字符串,最好使用 :
SQL = "Update tWorkers SET GivenName = " & CSql(EnterName) & _
" WHERE tWorkers.IDName = " & CSql(LocationID)
Debug.Print SQL
CurrentDb.Execute SQL
我尝试使用从 InputBox 传送的文本更新 table 中的数据。
Private Sub editname_Click()
Dim GivenNameTB As String
Dim EnterName As String
Dim SQl As String
Dim LocationID As Integer
Me.txt_name.SetFocus
GivenNameTB = Me.txt_name.Text
EnterName = InputBox("Change name", "Change name", GivenNameTB)
LocationID = Me.list_ma.Column(1)
SQl = " Update tWorkers SET GivenName = forms!mainform!EnterName WHERE tWorkers.IDName = forms!mainform!LocationID "
CurrentDb.Execute SQl
End Sub
但是,我收到错误代码 3061 "Too few parameters. Expected 2"
编辑:
tWorkers的table结构:
IDName - auto-increment (primary key)
LastName - text
GivenName - text
我按 SET GivenName = ...
定位 GivenName 列,按 LocationID 定位行。
LocationID 从列表字段 list_ma 中获取其值。列表字段由五列组成,而 IDName 是第 2 列。
我的重点是更新 table 中的字段。我表单中的文本框显示了一个名称,可以通过单击按钮进行编辑。然后弹出一个输入框。输入的字符串应保存在所需的字段中。
我认为你需要 DoCmd.RunSQL
而不是 CurrentDb.Execute
。
Private Sub editname_Click()
Dim GivenNameTB As String
Dim EnterName As String
Dim SQl As String
Dim LocationID As Integer
Me.txt_name.SetFocus
GivenNameTB = Me.txt_name.Text
EnterName = InputBox("Change name", "Change name", GivenNameTB)
LocationID = Me.list_ma.Column(1)
SQL = " Update tWorkers SET GName = " & chr(34) & EnterName & chr(34) & " WHERE tWorkers.IDName = " & LocationID
Debug.Print SQL -- For checking what code we are running.
DoCmd.RunSQL SQL
End Sub
重新阅读您的问题,您的数据存在于 VBA 变量中,而不是表单控件中。所以你不能从表单中读取参数(DoCmd.RunSQL
无济于事)。
您必须动态构建 SQL 字符串,最好使用
SQL = "Update tWorkers SET GivenName = " & CSql(EnterName) & _
" WHERE tWorkers.IDName = " & CSql(LocationID)
Debug.Print SQL
CurrentDb.Execute SQL