我是这个论坛的新手,对 VBA 和 SQL 也很陌生,所以我不知道这个问题是否很好地描述了这个问题。我已经解决了几天的问题,但我无法找到解决方案。我想从 excel 的单元格中设置客户代码“k_kod”,但它不起作用。它什么也不返回。如果我直接在 SQL 语句中设置“k_kod = '275'”,它就可以正常工作。它也适用于例如“vg_kod”或“pa_datum”上的参数(?)。我还尝试了一个更简单的代码字符串,然后我设法让它与“k_kod”上的参数输入一起工作。但在那段代码中,它没有嵌套的 WHERE 子句。
是因为“k_kod”在两个“WHERE 子句”中吗?
这是代码:
Sub grunddata_salj()
Sheets("Summary").Select
Dim Conn1_res As ADODB.Connection
Dim Cmd1_res As ADODB.Command
Dim Param1_res As ADODB.Parameter
Dim Param2_res As ADODB.Parameter
Dim Rs1_res As ADODB.Recordset
On Error Resume Next
Set Conn1_res = New ADODB.Connection
Conn1_res.ConnectionString = "DSN=xxx AB;uid=;pwd=;"
Conn1_res.Open
Set Rs1_res = New ADODB.Recordset
Set Cmd1_res = New ADODB.Command
Cmd1_res.ActiveConnection = Conn1_res
Cmd1_res.CommandText = "SELECT art_artnr, pa_datum, pa_pris " & _
"FROM m.prisandring as A, m.artikel, m.kundart, m.kund " & _
"WHERE pa_datum = " & _
"( " & _
"SELECT max(pa_datum), FROM m.kund, m.prisandring AS B " & _
"WHERE a.pa_id = b.pa_id " & _
"AND pa_datum < '2012-01-01' " & _
"AND pa_kl_id = kund.k_id " & _
"AND k_kod = ? " & _
") " & _
"AND artikel.art_id = pa_id " & _
"AND pa_prislista = '-3' " & _
"AND kundart.art_id = artikel.art_id " & _
"AND kundart.k_id = kund.k_id " & _
"AND pa_kl_id = kund.k_id " & _
"AND vg_kod <> '1' " & _
"AND k_kod = ? " & _
"GROUP BY art_artnr, pa_datum, pa_pris " & _
"ORDER BY art_artnr"
Set Param1_res = Cmd1_res.CreateParameter(, adInteger, adParamInput)
Param1_res.Value = "275"
Cmd1_res.Parameters.Append Param1_res
Set Param2_res = Cmd1_res.CreateParameter(, adInteger, adParamInput)
Param2_res.Value = "275"
Cmd1_res.Parameters.Append Param2_res
Set Rs1_res = Cmd1_res.Execute()
artno = Rs1_res("art_artnr")
p_date = Rs1_res("pa_datum")
p_price = Rs1_res("pa_pris")
Conn1_res.Close
Rs1_res.Close
End Sub
我将非常感谢您对此提供的一些帮助。