我尝试使用从InputBox传递的文本更新表中的数据。
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"参数太少。预计2"
编辑:
tWorkers 的表结构:
IDName - auto-increment (primary key)
LastName - text
GivenName - text
我按SET GivenName = ...
定位列 GivenName ,按位置ID定位行。
LocationID从列表字段 list_ma 中获取其值。列表字段由五列组成,而IDName为第二列。
我的重点是更新表格中的字段。我的表单中的文本框显示一个名称,可以通过单击按钮进行编辑。然后弹出一个输入框。输入的字符串应保存在所需的字段中。
答案 0 :(得分:1)
重新阅读您的问题,您的数据存在于VBA变量中,而不是表单控件中。因此,您无法从表单中读取参数(DoCmd.RunSQL
无效)。
您必须动态构建SQL字符串,最好使用CSql()
by Gustav:
SQL = "Update tWorkers SET GivenName = " & CSql(EnterName) & _
" WHERE tWorkers.IDName = " & CSql(LocationID)
Debug.Print SQL
CurrentDb.Execute SQL
答案 1 :(得分:0)
我认为您需要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