sql 的 vba 代码中 '' 附近的语法不正确
Incorrect syntax near '' in vba code for sql
我收到此错误是因为尝试将 Itemnr2 变量作为单元格值。如果我像在 ItemNr1 中那样使用普通值,则一切正常。但我需要使用单元格中的值,因为它会不时更改,所以不想每次都更改代码。如何将单元格中的值放入 SQL 代码以使其正确?
当我在所有值为数字的其他列中使用它时,它正在工作。在我需要的这一列中,有像 b2b_pkl 这样的值,因此我在将 nvarchar 值转换为数据类型 int 错误时得到了那些 Conversion failed。
PA = get_market_setting(market, "PA")
POS = get_market_setting(market, "POS")
NetAmount = get_market_setting(market, "Net Amount")
CostAmount = get_market_setting(market, "Cost Amount")
qty = get_market_setting(market, "Quantity")
transNo = get_market_setting(market, "Transaction No.")
ItemNo = get_market_setting(market, "Item No.")
CustNo = "[Customer No_]"
ItemNr1 = "('80502842')"
Itemnr2 = ThisWorkbook.Sheets("Data").Range("C1").Value
ECPOS_arr = "(" & get_market_setting("LT", "ECPOS_Arr") & ")"
' SQL query string
myquery = "SELECT -SUM(" & NetAmount & ") AS [Result] FROM " & table_TSE & _
" WHERE " & ItemNo & " IN " & Itemnr2 & _
" AND [Date] BETWEEN '" & date_from & "' AND '" & date_to & "'"
' Set Recordset to query set database on SQL string
Set sourceRs = sourceDb.OpenRecordset(myquery)
'Assign Data to variable from RecordSet'
sourceRs.MoveFirst
NetAmount = sourceRs![Result]
sourceRs.Close
sourceDb.CloseRecordset
'Populate HFB Report file'
enter image description here
如果 ItemNo 和 Itemnr2 始终有括号且没有单引号,则应将其视为字符串:
Itemnr2 = ThisWorkbook.Sheets("Data").Range("C1").Value
myquery = "SELECT -SUM(" & NetAmount & ") AS [Result] FROM " & table_TSE & _
" WHERE '" & ItemNo & "' = '" & Itemnr2 & _
"' AND [Date] BETWEEN '" & date_from & "' AND '" & date_to & "'"
我收到此错误是因为尝试将 Itemnr2 变量作为单元格值。如果我像在 ItemNr1 中那样使用普通值,则一切正常。但我需要使用单元格中的值,因为它会不时更改,所以不想每次都更改代码。如何将单元格中的值放入 SQL 代码以使其正确?
当我在所有值为数字的其他列中使用它时,它正在工作。在我需要的这一列中,有像 b2b_pkl 这样的值,因此我在将 nvarchar 值转换为数据类型 int 错误时得到了那些 Conversion failed。
PA = get_market_setting(market, "PA")
POS = get_market_setting(market, "POS")
NetAmount = get_market_setting(market, "Net Amount")
CostAmount = get_market_setting(market, "Cost Amount")
qty = get_market_setting(market, "Quantity")
transNo = get_market_setting(market, "Transaction No.")
ItemNo = get_market_setting(market, "Item No.")
CustNo = "[Customer No_]"
ItemNr1 = "('80502842')"
Itemnr2 = ThisWorkbook.Sheets("Data").Range("C1").Value
ECPOS_arr = "(" & get_market_setting("LT", "ECPOS_Arr") & ")"
' SQL query string
myquery = "SELECT -SUM(" & NetAmount & ") AS [Result] FROM " & table_TSE & _
" WHERE " & ItemNo & " IN " & Itemnr2 & _
" AND [Date] BETWEEN '" & date_from & "' AND '" & date_to & "'"
' Set Recordset to query set database on SQL string
Set sourceRs = sourceDb.OpenRecordset(myquery)
'Assign Data to variable from RecordSet'
sourceRs.MoveFirst
NetAmount = sourceRs![Result]
sourceRs.Close
sourceDb.CloseRecordset
'Populate HFB Report file'
enter image description here
如果 ItemNo 和 Itemnr2 始终有括号且没有单引号,则应将其视为字符串:
Itemnr2 = ThisWorkbook.Sheets("Data").Range("C1").Value
myquery = "SELECT -SUM(" & NetAmount & ") AS [Result] FROM " & table_TSE & _
" WHERE '" & ItemNo & "' = '" & Itemnr2 & _
"' AND [Date] BETWEEN '" & date_from & "' AND '" & date_to & "'"