Update Microsoft Query ODBC Connection and Command text through VBA

MzCheez

New Member
Joined
Mar 3, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
My company current migrated our data to a new server. On doing so I updated the ODBC connection but that did not update in any of my 100+ microsoft queries.

After a lot of research I was able to put the follow two together. But when I came to a report that had multiple tables, the second VBA didnt work and i had to do them manually. Please help:

Sub Fixmyreport1()

Dim conn As WorkbookConnection
Dim sOldConnection As String, sNewConnection As String

Const sOldPath As String = "\\VM2" '--omit trailing backslashes to change DefaultDir
Const sNewPath As String = "\\chr-timber"

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
End If
End If
End With
Next conn

Set conn = Nothing

End Sub

Public Sub fixmyreport2()

Dim ws As Worksheet, lo As ListObject, qt As QueryTable, prev As String

For Each ws In ActiveWorkbook.Worksheets
For Each lo In ws.ListObjects
Set qt = lo.QueryTable
prev = qt.CommandText
qt.CommandText = Replace(qt.CommandText, "\\VM2", "\\chr-timber", compare:=vbTextCompare)
MsgBox "Previous CommandText: " & prev & vbCrLf & "New CommandText: " & qt.CommandText
Next
Next

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
@John_w - I got the second portion from you. I was hoping you could help me adjust it. Or even make the whole Macro one.
 
Upvote 0
looks like the list objects causing the issue
 

Attachments

  • update1 error.jpg
    update1 error.jpg
    16.5 KB · Views: 57
  • update2 error.jpg
    update2 error.jpg
    112.2 KB · Views: 58
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top