我在查询向导的添加/更新按钮中创建了一个SQL语句,我将其更改回SQL视图,以查看程序如何使我成为代码,以及何时在btnAdd的If语句中复制并粘贴相同的错误它会引发语法错误,但是如何?
这是整个代码:
Private Sub cmdAdd_Click()
'In the button add we have two options
'1. Insert
'2. Update
If Me.txtID.Tag & "" = "" Then
CurrentDb.Execute "INSERT INTO tblClients ( ClientID, ClientName, Gender, " & _
"City, [Address (Fisical)], [Cellphone/Telephone] ) " & _
"SELECT " & Me.txtID & ",'" & Me.txtName & "','" & Me.cboGender & "', '" & Me.cboCity & "','" & Me.txtAddress & "','" & Me.txtCellphone & "'"
Else
'Otherwise the data will be updated
CurrentDb.Execute "UPDATE tblClients SET tblClients.ClientName = [me]. [txtName], tblClients.Gender = [me].[cboGender], tblClients.City = [me].[cboCity], tblClients.[Address (Fisical)] = [me].[txtAddress], tblClients.[Cellphone/Telephone] = [me].[txtCellphone] "
WHERE (([ClientID]=[Me].[txtID].[Tag]));
End If
cmdClear_Click
tblClients_subform.Form.Requery
End Sub
它突出显示我的这一行是红色的:
WHERE (([ClientID]=[Me].[txtID].[Tag]));
答案 0 :(得分:0)
以下代码似乎不在同一行
(require 'perl-mode)
(add-hook 'cperl-mode-hook
(lambda ()
(setq font-lock-defaults
'((perl-font-lock-keywords perl-font-lock-keywords-1 perl-font-lock-keywords-2)
nil nil ((?\_ . "w")) nil
(font-lock-syntactic-face-function . perl-font-lock-syntactic-face-function)))
(font-lock-refresh-defaults)))
所以你可能想把它改成
CurrentDb.Execute "UPDATE tblClients SET tblClients.ClientName = [me]. [txtName], tblClients.Gender = [me].[cboGender], tblClients.City = [me].[cboCity], tblClients.[Address (Fisical)] = [me].[txtAddress], tblClients.[Cellphone/Telephone] = [me].[txtCellphone] "
WHERE (([ClientID]=[Me].[txtID].[Tag]))
答案 1 :(得分:0)
在INSERT语句中将SELECT关键字更改为VALUES。
CurrentDb.Execute "INSERT INTO tblClients ( ClientID, ClientName, Gender, " & _
"City, [Address (Fisical)], [Cellphone/Telephone] ) " & _
"VALUES (" & Me.txtID & ",'" & Me.txtName & "','" & Me.cboGender & "', '" & Me.cboCity & "','" & Me.txtAddress & "','" & Me.txtCellphone & "')"
UPDATE应该是这个。这里的问题是你试图在SQL中使用Form
控件,但是你需要首先评估控件然后将它们的值连接到文字字符串。
我想知道您是否真的需要Me.txtID
而不是Me.txtID.Tag
如果它不起作用,那就摇摆不定。
CurrentDb.Execute "UPDATE tblClients SET tblClients.ClientName = '" & me.txtName & "', tblClients.Gender = '" & me.cboGender & "', tblClients.City = '" & me.cboCity & "', tblClients.[Address (Fisical)] = '" & me.txtAddress & "', tblClients.[Cellphone/Telephone] = '" & me.txtCellphone & "' WHERE (([ClientID]=" & Me.txtID.Tag & "));"
答案 2 :(得分:0)
除了Cableload的正确答案之外,新代码行上的WHERE
语句未通过在末尾使用下划线连接到上一行第一个,仍有一个引用问题。
您正在引用UserForm中的值,就像表中的列一样,因此找不到您要查找的值。要将值放入SQL语句中,您需要从文字字符串中取出,引用该值,然后继续写入字符串(不要忘记用'
括起来): -
CurrentDb.Execute "UPDATE tblClients SET " & _
"[ClientName] = '" & Me.txtName & "', " & _
"[Gender] = '" & Me.cboGender & "', " & _
"[City] = '" & Me.cboCity & "', " & _
"[Address (Fisical)] = '" & Me.txtAddress & "', " & _
"[Cellphone/Telephone] = '" & Me.txtCellphone & "' " & _
"WHERE [ClientID]=" & Me.txtID.Tag
为了便于阅读,我将其分布在多行中,但显然您可以根据需要调整实际代码。
我还会质疑[ClientID]=" & Me.txtID.Tag
,是txtID.value
或txtID.Tag
中的ClientID,它们是不同的地方。 value
属性是文本框中的值,Tag
属性更像是元数据区域,您可以根据需要填充,但默认情况下不会自动填充。
最后,我想回过头来回答你所拥有的previous question的答案,在答案的底部有一个关于将结果查询放入SQL视图中的Access Query的提示获得有关错误的更好信息,这也可以帮助你。为“结果查询”提供进一步的帮助。
CurrentDb.Execute
突出显示但在运行之前(使用F8逐步执行每一行,直到到达那里,或在该行上放置断点CurrentDb.Execute
语句后复制所有相关代码,在这种情况下,它将是UPDATE ... .Tag