我正在尝试从“输入”文件中获取百分比列(第3列),并使用该列对价格进行折价并将该价格逐个放入第7列的输出文件中。
我将SQL连接传递给一个函数,一个UPC值在数据库中查找信息,然后将百分比值(一个Double)作为输入。
但是,我不断收到错误“关键字%附近的语法不正确”。我试图重命名百分比变量,也使用Val()函数(如您在代码中看到的那样)。
如果我取出Val(),则会收到类型不匹配错误
基本上:我无法弄清楚如何在查询中使用“ .10”之类的“百分比”输入。
For i = 1 To 381
wrkb.Worksheets("Output").Cells(i + 1, 2).CopyFromRecordset extractInfo(cnn, wrkb.Worksheets("Input").Cells(i, 2).Value, Val(wrkb.Worksheets("Input").Cells(i, 3).Value))
Next i
Function extractInfo(cnn As ADODB.Connection, upc As String, percent As Double) As ADODB.Recordset
'Initializes variables
Dim rst As New ADODB.Recordset
Dim StrQuery As String
'The query to run, feed the UPC value you acquired to then get all the other variables
StrQuery = "SELECT 'N' as Division, zzeupcnr.style as Style, color_code as Color, ' ' as label_code, dimension as Dimension, ROUND((a_price * (1.00 - percent)), 2), ret_price " & _
"FROM zzeupcnr JOIN zzxstylr " & _
"ON zzeupcnr.style = zzxstylr.style " & _
"WHERE upc = '" & upc & "'"
'Performs the actual query
rst.Open StrQuery, cnn
Set extractInfo = rst 'Stores result
答案 0 :(得分:1)
您在查询字符串中使用percent
作为字符串。
如果要使用变量值,则需要执行以下操作:
StrQuery = "SELECT 'N' as Division, zzeupcnr.style as Style, color_code as Color, ' ' as label_code, dimension as Dimension, ROUND((a_price * (1.00 - " & percent & ")), 2), ret_price " & _
"FROM zzeupcnr JOIN zzxstylr " & _
"ON zzeupcnr.style = zzxstylr.style " & _
"WHERE upc = '" & upc & "'"
请注意" & percent & "