Option Explicit
Sub swap_connections()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim wcOLD As WorkbookConnection
Dim wcNEW As WorkbookConnection
Dim strName As String
Dim strOLD(0 To 2) As String
strOLD(0) = "F:\Live_Databases\DATA FILES.mdb"
strOLD(1) = "F:\Live_Databases\DATA FILES Annex.mdb"
Dim strNEW(0 To 2) As String
strNEW(0) = "P:\Test_Databases\DATA FILES.mdb"
strNEW(1) = "P:\Test_Databases\DATA FILES Annex.mdb"
Dim i As Long
Dim ws As Worksheet
Set ws = wb.Worksheets.Add()
ws.Names.Add Name:="nmConnection", RefersTo:=ws.Range("$a$1")
ws.Names.Add Name:="nmCommandText", RefersTo:=ws.Range("$a$2")
Dim odbcConn As ODBCConnection
Dim oledbConn As OLEDBConnection
Dim skip As Boolean
skip = False
Dim defdirOLD As String
Dim defdirNEW As String
Dim strFromOLD As String
Dim strFromNEW As String
Dim GetOut As Boolean
GetOut = False
Dim strDescription As String
Dim strDB As String
Dim tp As String
For Each wcOLD In wb.Connections
strDB = vbNullString
' Get current connection string
If wcOLD.Type = xlConnectionTypeODBC Then
skip = False
Set odbcConn = wcOLD.ODBCConnection
ws.Range("nmConnection").Value = odbcConn.Connection
ws.Range("nmCommandText").Value = odbcConn.CommandText
ElseIf wcOLD.Type = xlConnectionTypeOLEDB Then
skip = False
Set oledbConn = wcOLD.OLEDBConnection
ws.Range("nmConnection").Value = oledbConn.Connection
ws.Range("nmCommandText").Value = oledbConn.CommandText
Else
skip = True
' This version doesn't include handling for text, web, or other sources
End If
' Find and replace appropriate connection strings (and/or CommandText)
For i = LBound(strOLD) To UBound(strOLD)
GetOut = False
defdirOLD = "DefaultDir=" & Left(Replace(strOLD(i), Split(strOLD(i), "\")(UBound(Split(strOLD(i), "\"))), ""), Len(Replace(strOLD(i), Split(strOLD(i), "\")(UBound(Split(strOLD(i), "\"))), "")) - 1)
defdirNEW = "DefaultDir=" & Left(Replace(strNEW(i), Split(strNEW(i), "\")(UBound(Split(strNEW(i), "\"))), ""), Len(Replace(strNEW(i), Split(strNEW(i), "\")(UBound(Split(strNEW(i), "\"))), "")) - 1)
strFromOLD = Replace(strOLD(i), ".mdb", "")
strFromNEW = Replace(strNEW(i), ".mdb", "")
strDB = Replace(Split(strOLD(i), "\")(UBound(Split(strOLD(i), "\"))), ".mdb", "")
If InStr(ws.Range("nmConnection").Value, strOLD(i)) > 0 Then
ws.Range("nmConnection").Value = Replace(Replace(ws.Range("nmConnection").Value, strOLD(i), strNEW(i)), defdirOLD, defdirNEW)
GetOut = True
End If
If InStr(ws.Range("nmCommandText").Value, strFromOLD) > 0 Then
ws.Range("nmCommandText").Value = Replace(ws.Range("nmCommandText").Value, strFromOLD, strFromNEW)
GetOut = True
End If
If GetOut = True Then
Exit For
End If
Next i
If Left(strNEW(0), 3) = "P:\" Then
strDescription = "TEST"
ElseIf Left(strNEW(0), 3) = "F:\" Or InStr(ws.Range("nmConnection").Value, "ServerName=192.168.1.208.1583") > 0 Then
strDescription = "LIVE"
End If
strDescription = strDescription & " connection to " & strDB
strName = wcOLD.Name
If Not skip = True Then
On Error Resume Next
Set wcNEW = wb.Connections.Add(Name:=strName & "_NEW", Description:=strDescription, ConnectionString:=ws.Range("nmConnection").Value, CommandText:=ws.Range("nmCommandText").Value, lCmdtype:=xlCmdSql)
If wcNEW.Type = xlConnectionTypeODBC Then
tp = "ODBC"
With wcNEW.ODBCConnection
.BackgroundQuery = False
.RefreshOnFileOpen = False
.SavePassword = True
.SourceConnectionFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
ElseIf wcNEW.Type = xlConnectionTypeOLEDB Then
tp = "OLEDB"
With wcNEW.OLEDBConnection
.BackgroundQuery = False
.RefreshOnFileOpen = False
.SavePassword = True
.SourceConnectionFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
.MaintainConnection = True
End With
End If
If Err.Number > 0 Then
MsgBox ("Fail on: " & strName & vbCrLf & "Type = " & tp & vbCrLf & "Error Description = " & Err.Description)
Stop
Else
' If new connection successfully added, delete old and rename new
wcOLD.Delete
' Remove "_NEW" from Name
wcNEW.Name = strName
End If
On Error GoTo 0
Else
MsgBox ("Skip = True" & vbCrLf & "strName = " & strName)
End If
Next wcOLD
ws.Delete
Set wb = Nothing
Set wcOLD = Nothing
Set wcNEW = Nothing
End Sub
Bookmarks