【问题标题】:VBA SQL String Run-Time Error 3075VBA SQL 字符串运行时错误 3075
【发布时间】:2016-05-11 22:08:42
【问题描述】:

我有一个将数据添加到表中的字符串,因此我可以从数据中打印报告或标签。数据由地址组成,并且由于地址中的逗号而导致字符串失败。这个字符串一直在工作,但是当它有一些奇怪的地址时,我认为这就是造成这种情况的原因。

sqls = "INSERT INTO tInvReportDataWrk(SO,ITEM,QTY,billTO,shipTO,LINEKEY)VALUES('" & 
  SO & "', '" & it & "', '" & qty & "', '" & billTO & "', '" & shipTO & "', '" & lk & "')"

debug.print 中尝试的数据如下所示

INSERT INTO tInvReportDataWrk(SO,ITEM,QTY,billTO,shipTO,LINEKEY)
VALUES('0000001', 'L-R_4-8R2B-01', '2', 'BAR - ANAHEIM
BAR BRANCH
P.O. BOX 00000
VENDOR ID# VC-00001
Saint Louis, MO  00008
', 'ABC ELEMENT WAREHOUSE
2000 O'TOOL AVE.
San Jose, CA  95131-1301
', '000001')

【问题讨论】:

  • 准备好的语句也可能对您有所帮助,因为这样您在构建字符串时就不必考虑特殊字符。看看这个样本是否对你有帮助。 stackoverflow.com/questions/6572448/…

标签: sql ms-access vba


【解决方案1】:

我不确定地址中的逗号是否是问题所在。在我看来,O'TOOL 中的撇号应该是个问题。但是,如果它不是 Access 抱怨的第一个错误的原因,它应该在您修复第一个错误后触发另一个错误。

举个简单的例子,下面的代码触发了错误3075,“查询表达式''O'TOOL'中的语法错误(缺少运算符);'。”

Dim strInsert As String
strInsert = "INSERT INTO tblFoo(text_field) VALUES ('O'TOOL');"
CurrentDb.Execute strInsert, dbFailOnError

将INSERT 语句更改为其中任何一个都可以使语句无错误地执行。

strInsert = "INSERT INTO tblFoo(text_field) VALUES ('O''TOOL');"
strInsert = "INSERT INTO tblFoo(text_field) VALUES (""O'TOOL"");"

您可以修改代码以使用其中一种方法。但是请考虑使用parameter query 或DAO.Recordset.AddNew 方法......然后引号和撇号将不那么麻烦。

【讨论】:

  • 决定尝试 DAO Recordset AddNew。我无法相信将数据添加到表格时有多好,非常流畅。我一直在与撇号和引号作斗争。感谢您的回复非常有帮助!
  • 不客气。我也喜欢参数查询和Recordset.AddNew 使日期更容易处理——您不必为格式问题和# 分隔符而烦恼。
【解决方案2】:

是的,它是撇号。

在连接 SQL 时,您可以使用此功能来避免此问题和大多数其他问题:

' Converts a value of any type to its string representation.
' The function can be concatenated into an SQL expression as is
' without any delimiters or leading/trailing white-space.
'
' Examples:
'   SQL = "Select * From TableTest Where [Amount]>" & CSql(12.5) & "And [DueDate]<" & CSql(Date) & ""
'   SQL -> Select * From TableTest Where [Amount]> 12.5 And [DueDate]< #2016/01/30 00:00:00#
'
'   SQL = "Insert Into TableTest ( [Street] ) Values (" & CSql(" ") & ")"
'   SQL -> Insert Into TableTest ( [Street] ) Values ( Null )
'
' Trims text variables for leading/trailing Space and secures single quotes.
' Replaces zero length strings with Null.
' Formats date/time variables as safe string expressions.
' Uses Str to format decimal values to string expressions.
' Returns Null for values that cannot be expressed with a string expression.
'
' 2016-01-30. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function CSql( _
    ByVal Value As Variant) _
    As String

    Const vbLongLong    As Integer = 20
    Const SqlNull       As String = " Null"

    Dim Sql             As String
    Dim LongLong        As Integer

    #If Win32 Then
        LongLong = vbLongLong
    #End If
    #If Win64 Then
        LongLong = VBA.vbLongLong
    #End If

    Select Case VarType(Value)
        Case vbEmpty            '    0  Empty (uninitialized).
            Sql = SqlNull
        Case vbNull             '    1  Null (no valid data).
            Sql = SqlNull
        Case vbInteger          '    2  Integer.
            Sql = Str(Value)
        Case vbLong             '    3  Long integer.
            Sql = Str(Value)
        Case vbSingle           '    4  Single-precision floating-point number.
            Sql = Str(Value)
        Case vbDouble           '    5  Double-precision floating-point number.
            Sql = Str(Value)
        Case vbCurrency         '    6  Currency.
            Sql = Str(Value)
        Case vbDate             '    7  Date.
            Sql = Format(Value, " \#yyyy\/mm\/dd hh\:nn\:ss\#")
        Case vbString           '    8  String.
            Sql = Replace(Trim(Value), "'", "''")
            If Sql = "" Then
                Sql = SqlNull
            Else
                Sql = " '" & Sql & "'"
            End If
        Case vbObject           '    9  Object.
            Sql = SqlNull
        Case vbError            '   10  Error.
            Sql = SqlNull
        Case vbBoolean          '   11  Boolean.
            Sql = Str(Abs(Value))
        Case vbVariant          '   12  Variant (used only with arrays of variants).
            Sql = SqlNull
        Case vbDataObject       '   13  A data access object.
            Sql = SqlNull
        Case vbDecimal          '   14  Decimal.
            Sql = Str(Value)
        Case vbByte             '   17  Byte.
            Sql = Str(Value)
        Case LongLong           '   20  LongLong integer (Valid on 64-bit platforms only).
            Sql = Str(Value)
        Case vbUserDefinedType  '   36  Variants that contain user-defined types.
            Sql = SqlNull
        Case vbArray            ' 8192  Array.
            Sql = SqlNull
        Case Else               '       Should not happen.
            Sql = SqlNull
    End Select

    CSql = Sql & " "

End Function

【讨论】:

  • 感谢您的回复,我将考虑将此功能添加到我的数据库中。我需要更多地阅读它才能更好地理解它。
  • 它可能看起来比实际更多。这真的很简单 - 请参阅在线示例。
【解决方案3】:

问题是由于“O'TOOL”中的 '(撇号)。尝试用双撇号 (O''TOOL) 替换它或将地址放在双引号中

【讨论】:

  • 我知道这可能是一个逗号或撇号,我很难使用引号和逗号。感谢您的回复!
猜你喜欢
  • 2012-12-13
  • 2016-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-10
  • 1970-01-01
相关资源
最近更新 更多