我在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在评论中指出,那
MID is the name of a function,so it would be safer to bracket or alias that field name in the sql statement: SET [MID]