Hello this is my script for changing the data source and catalog.
When I run the script I can see in the console that the source table and catalog are changing, but when I enter in my DataModel in Excel and check Existing Connection I still see the old one.
Also, when I open the folder and check date modified I see that it's saved the excel file, but the changes for Data Source and Catalog are not there.
Sub changeConn()
Dim conn As Variant
Dim connectString As String
For Each conn In ActiveWorkbook.Connections
connectString = ActiveWorkbook.Connections("NGNSQLMDR MDR Sales").OLEDBConnection.Connection
Debug.Print "Before changes: "; connectString
connectString = Replace(connectString, "Catalog=MDR", "Catalog=MDR")
connectString = Replace(connectString, "Data Source=SQLMDR", "Data Source=sql.Company.local")
Debug.Print "After Changes: "; connectString
conn.OLEDBConnection.Connection = connectString
Next conn
End Sub
Stringwhich contains your changes. You don't actually USE that new string to do anything! After your replacement lines, you need to use something likeconn.ConnectionString = connectStringin order to change the actual connection object's connection string to yourconnectStringwith the new details.connis after theFor Each connline? I don't know if you're using OLE or ADODB con objects or whatconnvariant when the code is running using theVIEW>LOCALS WINDOW- it should be very simple to set a connection string on theconnobject but there are different types of connection object so in order to give you an accurate answer it'd be best to know what theconnobject is.