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