2013-10-17 55 views
1

我試圖在Report_Load事件中將報表(或表單)的記錄集更改爲動態調用的MySQL存儲過程的結果。具體來說,我將如何設置連接?我看過Bind Access form to the results from a Stored Procedure以及How to bind Microsoft Access forms to ADO recordsets。我已經成功地使用傳遞查詢中的硬編碼值連接到存儲過程,詳見Calling Stored Procedures and other SQL statements from MS Access 2003 (Pass Through)從MySQL存儲過程綁定訪問表單/報表

下面是示例代碼從Bind Access form to the results from a Stored Procedure - 我想設置,而不是使用SQL服務器的MySQL連接:

With cn 
    .Provider = "Microsoft.Access.OLEDB.10.0" 
    .Properties("Data Provider").Value = "SQLOLEDB" 
    .Properties("Data Source").Value = "Server" 
    .Properties("Integrated Security").Value = "SSPI" 
    .Properties("Initial Catalog").Value = "Test" 
    .Open 
End With 

我使用Access 2007年。


出於興趣,這裏是我用來修改sp調用的代碼,給出一個錯誤「32585該功能只在ADP中可用」。 Gord Thompson提出修改實際查詢的建議,所以我使用了這個。

If Not CurrentProject.AllForms("foo_frm").IsLoaded Then 
     'use hard-coded query (stored procedure) for dev work 
     Exit Sub 
    End If 

    Dim action, startdate, enddate As String 
    action = Forms![foo_frm].txtAction 

    If action = "cmdDaily" Then 
     startdate = Forms![foo_frm].txtYesterday 
     enddate = Forms![foo_frm].txtToday 
    Else 
     startdate = Forms![foo_frm].cboStartDate 
     enddate = Forms![foo_frm].cboEndDate 
    End If 

    Dim cn As New ADODB.Connection 
    Dim strConnection As String 

    strConnection = "ODBC;DSN=Foo01;UID=root;PWD=Secret;DATABASE=bar" 
    With cn 
     .Provider = "MSDASQL" 
     .Properties("Data Source").Value = strConnection 
     .Open 
    End With 

    Dim prmStartDate, prmEndDate As New ADODB.Parameter 
    Dim cmd As New ADODB.Command 

    Set prmStartDate = cmd.CreateParameter("startdate", adDate, adParamInput) 
    prmStartDate.Value = CDate(startdate) 
    cmd.Parameters.Append (prmStartDate) 

    Set prmEndDate = cmd.CreateParameter("enddate", adDate, adParamInput) 
    prmEndDate.Value = CDate(enddate) 
    cmd.Parameters.Append (prmEndDate) 

    With cmd 
     .ActiveConnection = cn 
     .CommandText = "qux_sp" 
     .CommandType = adCmdStoredProc 
     Set Me.Recordset = .Execute 
    End With 
+0

僅供參考,你不能綁定的訪問報告到ADO記錄集,除非您使用的Access數據項目(稱爲ADP)是一種特殊類型的Access應用程序文件,現在在Access 2013中已被棄用。就形式而言,是的,您應該可以執行此操作。 – HK1

+0

是的,我得到了一個錯誤「32585此功能只適用於ADP」 –

回答

1

你已經擁有的報告與傳遞查詢工作,因此而不是使用代碼與報表的行源撥弄爲什麼不見好就收綁定到傳遞查詢該報告並使用代碼調整它的SQL語句並用你想要的參數調用存儲過程?

例如,下面的VBA代碼...

Sub TweakPassThroughQuery() 
Dim testID As Long 
Dim qdf As DAO.QueryDef, rst As DAO.Recordset 
testID = 2 ' this is the parameter we really want to pass to the stored procedure 
Set qdf = CurrentDb.QueryDefs("ptq_myproc") 
Debug.Print "Existing pass-through query..." 
Debug.Print " " & qdf.SQL 
Set rst = qdf.OpenRecordset 
Debug.Print "...returns a value like this:" 
Debug.Print " " & rst!col1 
rst.Close 
Set rst = Nothing 
' now tweak the stored procedure call 
qdf.SQL = "CALL myproc(" & testID & ")" 
Debug.Print "Modified pass-through query..." 
Debug.Print " " & qdf.SQL 
Set rst = qdf.OpenRecordset 
Debug.Print "...returns a value like this:" 
Debug.Print " " & rst!col1 
rst.Close 
Set rst = Nothing 
Set qdf = Nothing 
End Sub 

...顯示以下輸出:

Existing pass-through query... 
    CALL myproc(1) 
...returns a value like this: 
    foo 
Modified pass-through query... 
    CALL myproc(2) 
...returns a value like this: 
    bar 
+0

很好,謝謝!我會放棄這一點。我昨天晚上想着修改實際查詢本身的方法,不知道如何處理它。 –