PDA

View Full Version : Macro To Add DSN



jo15765
05-18-2016, 06:11 AM
Is there a way to add a DSN to a PC through VBA? I know how to do it through windows, ODBC --> Data Sources etc, but I was wondering if this could be achieved through code?

Aflatoon
05-18-2016, 07:55 AM
Yes (https://support.microsoft.com/en-us/kb/171146) but why do you need to?

jo15765
05-18-2016, 08:09 AM
Yes (https://support.microsoft.com/en-us/kb/171146) but why do you need to?

In Excel 2013, from the Power Query Tab, I am importing multiple ODBC queries into a workbook. These queries refresh perfectly on my machine as I have the DSN installed. If refreshed from a PC that does not have the DSN installed, it errors and will not refresh.

In my mind the best solution would be to have VBA install the DSN on the machine, then run the refresh.

Another method I have been trying to work on is something like this, where I would pass credentials to the connection, then refresh (but this may not work)

ConnString = "DRIVER={PostgreSQL Unicode};DATABASE=123;SERVER=XXX.XXX.XXX.XXX;UID=XXXX;PWD=XXXX;Port:XXXX;"
With ActiveWorkbook.Connections("Connection Name"). _
OLEDBConnection
.Connection = StringToArray(ConnectionString)
End With
ActiveWorkbook.Connections("Connection Name").Refresh