我試圖在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
僅供參考,你不能綁定的訪問報告到ADO記錄集,除非您使用的Access數據項目(稱爲ADP)是一種特殊類型的Access應用程序文件,現在在Access 2013中已被棄用。就形式而言,是的,您應該可以執行此操作。 – HK1
是的,我得到了一個錯誤「32585此功能只適用於ADP」 –