[英]VB SQL error; Syntax error (comma) in query expression
I have a VBA function returning a decimal value based on a WHERE clause.我有一个 VBA function 返回基于 WHERE 子句的十进制值。
SELECT * FROM [tbl_break] WHERE [From] < 6,25 AND [To] >= 6,25
I am getting the error... Syntax error (comma) in query expression '[From] < 6,25 AND [To] >= 6,25'.我收到错误...查询表达式“[From] < 6,25 AND [To] >= 6,25”中的语法错误(逗号)。
The Tabel look like this...
From To Break
0,00 4,00 0,25
4,00 8,00 0,50
8,00 12,00 0,75
How can I deal with decimals in query statements?如何处理查询语句中的小数?
Public Function CalculateBreak(Units) As Double
On Error GoTo ErrorHandler
Dim connection As Object, result As Object
Dim sql As String
sql = "SELECT * FROM [tbl_break] WHERE [From] < " & Units & " AND [To] >= " & Units
Debug.Print sql
' Connect to the current datasource of the Excel file
Set connection = CreateObject("ADODB.Connection")
With connection
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & ThisWorkbook.Path & "\" & ThisWorkbook.Name & ";" & _
"Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
.Open
End With
' Run the SQL query
Set result = connection.Execute(sql)
CalculateBreak = result(2)
Debug.Print result(2)
Exit Function
ErrorHandler:
CalculateBreak = 0
Debug.Print Err.Description
End Function
The solution works well with integers but I depend on decimal numbers.该解决方案适用于整数,但我依赖于小数。
Use Str
to force a string expression of the decimal value with a dot as the separator which is was SQL expects:使用
Str
强制使用点作为分隔符的十进制值的字符串表达式,这是 SQL 期望的:
sql = "SELECT * FROM [tbl_break] WHERE [From] < " & Str(Units) & " AND [To] >= " & Str(Units) & ""
Result:结果:
SELECT * FROM [tbl_break] WHERE [From] < 6.25 AND [To] >= 6.25
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.