用于从Mac Excel 2011中的Mysql DB获取数据的VBA代码
作者:互联网
我在Windows中使用ADODB代码从Mysql数据库中获取数据,它工作正常.但是,我似乎无法使用我的Excel工作簿(使用ADODB)来使用Excel Mac 2011.经过大量的Google搜索后,我发现了来自Actual tech的ODBC连接器,我可以使用Microsoft Query获取三行.但我希望使用VBA代码完成此操作,但无法执行此操作.有没有人得到这个工作?如果是,请您提供示例代码.提前致谢 !!
P.S:我知道存在类似的问题,但答案中提供的链接已不再有效.因此,我问了一个新问题
此外,如果有人需要通过Microsoft Query进行链接,这里是链接:
http://www.agentjim.com/MVP/Excel/2011Relational7Queries.html
这是我在Windows中使用的代码:
Sub getMysqlDBdata()
Dim Cn As Object
Dim sqlQa as string
dim temparray1 as variant
Source = "MySQL"
mysql_driver = "MySQL ODBC 5.2 ANSI Driver"
sqlQa = "select * from test.TestTable;"
Set Cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Cn.Open "Driver={" & "MySQL ODBC 5.2 ANSI Driver" & "};Server=" & "127.0.01" & ";Database= test;UID=" & "root" & ";PWD=" & "12345"
rs.Open sqlQa, Cn, adOpenStatic
temparray1 = rs.GetRows()
rs.Close
Set rs = Nothing
End Sub
解决方法:
经过大量的谷歌搜索后,我遇到了MSDN中Bryan Duchesne提供的示例代码:
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
标签:excel-vba-mac,mysql,vba,excel-vba,osx-mountain-lion 来源: https://codeday.me/bug/20190831/1772048.html