多个MySQL查询到不同的单元格

时间:2015-08-13 18:40:54

标签: mysql excel vba excel-vba database-connection

我尝试开始工作的是一个Sub中不同的MySQL查询,它应该填充一个表中的不同范围。

这是我到目前为止所得到的:

使用功能“ConnectionDB”我想连接到远程数据库。在Sub“QueryDatabase”中,我打开连接并执行两个Sql查询。第一个Sql查询应填充范围A2,第二个查询应填充同一工作表中的范围D2。

我将记录的宏中的Sql字符串从远程数据库中提取出来,并且在记录的宏中它运行良好。

当我执行我的代码(见下文)时,Excel正在运行一段时间,但已解决的单元格保持空白。我没有调试错误。

所以,伙计们,这里是我为能得到这项工作而得到的每一项帮助感到高兴的事情=)这是否有可能成为现在想要的方式?

Public conMySQL As ADODB.Connection
Public rs As ADODB.Recordset
Public strSql As String

Public Function ConnectionDB()

Set conMySQL = New ADODB.Connection
Set rs = New ADODB.Recordset

conMySQL.Open 
"Driver={MySQL ODBC 5.3 UNICODE Driver}; _
    Server={XXX}; UID=XXX; PWD=XXX; DATABASE=XXX; _
    Option=3; PORT=3306"
End Function


Sub QueryDatabase()

  Call ConnectionDB 

    With Sheets("Energiedaten").Range("A2") 

        strSql = "SELECT Messdaten_0.Terminal, Messdaten_0.Timestamp, Messdaten_0.Value" & Chr(13) & "" & Chr(10) & "FROM BLB_Data.Messdaten Messdaten_0" & Chr(13) & "" & Chr(10) & _
        "WHERE (Messdaten_0.Terminal = 'TerminalX') AND (Messdaten_0.Timestamp>=('" & UserForm1.TextBox1.Text & "') And (Messdaten_0.Timestamp<=('" & UserForm1.TextBox2.Text & "'))" _
        & Chr(13) & "" & Chr(10) & "ORDER BY Messdaten_0.Timestamp" 

    End With

    With Sheets("Energiedaten").Range("D2")

        strSql = "SELECT Messdaten_0.Terminal, Messdaten_0.Timestamp, Messdaten_0.Value" & Chr(13) & "" & Chr(10) & "FROM BLB_Data.Messdaten Messdaten_0" & Chr(13) & "" & Chr(10) & _
        "WHERE (Messdaten_0.Terminal = 'TerminalY') AND (Messdaten_0.Timestamp>=('" & UserForm1.TextBox1.Text & "') And Messdaten_0.Timestamp<=('" & UserForm1.TextBox2.Text & "'))" _
        & Chr(13) & "" & Chr(10) & "ORDER BY Messdaten_0.Timestamp " 

    End With

conMySQL.execute strSql

conMySQL.Close

End Sub

2 个答案:

答案 0 :(得分:0)

您可以使用Range.CopyFromRecordset方法。您已经声明了rs As ADODB.Recordset,因此您需要打开记录集,将其写入工作表,然后为每个SQL语句关闭它。

编辑:用完整代码替换代码的示例部分:

Option Explicit

Public conMySQL As ADODB.Connection

Public Function ConnectionDB()

    Set conMySQL = New ADODB.Connection

    conMySQL.Open "Driver={MySQL ODBC 5.3 UNICODE Driver};" _
        & "Server={XXX}; UID=XXX; PWD=XXX; DATABASE=XXX;" _
        & "Option=3; PORT=3306"

End Function

Sub QueryDatabase()

Dim rs As ADODB.Recordset
Dim strSql As String

    Set rs = New ADODB.Recordset

    Call ConnectionDB

    strSql = "SELECT `Terminal`, `Timestamp`, `Value` FROM Messdaten WHERE `Terminal` = 'Extruder' AND `Timestamp` >= '" & UserForm1.TextBox1.Text & "' AND `Timestamp` <= '" & UserForm1.TextBox1.Text & "' ORDER BY `Timestamp` ASC"

    rs.Open Source:=strSql, ActiveConnection:=conMySQL, CursorType:=adOpenStatic, _
        LockType:=adLockOptimistic
    ThisWorkbook.Sheets("Energiedaten").Range("A2").CopyFromRecordset rs
    rs.Close

    strSql = "SELECT `Terminal`, `Timestamp`, `Value` FROM Messdaten WHERE `Terminal` = 'Beschichtungseinheit' AND `Timestamp` >= '" & UserForm1.TextBox1.Text & "' AND `Timestamp` <= '" & UserForm1.TextBox1.Text & "' ORDER BY `Timestamp` ASC "

    rs.Open Source:=strSql, ActiveConnection:=conMySQL, CursorType:=adOpenStatic, _
        LockType:=adLockOptimistic
    ThisWorkbook.Sheets("Energiedaten").Range("D2").CopyFromRecordset rs
    rs.Close

    conMySQL.Close

End Sub

答案 1 :(得分:0)

类似的东西:

Option Explicit

Public conMySQL As ADODB.Connection

Public Sub ConnectionDB()

    Set conMySQL = New ADODB.Connection

    conMySQL.Open "Driver={MySQL ODBC 5.3 UNICODE Driver};" & _
                  "Server={XXX}; UID=XXX; PWD=XXX; DATABASE=XXX;" & _
                  "Option=3; PORT=3306"
End Sub

Sub QueryDatabase()

   ConnectionDB

    strSql = "SELECT Messdaten_0.Terminal, Messdaten_0.Timestamp, Messdaten_0.Value" & Chr(13) & "" & Chr(10) & "FROM BLB_Data.Messdaten Messdaten_0" & Chr(13) & "" & Chr(10) & _
    "WHERE (Messdaten_0.Terminal = 'TerminalX') AND (Messdaten_0.Timestamp>=('" & UserForm1.TextBox1.Text & "') And (Messdaten_0.Timestamp<=('" & UserForm1.TextBox2.Text & "'))" _
    & Chr(13) & "" & Chr(10) & "ORDER BY Messdaten_0.Timestamp"

    PutResults Sheets("Energiedaten").Range("A2"), strSql


    strSql = "SELECT Messdaten_0.Terminal, Messdaten_0.Timestamp, Messdaten_0.Value" & Chr(13) & "" & Chr(10) & "FROM BLB_Data.Messdaten Messdaten_0" & Chr(13) & "" & Chr(10) & _
        "WHERE (Messdaten_0.Terminal = 'TerminalY') AND (Messdaten_0.Timestamp>=('" & UserForm1.TextBox1.Text & "') And Messdaten_0.Timestamp<=('" & UserForm1.TextBox2.Text & "'))" _
        & Chr(13) & "" & Chr(10) & "ORDER BY Messdaten_0.Timestamp "

    PutResults Sheets("Energiedaten").Range("D2"), strSql


    conMySQL.Close

End Sub

Sub PutResults(rng As Range, SQL As String)
    Dim rs As ADODB.Recordset
    Set rs = conMySQL.Execute(SQL)
    If Not rs.EOF Then
        rng.Cells(1).CopyFromRecordset rs
    End If
    rs.Close
    Set rs = Nothing
End Sub