我在 VBA 中有以下代码:

Dim strSQL As String
strSQL = "UPDATE Workstations SET MID = newvalue WHERE MID = tempvalue"
DoCmd.RunSQL strSQL

newvalue 和 tempvalue 都是全局变量并且已经被设置了值。语法明智,这有意义吗?还是我缺少引号?

最佳答案

试试这个:

如果 MID 是数字:

Dim strSQL As String
strSQL = "UPDATE Workstations SET [MID] = " & newvalue & " WHERE [MID] = " & tempvalue
DoCmd.RunSQL strSQL

如果 MID 是字符串(如果 newvalue/tempvalue 不包含单引号 ' ):
Dim strSQL As String
strSQL = "UPDATE Workstations SET [MID] = '" & newvalue & "' WHERE [MID] = '" & tempvalue & "'"
DoCmd.RunSQL strSQL

如果 MID 是字符串(如果 newvalue/tempvalue 包含单引号 '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 在评论中指出

关于sql - SQL 语句中的全局变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22563844/

10-12 15:00