0

以下代码执行存储过程传递查询。从表单接收参数并传递给存储过程。该错误表明它是无效的 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
4

1 回答 1

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
于 2012-07-16T18:42:18.400 回答