【问题标题】:VBA run-time error when passing dates to MySQL query将日期传递给 MySQL 查询时出现 VBA 运行时错误
【发布时间】:2015-11-04 03:06:25
【问题描述】:

我从 Excel VBA 连接到 MySQL 以获取 my_stored_procedure。工作正常,除了当我使用日期过滤器作为参数传递给 MySQL SP 时,我收到一个运行时错误。

日期过滤器由用户在开始日期和结束日期两个单元格中输入。单元格定义为范围 dateFrom 和 dateTo。

所以我使用这些范围传递给 mySql rs,但 VBA 会返回:

运行时错误'-2147217900 (80040e14)'

你的 MySQL 语法有错误;检查相应的手册 到您的 MYSQL 服务器版本... 'my_stored_procedure '2015-10-01', '2015-10-30'' 在第 1 行。

下面是我的 VBA 代码:

        Const ConnStr As String = "Driver={MySQL ODBC 5.3 ANSI Driver};Server=0.0.0.0;Database=db;User=user;Password=pw;Option=3;PORT=3306;Connect Timeout=20;"

Function MySqlCommand(strSql As String) As Variant
    Dim conMySQL
    Set conMySQL = CreateObject("ADODB.Connection")
    Set cmd = CreateObject("ADODB.Command")
    conMySQL.ConnectionString = ConnStr
    conMySQL.Open
    Set cmd.ActiveConnection = conMySQL
    cmd.CommandText = strSql
    ReturnValue = cmd.Execute
    conMySQL.Close
End Function

Function MySqlRS(strSql As String) As ADODB.Recordset
    Dim conMySQL
    Set conMySQL = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
    conMySQL.ConnectionString = ConnStr
    conMySQL.Open
    Set rs.ActiveConnection = conMySQL
    rs.Open strSql
    Set MySqlRS = rs
End Function
_____
Public Sub fetchReport()

Dim rs As ADODB.Recordset
    Set rs = MySqlRS("my_stored_procedure '" & Format(Range("dateFrom"), "yyyy-mm-dd") & "' ,'" & Format(Range("dateTo"), "yyyy-mm-dd") & "' ")
Dim hd As Integer


    If Not rs.EOF Then
            For hd = 0 To rs.Fields.Count - 1
                Worksheets("data").Cells(1, hd + 1).Value = rs.Fields(hd).Name  'insert column headers
            Next

        Sheets("data").Range("A2").CopyFromRecordset rs  'insert data
        Sheets("data").Range("A1").CurrentRegion.Name = "rsRange"  'set named range for pivot
    End If

ActiveWorkbook.RefreshAll

End Sub

这是我的 SP 语法:

CREATE DEFINER=`user`@`%` PROCEDURE `my_stored_procedure`
(
in fromDate date,
in toDate date
)
BEGIN
SELECT ...
WHERE dateColumn >= fromDate AND dateColumn <= toDate 
END

感谢任何帮助。

乔尔

【问题讨论】:

  • 这就是你的全部代码吗?或者还有更多?我正在寻找一个命令字符串和一个执行方法
  • 我刚刚用整个 vb 代码 tnx 编辑了 op!
  • 这是您处理对数据库连接字符串 > 连接 > 命令 > 记录集的查询所需的方式。您缺少创建命令传递存储过程并执行。
  • 谢谢。我会在哪里添加它和什么?只是补充一下,在我添加日期查询之前,代码工作得很好。设置 rs = MySqlRS("my_stored_procedure")
  • 我给你一些代码给我一点

标签: mysql vba excel runtime-error parameter-passing


【解决方案1】:

我希望这会有所帮助。我已经很久没有这样做了。

架构

create table myTable
(   id int auto_increment primary key,
    dateColumn date not null,
    stuff varchar(100) not null
);
insert myTable(dateColumn,stuff) values
('2014-12-21','dogs'),('2015-02-21','frogs'),('2015-07-21','snakes'),('2015-12-21','cats');

存储过程

drop procedure if exists `my_stored_procedure`;
DELIMITER $$
CREATE PROCEDURE `my_stored_procedure`
(
    in fromDate date,
    in toDate date
)
BEGIN
    SELECT * 
    from myTable
    WHERE dateColumn >= fromDate AND dateColumn <= toDate; 
END
$$
DELIMITER ;

VBA

Function MySqlStoredProcRS(strSql As String) As ADODB.Recordset
    Dim conMySQL
    Set conMySQL = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
    conMySQL.ConnectionString = ConnStr ' not shown but like yours in Op Question
    conMySQL.Open
    Set rs.ActiveConnection = conMySQL
    rs.CursorLocation = adUseClient
    rs.Open strSql, conMySQL, , , adCmdText
    Set MySqlStoredProcRS = rs
End Function
Public Sub fetchReport()

Dim rs As ADODB.Recordset
Dim sql As String
sql = "call my_stored_procedure ('" & Format(Range("dateFrom"), "yyyy-mm-dd") & "' ,'" & Format(Range("dateTo"), "yyyy-mm-dd") & "')"
Set rs = MySqlStoredProcRS(sql)
Dim hd As Integer


    If Not rs.EOF Then
            For hd = 0 To rs.Fields.Count - 1
                Worksheets("data").Cells(1, hd + 1).Value = rs.Fields(hd).Name  'insert column headers
            Next

        Sheets("data").Range("A2").CopyFromRecordset rs  'insert data
        Sheets("data").Range("A1").CurrentRegion.Name = "rsRange"  'set named range for pivot
    End If

ActiveWorkbook.RefreshAll

End Sub

输出图片(点击蓝色的东西后)

外卖

我想探索对新函数MySqlStoredProcRS 的更改,ADODB.RecordSet 的处理,使用call,以及将参数包装在调用字符串中的括号。

祝你好运。

【讨论】:

  • Tnx 。错误输出到:PROCEDURE db.my_stored_procedure 不存在。
  • 然后创建它。也许你有 db、db2、db3。我知道什么:) 也许您不知道如何创建存储过程。也许你不知道你在什么数据库。这些都是基本的东西。 Stackoverflow 是用于编程问题的,它不是一个辅导网站,尽管它经常成为一个。我只是现在不想当导师。
  • 大声笑,它确实存在,我直接通过工作台使用它。奇怪的是,当我在 SP 之前删除“调用”时,我得到一个语法不正确的不同错误,与 OP 中的错误相同。
  • 我们可以就这个馅饼达成一致,对吧? ----> paste.org/10527393
  • 没有call,你注定要失败。你还不如拍打键盘,送出那根弦。
【解决方案2】:

你好,试试这个,看看效果如何

Option Explicit
Const strCONNECTION As String = "Driver={MySQL ODBC 5.3 ANSI Driver};Server=0.0.0.0;Database=db;User=user;Password=pw;Option=3;PORT=3306;Connect Timeout=20;"


Function GetConnection(ByVal strConnString As String) As ADODB.Connection

    Dim ret As ADODB.Connection

    Set ret = New ADODB.Connection
    ret.Open strConnString

    ' Return the connection object
    Set GetConnection = ret

End Function


Function GetCommand(ByRef oConn As ADODB.Connection, ByVal strCommand As String) As ADODB.Command

    Dim ret As ADODB.Command

    Set ret = New ADODB.Command

    With ret
        Set .ActiveConnection = oConn
        .CommandText = strCommand
        .CommandType = adCmdText
    End With

    ' Return the command
    Set GetCommand = ret

End Function

Function GetRecordSet(ByRef oCommand As ADODB.Command) As ADODB.Recordset
    Set GetRecordSet = oCommand.Execute
End Function


Public Sub fetchReport()

    Dim oCon As ADODB.Connection
    Dim oCom As ADODB.Command
    Dim oRecordSet As ADODB.Recordset
    Dim strCommand As String
    Dim hd As Integer

    ' Create and load the objects
    strCommand = "exec my_stored_procedure '" & Format(Range("dateFrom"), "yyyy-mm-dd") & "' ,'" & Format(Range("dateTo"), "yyyy-mm-dd") & "' "

    ' Connection string > Connection > Command > RecordSet
    Set oCon = GetConnection(strCONNECTION)
    Set oCom = GetCommand(oCon, strCommand)
    Set oRecordSet = GetRecordSet(oCom)


    If Not oRecordSet.EOF Then
        For hd = 0 To oRecordSet.Fields.Count - 1
            Worksheets("data").Cells(1, hd + 1).Value = oRecordSet.Fields(hd).Name      'insert column headers
        Next

        Sheets("data").Range("A2").CopyFromRecordset oRecordSet  'insert data
        Sheets("data").Range("A1").CurrentRegion.Name = "rsRange"  'set named range for pivot
    End If

    ActiveWorkbook.RefreshAll

End Sub

我希望这会有所帮助:)

【讨论】:

  • 谢谢弗雷德。以下编译错误,“变量未定义”在行 Set oCon = GetConnection(strCONNECTION)
  • 你复制了所有的代码吗?我无法复制;它对我来说工作正常。
  • 你说得对,我错过了第二行。现在我在 strCommand = "SP_SalesReport_flat '" & Format(Range("dateFrom"), "yyyy-mm-dd") & "' ,'" & Format(Range("dateTo" ), "yyyy-mm-dd") & "'"
  • 确保名称范围存在并使用末尾范围的值 Range("dateFrom").value 也根据其他帖子将“Call”放在存储过程名称的前面
  • 不知道为什么,但是经过建议的修改后,无论使用“调用”和 .Value,我都会遇到与 OP 相同的错误:您在 MySQL 语法中有错误; ... 'my_stored_procedure '2015-10-01', '2015-10-30'' 在第 1 行。也许我需要启用模块/加载项??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多