sql >> Base de Datos >  >> RDS >> Mysql

Código VBA para obtener datos de Mysql DB en Mac Excel 2011

Después de buscar mucho en Google, encontré este código de muestra proporcionado por Bryan Duchesne en MSDN:

Sub TestSqlConnection()
Dim sqlstring As String
Dim connstring As String
Dim sLogin As String
sLogon = "Uid=myUserID;Pwd=myPassowrkd;"
sqlstring = "select * from zitemloc"
connstring = "ODBC;DSN=myDSN;" & sLogon
ActiveSheet.Range("B1:t2000").Clear

Dim qt As QueryTable
For Each qt In ActiveSheet.QueryTables
    qt.Delete
Next qt
With ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("B1"),   Sql:=sqlstring)
    .BackgroundQuery = False
    .Refresh
End With

Set qt = ActiveSheet.QueryTables(1)
Dim rowCount As Integer
rowCount = UBound(qt.ResultRange.Value)


Dim ix, iy As Integer
Dim data As Variant
Dim colCount As Integer
colCount = qt.ResultRange.Columns.Count

For ix = 1 To rowCount
    If Not IsArray(data) Then
            ReDim data(rowCount - 1, colCount - 1)
    End If
    For iy = 1 To qt.ResultRange.Columns.Count
        data(ix - 1, iy - 1) = qt.ResultRange.Value2(ix, iy)

    Next
Next
End Sub