运行时错误3075缺少运算符

时间:2015-06-24 14:10:44

标签: vba ms-access ms-access-2010

当我使用ms access 2007时,它可以完美运行。我打开ms访问2010,现在它不起作用。

 DoCmd.RunSQL ("INSERT INTO Pending_Orders (Customer, ItemNumber, Description,   Qty, [Order #], Temp, ShipDate) VALUES (" & _
  "'" & Replace(rst!Customer, "'", "''") & "','" & rst![Item #] & "','" & rst!Description & "'," & rst!Qty & ",'" & rst![Order #] & "'," & NextTemp & ",#" & rst![Ship Date] & "#)")

我现在收到此错误。

它可能是什么?

2 个答案:

答案 0 :(得分:0)

我建议你将上面代码中的单引号替换为双引号,这是Paul O' Connor等名称的一个常见问题。也可以在DoCmd.RunSQL上使用CurrentDb.Execute。使用currentBD可以抑制恼人的消息并查看更多信息。最后,总是使用String来获取SQL查询,因此调试可能会容易得多。

尝试以下方法,

strSQL = "INSERT INTO Pending_Orders (Customer, ItemNumber, Description, Qty, [Order #], " & _
         "Temp, ShipDate) VALUES (" & Chr(34) & rst!Customer & Chr(34) & _
         ", " & Chr(34) & rst![Item #] & Chr(34) & _
         ", " & Chr(34) & rst!Description & Chr(34) & _
         ", " & rst![Qty] & ", " & Chr(34) & rst![Order #] & Chr(34) & _
         ", " & NextTemp & ", " & Format(rst![Ship Date], "\#mm\/dd\/yyyy\#") & ")

CurrentDB.Execute strSQL 

答案 1 :(得分:0)

除了Paul Francis的回答,我还提供了EscapeDBstring函数,它解决了" Paul O' Connor"中的嵌入引用问题。通过转义嵌入的引用:

' dbEscapeString -- escape the single quote in a string that will be included in
'                   an SQL statement. The single quote is the database/SQL string delimiter.
Public Function dbEscapeString(ByVal s)
Dim i
    i = 1
    While (i <= Len(s))
        If (Mid(s, i, 1) = "'") Then
            'If (Mid(s, i + 1, 1) = "'") Then   ' this would consider two single quotes to be escaped already
            '    i = i + 1
            'Else
                s = Mid(s, 1, i) + Mid(s, i)
                i = i + 1
            'End If
        End If
        i = i + 1
    Wend
    dbEscapeString = s
End Function
相关问题