Hi the DSN is looking at the server.
I found this code which works (see below) from another forum by Jerry Sullivan, where you can change the database. It only works once because it has constant the string stays the same, i tried changing the code to having the database as a variable but this did not work. As i have 15 database with the same structure on the same server, i want to swap between database to find my results, only thing which needs changing is the database name.
I have pasted the code
Sub SwitchODBCSource()
Dim conn As WorkbookConnection
Dim sOldConnection As String, sNewConnection As String
Const sOldPath As String = "DSN=xxxx;UID=XX;PWD=xxx;APP=Microsoft Office 2010;WSID=xxx;DATABASE=test2; '--omit trailing backslashes to change DefaultDir
Const sNewPath As String = "DSN=xxxx;UID=XX;PWD=xxx;APP=Microsoft Office 2010;WSID=xxx;DATABASE=test2;"
For Each conn In ActiveWorkbook.Connections
With conn
If .Type = xlConnectionTypeODBC Then
sOldConnection = .ODBCConnection.Connection
If InStr(1, sOldConnection, sOldPath) > 0 Then
sNewConnection = Replace(sOldConnection, _
sOldPath, sNewPath, Compare:=vbTextCompare)
.ODBCConnection.Connection = sNewConnection
.Refresh '--optional to refresh now
End If
End If
End With
Next conn
Set conn = Nothing
End Sub