本文介绍了SQL 语句中的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 VBA 中有以下代码:
I have the following code in VBA:
Dim strSQL As String
strSQL = "UPDATE Workstations SET MID = newvalue WHERE MID = tempvalue"
DoCmd.RunSQL strSQL
newvalue 和 tempvalue 都是全局变量,并且已经设置了值.语法明智,这有意义吗?还是我遗漏了引号?
newvalue and tempvalue are both global variables and have already been set values. Syntax wise, does this make sense? or am I missing quotation marks?
推荐答案
试试这个:
如果MID
是数字:
Dim strSQL As String
strSQL = "UPDATE Workstations SET [MID] = " & newvalue & " WHERE [MID] = " & tempvalue
DoCmd.RunSQL strSQL
如果 MID
是 string(如果 newvalue
/tempvalue
不包含单引号 '
):
If MID
is string (if newvalue
/tempvalue
doesn't contain single quote '
):
Dim strSQL As String
strSQL = "UPDATE Workstations SET [MID] = '" & newvalue & "' WHERE [MID] = '" & tempvalue & "'"
DoCmd.RunSQL strSQL
如果 MID
是 string(如果 newvalue
/tempvalue
包含单引号 '
> 喜欢 newvalue="Mike's car"
):
If MID
is string (if newvalue
/tempvalue
contains single quote '
like newvalue="Mike's car"
):
Dim strSQL As String
strSQL = "UPDATE Workstations SET [MID] = '" & Replace(newvalue, "'", "''") & "' WHERE [MID] = '" & Replace(tempvalue, "'", "''") & "'"
DoCmd.RunSQL strSQL
如果MID
是日期:
Dim strSQL As String
strSQL = "UPDATE Workstations SET [MID] = #" & newvalue & "# WHERE [MID] = #" & tempvalue & "#"
DoCmd.RunSQL strSQL
感谢@HansUp 在评论中指出
Thanks to @HansUp for pointing out in comments, that
MID 是函数的名称,因此在 SQL 语句中将该字段名称括起来或别名会更安全:SET [MID]
这篇关于SQL 语句中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!