I have been working on my problem for a while, and in fact I really canโt find an exact and working answer, so here I am ... I am new to vba, I have to admit .......
My problem is this:
I am working on Mac OS X Yosemite since ms office 2011 and I am trying to connect my Excel workbook to sql database. This needs to be done through vba, since later I want to read data from the sql database, and also write data to the database. I could not find a way to establish a connection to this database. I even downloaded the actual odbc driver and installed my dsn (not sure if I did it right, although I couldnโt find how to do this with the sql server on the home page ....)
The following code is all I could find, but I still have errors:
strSRV = "mysql01.gutknecht-net.com" strDB = "gi_kunden" sqlLogin = "TEST" 'has to be changed sqlPW = "TEST_PW" 'has to be changed strConn = "ODBC;DSN=" & strSRV & ";UID=" & sqlLogin & ";PWD=" & sqlPW & ";Database=gi_kunden" With Sheets("Firma").ListObjects sqlCommand = "Select * From tbl_firma" .Add(SourceType:=0, Source:=strConn, LinkSource:=True, Destination:=ActiveWorkbook.Sheets("Firma").Range("A2")).QueryTable 'Get an error here .CommandText = Array(sqlCommand) End With With Sheets("Person").ListObjects sqlCommand = "Select * From tbl_person" .Add(SourceType:=0, Source:=strConn, LinkSource:=True, Destination:=ActiveWorkbook.Sheets("Person").Range("A2")).QueryTable 'Get an error here .CommandText = Array(sqlCommand) End With
Also tried it with the following code:
strConn = "Provider=SQLNCLI10;" & _ "Server=" & strSRV & ";" & _ "Database=" & strDB & ";" & _ "UID=" & sqlLogin & ";" & _ "PWD=" & sqlPW & ";"
but it still does not work ...
If anyone could help me, I would really appreciate it. If you need more information, just say so :)
vba sql-server excel excel-vba-mac macos
Kathara
source share