爲什麼我不能執行這個簡單的更新查詢:MS Access 2007中使用逗號更新次數失敗
SQL = "UPDATE Table SET field=0,11 WHERE id=12456"
db.Execute SQL, dbSeeChanges
如果我的字段值設置爲0.11(帶小數點),更新查詢成功執行。
我的Access表字段數據類型是Number。
這裏是我的錯誤: 「3144 - 在UPDATE語句的語法錯誤」
爲什麼我不能執行這個簡單的更新查詢:MS Access 2007中使用逗號更新次數失敗
SQL = "UPDATE Table SET field=0,11 WHERE id=12456"
db.Execute SQL, dbSeeChanges
如果我的字段值設置爲0.11(帶小數點),更新查詢成功執行。
我的Access表字段數據類型是Number。
這裏是我的錯誤: 「3144 - 在UPDATE語句的語法錯誤」
你需要寫一個點不是逗號的,就像這樣:
SQL = "UPDATE Table SET field=0.11 WHERE id=12456"
db.Execute SQL, dbSeeChanges
好的,但是如果我的值來自另一個源如表格。那麼值仍然是0,11。我應該用點逗號來點替換嗎? – wiz6
源列的數據類型是什麼? –
@ wiz6這是一個語法問題。當您在VBA中編寫SQL語句時,您必須**使用**點**作爲小數點分隔符。你不能寫'0,11' - 你必須寫'0.11' –
使用點(「」)作爲小數點分隔符,而不是逗號。
SQL = "UPDATE Table SET field=0.11 WHERE id=12456"
如果你正在構建的SQL命令,爲了將數字轉換爲字符串使用Str$
。它始終使用.
作爲小數點分隔符,並且不依賴區域設置。另一方面,Format$
函數使用Windows區域設置(可能是逗號)中定義的小數點分隔符。
SQL = "UPDATE Table SET field=" & Str$(x) & " WHERE id=" & id
謝謝,簡單又容易! – wiz6
您可以使用此功能串聯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
您可能希望看到這樣一個問題:http://stackoverflow.com/questions/11565335/ms -access-database-with-number-fields-in-a-language- – Bobort