【问题标题】:MS Access 2007 update number with commas fails带逗号的 MS Access 2007 更新号失败
【发布时间】:2016-08-02 18:47:58
【问题描述】:

为什么我不能执行这个简单的更新查询:

SQL = "UPDATE Table SET field=0,11 WHERE id=12456"
db.Execute SQL, dbSeeChanges

如果我将字段值设置为 0.11(带小数点),则更新查询执行成功。

我的 Access 表字段数据类型是数字。

这是我得到的错误:

“3144 - UPDATE 语句中的语法错误。”

【问题讨论】:

标签: sql ms-access


【解决方案1】:

使用点(“.”)作为小数分隔符而不是逗号。

SQL = "UPDATE Table SET field=0.11 WHERE id=12456"

如果您正在构建 SQL 命令,请使用Str$ 将数字转换为字符串。它始终使用. 作为小数分隔符,并且不依赖于区域设置。另一方面,Format$ 函数使用在 Windows 的区域设置中定义的小数分隔符(可能是逗号)。

SQL = "UPDATE Table SET field=" & Str$(x) & " WHERE id=" & id

【讨论】:

    【解决方案2】:

    你需要写一个而不是逗号,像这样:

    SQL = "UPDATE Table SET field=0.11 WHERE id=12456"
    db.Execute SQL, dbSeeChanges
    

    【讨论】:

    • 好的,但是如果我的值来自其他来源,例如表。那么值仍然是0,11。我应该用逗号替换吗?
    • 源列的数据类型是什么?
    • @wiz6 这是一个语法问题。当您在 VBA 中编写 SQL 语句时,您必须使用 dot 作为小数分隔符。你不能写0,11 - 你必须写0.11
    【解决方案3】:

    在连接 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 2016-12-18
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多