传递参数查询定义

时间:2012-07-16 18:38:52

标签: vba access-vba

以下代码执行存储过程传递查询。从表单接收参数并将其传递给存储过程。该错误表明它是一个无效的SQL语句。我需要知道代码是否正确以及如何连接到数据库。结果以记录集的形式返回。谢谢!

Private Sub Command10_Click()
Dim rs1 As DAO.Recordset
Dim DB As Database
Dim Q As QueryDef
Dim strSQL As String

Set DB = CurrentDb()
Set Q = DB.QueryDefs("Call_SP")

strSQL = "execute dbo.ix_spc_planogram_match " & [Forms]![start]![Selection]![cat_code]
Q.ReturnsRecords = True

Q.SQL = strSQL

Set rs1 = DB.OpenRecordset(strSQL)

Do While Not rs1.EOF

Debug.Print rs1.Fields.Item("POG_DBKEY").Value = "POG_DBKEY"
Debug.Print rs1.Fields.Item("COMP_POG_DBKEY").Value = "COMP_POG_DBKEY"
Debug.Print rs1.Fields.Item("CURR_SKU_CNT").Value = "CURR_SKU_CNT"
Debug.Print rs1.Fields.Item("COMP_SKU_CNT").Value = "COMP_SKU_CNT"
Debug.Print rs1.Fields.Item("SKU_TOTAL").Value = "SKU_TOTAL"
Debug.Print rs1.Fields.Item("MATCHD").Value = "MATCHD"

rs1.MoveNext
Loop
rs1.Close
Set rs1 = Nothing
Set rs1 = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

CurrentDB是MS Access,但您正在执行SQL Server存储过程。您需要针对与服务器的连接执行。

例如:

Dim objcon As New ADODB.Connection

scn = "Provider=sqloledb;Data Source=ServerName;" _
    & "Initial Catalog=DBNAME;User Id=USERNAME;Password=Password;"

objcon.Open scn

Set rs = objcon.Execute