【问题标题】:Bind Access Form/Report to results from MySQL Stored Procedure将访问表单/报告绑定到 MySQL 存储过程的结果
【发布时间】:2013-10-17 00:58:35
【问题描述】:

我试图在 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)

这是来自Bind Access form to the results from a Stored Procedure 的示例代码 - 我想设置连接以使用 MySQL 而不是 SQL Server:

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 记录集,除非您使用“访问数据项目”(称为 ADP),这是一种特殊类型的访问应用程序文件,现已弃用在 Access 2013 中。就表单而言,是的,您应该能够做到这一点。
  • 是的,我收到一个错误“32585 此功能仅在 ADP 中可用”

标签: mysql ms-access stored-procedures


【解决方案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

【讨论】:

  • 很好,谢谢!我会试一试的。昨晚我在考虑修改实际查询本身的方法,不知道如何处理。
猜你喜欢
  • 1970-01-01
  • 2015-07-20
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多