【问题标题】:How can I get a mysql stored function result in VBA code如何在 VBA 代码中获取 mysql 存储的函数结果
【发布时间】:2018-03-02 16:59:28
【问题描述】:

我的 mysql 数据库中有一个函数。 我试图在 VBA 函数中调用我的 VBA 中的 mysql 函数,以通过从表单传递参数来获取结果。 我不知道该怎么做。 这就是我目前所拥有的

function getval(Id As Long) As String
Dim strSQL As String
Dim rsTch As DAO.Recordset

strSQL = "SELECT getname(" & Id & ")"
Set rsTch = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)

End Function

但是我收到了这个错误

未定义函数 getname()

getname() 是我的 mysql 函数。不过,当我在 mysql 工作台中运行查询时,我得到了结果。

getval() 是我执行查询的 VBA 函数 id是mysql函数的参数 请帮帮我

【问题讨论】:

标签: mysql vba ms-access-2013


【解决方案1】:

MDAC-Engine(Jet) 在将 SQL 发送到 MySQL 之前对其进行解析。引擎看到函数 getname() 并在 vba 代码中查找它,但它不存在,因此引发错误 Undefined function getname()

为防止这种情况,您必须使用直通查询绕过 MDAC 解析,其中 sql 代码按原样发送到 MySQL。

有两种选择。

  1. 按照 Tim 的建议,使用 ADO 而不是 DAO。这是专业人士使用的推荐选项(我仍然主要使用 DAO,但 ADO 在我的技能列表中名列前茅)。

  2. 或留在 DAO 上并创建一个 Query-Def,如 sql-server-passthrough-query-as-basis-for-a-dao-recordset-in-access 中所述,来自 戈德·汤普森。

Function getval(Id As Long) As String

Dim strSQL As String
Dim qdf As DAO.QueryDef, rst As DAO.Recordset

strSQL = "SELECT getname(" & Id & ")"


Set qdf = CurrentDb.CreateQueryDef("")
With qdf
    .Connect = "ODBC;Driver={MySQL ODBC 5.3 ANSI Driver};" _ 
                        & "SERVER=Your Server;PORT=3306;DATABASE=Your Database;" _ 
                        & "USER=Your User;PASSWORD=1234;OPTION=3"
    .SQL = strSQL
    .ReturnsRecords = True
    Set rst = .OpenRecordset
End With
Debug.Print rst(0)
rst.Close
Set rst = Nothing
Set qdf = Nothing
End Function

【讨论】:

    猜你喜欢
    • 2010-10-24
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    相关资源
    最近更新 更多