1

I am creating a button within Excel which is supposed to change the connection string (to SQL Server) according to few parameters entered on a specific excel sheet. I have came across this link Excel macro to change external data query connections - e.g. point from one database to another

I concluded the following but it is not working

Sub GetConnections()
    Dim ConnectionString
    ConnectionString = "DRIVER=SQL Server;SERVER=.;UID=sa;PWD=123;APP=Microsoft Office 2013;WSID=MachineID;DATABASE=X"

    Dim w As Worksheet
    Dim cn As WorkbookConnection
    Dim OdbcCn As ODBCConnection
    For Each cn In ThisWorkbook.Connections
        Set OdbcCn = cn.ODBCConnection
        OdbcCn.SavePassword = True
        OdbcCn.Connection = ConnectionString

    Next
MsgBox ("Done")
End Sub

When I run the code above, I get Run Time error (1004): Application-defined or object-defined error

The debugger highlights this "OdbcCn.Connection = ConnectionString"

Your help is highly appreciated,

Community
  • 1
  • 1
MahmoudSa
  • 11
  • 3

0 Answers0