将报表从存储过程填充到Access前端

时间:2016-03-02 16:38:24

标签: sql-server vba access-vba ms-access-2010

在SQL Server中假设此存储过程:

CREATE PROCEDURE sp_MySP AS
  BEGIN
    SELECT a.Field1, a.Field2, v.Field3, p.Field4, a.Field5
    INTO #Temp1
    FROM tblMyTable1 AS a, tblMyTable2 AS p, tblMyTable3 AS v
    WHERE a.Field1 = p.Field1 
    AND a.Field1 = v.Field1 

    SELECT a.Field1, a.Field2, v.Field3, p.Field4, a.Field5
    INTO #Temp2
    FROM tblMyTable1 AS a, tblMyTable2 AS p, tblMyTable3 AS v
    WHERE a.Field1 = p.Field1 
    AND a.Field1 = v.Field1 

    SELECT *
    FROM #Temp1

    UNION ALL

    SELECT *
    FROM #Temp2

  END

这个vba代码(我把vba代码放在报告的LOAD事件上):

Private Sub Report_Load()
    Dim rs As ADODB.Recordset
    Dim cnComments As New ADODB.Connection
    Dim strConn As String
    strConn = "PROVIDER=SQLOLEDB;DATA SOURCE=WML202;INITIAL CATALOG=TOOL_TRACKING;UID=" & CVHx(SSTR1) & ";PWD=" & CVHx(SSTR2) & ";"
    cnComments.Open strConn

    Set rs = cnComments.Execute("EXEC sp_MySP")

    '** What do I do here?

End Sub

实际存储过程要复杂得多(并且不必要),但这是数据集的布局。它是UNION ALL的几个#Temp文件。

如何执行'** What do I do here?部分以使存储过程的结果填满报告?

1 个答案:

答案 0 :(得分:2)

您可以将表单的记录源设置为调用存储过程的传递查询(类似于如何将其设置为表或查询/视图的名称)。

然后你可以简单地使用数据感知控件。