vba mac connect to sql server with ActualTech odbc Driver

北城余情 提交于 2019-12-05 18:40:52

I finally got it working......... :)

I changed the code from VBA code to fetch data from Mysql DB in Mac Excel 2011 and adapted it to the following:

Sub SqlConnection()
    Dim sqlstring As String
    Dim connstring As String
    Dim sLogin As String
    sLogin = "Uid=$;Pwd=$;"
    sqlstringfirma = "select * from gi_kunden.tbl_firma"
    sqlstringperson = "select * from gi_kunden.tbl_person"
    connstring = "ODBC;DSN=KundeDB;" & sLogin

    ActiveWorkbook.Sheets("Firma").Select
    ActiveSheet.Range("A1:T2000").Clear

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

    ActiveWorkbook.Sheets("Person").Select
    ActiveSheet.Range("A1:T2000").Clear

    For Each qt In ActiveSheet.QueryTables
        qt.Delete
    Next qt
    With ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("A1"), Sql:=sqlstringperson)
        .BackgroundQuery = False
        .Refresh
    End With
End Sub

This works fine it seems.... After hours and hours of surfing and googling ^^ (halleluja!!)

Thanks anyway :)

I finally got a little more information. I managed to get the data through odbc from the sql-database manually and this is the code I got from recording it:

Sub GetFromSQL()
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$E$3"), , xlYes).Name = _
        "Table1"
    Range("Table1[#All]").Select
    With ActiveSheet.QueryTables.Add(Destination:=Range("Table1[[#Headers],[ID]]" _
    ))
        .PostText = "ExternalData_1"
        .Name = True
        .FieldNames = False
        .RefreshStyle = xlOverwriteCells
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .HasAutoFormat = False
        .RefreshOnFileOpen = 1
        .BackgroundQuery = False
        .TablesOnlyFromHTML = True
        .SaveData = True
        .Refresh BackgroundQuery:=False
        .UseListObject = True
    End With
End Sub

Problem is, I'm not able to re-run this macro as the part where I connect to the database as well as the query are nowhere to be found here.

Does anyone have a clue, what I need to add to this code, so it'll work?

Thankx for your support.

CU Kath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!