Share via

Setting up ODBC Connection - Switching from Microsoft to Mac - Need help updating the macros please

Anonymous
2011-11-08T21:13:47+00:00

I have a product which is an Excel front-end with a Access back end.  I need to convert this to Mac, as we have a client who purchased it and they only run Macs.  I purchased the ODBC driver from Actual Tech, but cannot get my macros to work. All I am trying to do is to select data based on certain values within the workbook and put it into another worksheet within the same macro.  Below is partial code that I used in Windows.  Below that is what I tried to do on the Mac side.  Any help would be greatly appreciated!  Thanks.

*******Windows macro************

Sub UpdateReport()

Dim selDemo, selCat, selSC, selRB, selChan, selB1, selB2, selB3, selB4, durk, selB5 As String

Dim Cn, cnP1, cnP2, cnP3, cnP4, cnP5, cnP6, cnP7, cnP8, cnP9

Dim cursheet, path, selDB As String

Dim pg1SQL As String

Application.ScreenUpdating = False

Application.Calculation = xlCalculationManual

    cursheet = ActiveSheet.Name

    'Select Datatables sheet

        Sheets("Selections").Select

        ActiveSheet.Calculate

        Sheets("Lookup").Visible = True

        Sheets("Lookup").Select

        ActiveSheet.Calculate

    'Read in Parameters

        selDemo = Range("HB1").Value

        selCat = Range("HB2").Value

        selChan = Range("HB3").Value

        selRB = Range("HB4").Value

        selB1 = Range("HB5").Value

        selB2 = Range("HB6").Value

        selB3 = Range("HB7").Value

        selB4 = Range("HB8").Value

        selB5 = Range("HB9").Value

        selDB = Range("HB10").Value

        selSC = Range("HB11").Value

        selInd = Range("HB12").Value

        durk = Range("HB13").Value

    'Database Connection Paths

    path = ActiveWorkbook.path

    strFile = path & "" & selDB & ".mdb" 'AccessImportData.mdb

    strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile & ";"

    'Create database objects

    Set Cn = CreateObject("ADODB.Connection")

    Set cnP1 = CreateObject("ADODB.Recordset")

     Set cnP2 = CreateObject("ADODB.Recordset")

      Set cnP3 = CreateObject("ADODB.Recordset")

       Set cnP4 = CreateObject("ADODB.Recordset")

        Set cnP5 = CreateObject("ADODB.Recordset")

         Set cnP6 = CreateObject("ADODB.Recordset")

          Set cnP7 = CreateObject("ADODB.Recordset")

           Set cnP8 = CreateObject("ADODB.Recordset")

 '           Set cnP9 = CreateObject("ADODB.Recordset")

    Cn.Open strCon

 'Page 1 SQL INDDAT *****************************************************************************************8

 pg1SQL = "SELECT ryear & tchannel & varcode as code,* " _

            & "FROM inddat " _

            & " WHERE tchannel =" & selChan _

            & " and demo =" & selDemo _

            & " and varcode in (" & selSC & "," & selInd & ")" _

            & " order by ryear "

    Sheets("inddat").Visible = True

    Sheets("inddat").Select

    Range("a4:G65536").ClearContents

    cnP1.Open pg1SQL, Cn

    Range("a4").CopyFromRecordset cnP1

    Sheets("inddat").Visible = False

    'Page 2 SQL INDBRND *****************************************************************************************8

 pg2SQL = "SELECT ryear & tchannel & varcode & brand, " & durk & "*1 as tyrk, ryear & tchannel & varcode & " & durk & ", brand*1 as code , * " _

            & "FROM indbrnddolsunts " _

            & " WHERE tchannel =" & selChan _

            & " and demo =" & selDemo _

            & " and varcode in (" & selSC & ")" _

            & " order by ryear "

    Sheets("indbrnd").Visible = True

    Sheets("indbrnd").Select

    Range("a4:N65536").ClearContents

    cnP2.Open pg2SQL, Cn

    Range("a4").CopyFromRecordset cnP2

    Sheets("indbrnd").Visible = False

   Sheets("Lookup").Visible = False

   Sheets("datatables").Visible = False

   Sheets("Selections").Select

    Cells(1, 1).Select

    'Application.Calculation = xlAutomatic

   Application.ScreenUpdating = True

End Sub

*****************************Mac Macro (not working)*******************************;

Sub TestDB()

Dim selDemo, selCat, selSC, selRB, selChan, selB1, selB2, selB3, selB4, durk, selRet, selDem2, selB5 As String

Dim Cn, cnP1, cnP2, cnP3, cnP4, cnP5, cnP6, cnP7, cnP8, cnP9, cnP10, cnP11

Dim cursheet, path, selDB As String

Dim pg1SQL As String

Application.ScreenUpdating = False

Application.Calculation = xlCalculationManual

cursheet = ActiveSheet.Name

'Select Datatables sheet

Sheets("Selections").Select

ActiveSheet.Calculate

Sheets("Lookup").Visible = True

Sheets("Lookup").Select

ActiveSheet.Calculate

'Read in Parameters

selDemo = Range("HB1").Value

selCat = Range("HB2").Value

selChan = Range("HB3").Value

selRB = Range("HB4").Value

selB1 = Range("HB5").Value

selB2 = Range("HB6").Value

selB3 = Range("HB7").Value

selB4 = Range("HB8").Value

selB5 = Range("HB9").Value

selDB = Range("HB10").Value

selSC = Range("HB11").Value

  selInd = Range("HB12").Value

durk = Range("HB13").Value

selRet = Range("HB14").Value

selDem2 = Range("HB15").Value

 Dim sql

sql = "SELECT ryear & tchannel & varcode as code,* " _

& "FROM inddat " _

& " WHERE tchannel =" & selChan _

& " and demo in (77779,41096,41098)" _

& " order by ryear " _

& " and varcode in (" & selSC & "," & selInd & ")" _

path = Active.Workbook.path --- This doesn’t work – how do I get the path of the current workbook?

Dim connString

connString = "ODBC;Driver={Actual Access}; DBQ=path/CCRC_App_TEST.mdb"

Dim inddat As QueryTable

Set inddat = testwksht.QueryTables.Add(Connection:=connString, Destination:=Range("b4"))

inddat.BackgroundQuery = False

inddat.sql = sql

On Error GoTo XERR

inddat.Refresh

Exit Sub

XERR:

Dim errErrs As ODBCErrors

Dim errErr As ODBCError

Set errErrs = Application.ODBCErrors

If errErrs.Count > 0 Then

For Each errErr In errErrs

strMsg = strMsg & "#" & errErr.SqlState & " = " & errErr.ErrorString

Next errErr

MsgBox strMsg, vbCritical, "ODBC ERROR"

End If

If Err.Number <> 0 Then

MsgBox Err.Number & vbCr & Err.Source & vbCr & vbCr & Err.Description, vbCritical

Err.Clear

End If

End Sub

Microsoft 365 and Office | Excel | For home | MacOS

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Anonymous
    2012-01-16T21:25:58+00:00

    Thank you so much for this post.  The Excel Object Model for Excel 2011 is poorly documented and I spent a lot of time trying to figure this out.  Just to add, for folks that want to use a data source name, here's an example:

    ConnectionString = "ODBC;DSN=MyDataSourceName;DATABASE=MyDatabaseName;SERVER="192.255.255.255";PORT=5432;UID=MyUserName"

    Was this answer helpful?

    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Anonymous
    2011-11-13T16:17:23+00:00

    Thank you very much for your help.  Can I ask what ODBC driver you are using?  I have the one from Actual Tech installed successfully (at least when I test the connect it is successful), but when I step into the Refresh code, it says I have no ODBC driver installed.  For some reason, Excel is not finding the connection. 

    Any thoughts?

    Thanks!

    Was this answer helpful?

    0 comments No comments
  2. Jim G 134K Reputation points MVP Volunteer Moderator
    2011-11-13T01:58:25+00:00

    OK - I got your code to add a query to the worksheet using a simple query. Your parameters should work OK with your data tables. Notice the path separator is the colon (:)

    Here goes...

    Sub TestDB()

    Dim selDemo, selCat, selSC, selRB, selChan, selB1, selB2, selB3, selB4, durk, selRet, selDem2, selB5 As String

    Dim Cn, cnP1, cnP2, cnP3, cnP4, cnP5, cnP6, cnP7, cnP8, cnP9, cnP10, cnP11

    Dim cursheet, path, selDB As String

    Dim pg1SQL As String

    Application.ScreenUpdating = False

    Application.Calculation = xlCalculationManual

        cursheet = ActiveSheet.Name

        'Select Datatables sheet

            Sheets("Sheet1").Select

            ActiveSheet.Calculate

            Sheets("Sheet1").Visible = True

            Sheets("Sheet1").Select

            ActiveSheet.Calculate

        'Read in Parameters

            selDemo = Range("HB1").Value

            selCat = Range("HB2").Value

            selChan = Range("HB3").Value

            selRB = Range("HB4").Value

            selB1 = Range("HB5").Value

            selB2 = Range("HB6").Value

            selB3 = Range("HB7").Value

            selB4 = Range("HB8").Value

            selB5 = Range("HB9").Value

            selDB = Range("HB10").Value

            selSC = Range("HB11").Value

            selInd = Range("HB12").Value

            durk = Range("HB13").Value

            selRet = Range("HB14").Value

            selDem2 = Range("HB15").Value

     Dim sql

    sql = "SELECT * FROM Customers"

    path = "Macintosh HD:Users:JBG:Documents:Databases:Examples:Data:"

    Dim connString

    connString = "ODBC; DBQ=path"

    Dim inddat As QueryTable

    Set inddat = ActiveSheet.QueryTables.Add(Connection:=connString, Destination:=Range("b4"))

    inddat.BackgroundQuery = False

    inddat.sql = sql

    On Error GoTo XERR

    inddat.Refresh

    Exit Sub

    XERR:

            Dim errErrs As ODBCErrors

            Dim errErr As ODBCError

            Set errErrs = Application.ODBCErrors

            If errErrs.Count > 0 Then

                For Each errErr In errErrs

                        strMsg = strMsg & "#" & errErr.SqlState & " = " & errErr.ErrorString

                Next errErr

                MsgBox strMsg, vbCritical, "ODBC ERROR"

            End If

            If Err.Number <> 0 Then

                MsgBox Err.Number & vbCr & Err.Source & vbCr & vbCr & Err.Description, vbCritical

                Err.Clear

            End If

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Jim G 134K Reputation points MVP Volunteer Moderator
    2011-11-13T01:45:00+00:00

    Excel MVP Ron de Bruin has a superb page about using file paths, complete with examples and suggestions.:

    http://www.rondebruin.nl/mac.htm

    Aside from the file path, are you getting results from ODBC with the rest of your code?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2011-11-09T16:40:51+00:00

    Thank you for visiting the Microsoft Answers Community site. The issue you posted would be better suited in the forum below. Please visit the link below to find a community that will offer the support you request.

    http://social.msdn.microsoft.com/Forums/en/exceldev/threads

    Was this answer helpful?

    0 comments No comments