VBA code to fetch data from Mysql DB in Mac Excel 2011

uni9e

New Member
Joined
Mar 16, 2014
Messages
3
I was using ADODB code in Windows to fetch data from Mysql Database and it was working fine. However, I can not seem to get my Excel workbook (using ADODB) to work with Excel Mac 2011. After a lot of googling, I found an ODBC connector from Actual tech and I was able to fetch three rows using Microsoft Query. But I want this to be done using VBA code but have been unable to do so. Has anyone gotten this to work? If yes, can you please provide me with a sample code. Thanks in Advance !!

P.S: I know there is a similar existing question but the link provided in the answer is no longer working. hence, I have asked a new question

Also, If anyone needs the link to do it through Microsoft Query, here is the link:Generate a report from a database


Here is the code I use in Windows:


<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit;"> 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 homeunion.propertydata;"
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</code>
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
As long as your references exist to ADO etc I use

Code:
Sub Master()
    Dim adoConn As New ADODB.Connection
    Dim adoRs As New ADODB.Recordset
    Dim sConn As String
    Dim sSql As String
    Dim sOutput As String
    Dim Var1 as String
    Dim rnStart As Range
    
    Set rnStart = Sheets("Reference Lookup").Range("A24")
    
    sConn = "Provider=SQLOLEDB; Data Source =***** ; Initial Catalog = ******; User Id = *******; Password=*******"

I use http://www.dpriver.com/pp/sqlformat.htm and output as VB/ASP as the output to wrap the entire string

    sSql = Var1

    Set adoConn = New ADODB.Connection
    adoConn.Open sConn

    Set adoRs = New ADODB.Recordset
    adoRs.Open Source:=sSql, _
               ActiveConnection:=adoConn

    On Error Resume Next
    If Not (adoRs.BOF Or adoRs.EOF) Then
        Do While Not adoRs.EOF

            rnStart.CopyFromRecordset adoRs    'rst

            adoRs.MoveNext  'When you code has been running for 1/2 an hour, you
            'probably forgot this line.  I know I forget it all the time.
        Loop

        'Close stuff


        'Get rid of the last comma
        sOutput = Left(sOutput, Len(sOutput) - 1)
    Else
        sOutput = "No Matched Data"
        Sheets("Reference Lookup").Range("A24") = sOutput
    End If

    'Output

    adoRs.Close
    adoConn.Close

    Set adoRs = Nothing
    Set adoConn = Nothing
    
End Sub
 
Upvote 0
Hi,

Thanks for your reply but ADODB objects(ActiveX controls to be more precise) are not supported in MAC. I came across this link which has a sample code that can be used in mac:
QueryTable object not the same as on PC - Microsoft Community


As long as your references exist to ADO etc I use

Code:
Sub Master()
    Dim adoConn As New ADODB.Connection
    Dim adoRs As New ADODB.Recordset
    Dim sConn As String
    Dim sSql As String
    Dim sOutput As String
    Dim Var1 as String
    Dim rnStart As Range
    
    Set rnStart = Sheets("Reference Lookup").Range("A24")
    
    sConn = "Provider=SQLOLEDB; Data Source =***** ; Initial Catalog = ******; User Id = *******; Password=*******"

I use http://www.dpriver.com/pp/sqlformat.htm and output as VB/ASP as the output to wrap the entire string

    sSql = Var1

    Set adoConn = New ADODB.Connection
    adoConn.Open sConn

    Set adoRs = New ADODB.Recordset
    adoRs.Open Source:=sSql, _
               ActiveConnection:=adoConn

    On Error Resume Next
    If Not (adoRs.BOF Or adoRs.EOF) Then
        Do While Not adoRs.EOF

            rnStart.CopyFromRecordset adoRs    'rst

            adoRs.MoveNext  'When you code has been running for 1/2 an hour, you
            'probably forgot this line.  I know I forget it all the time.
        Loop

        'Close stuff


        'Get rid of the last comma
        sOutput = Left(sOutput, Len(sOutput) - 1)
    Else
        sOutput = "No Matched Data"
        Sheets("Reference Lookup").Range("A24") = sOutput
    End If

    'Output

    adoRs.Close
    adoConn.Close

    Set adoRs = Nothing
    Set adoConn = Nothing
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,645
Messages
6,120,711
Members
448,984
Latest member
foxpro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top