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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
@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: 50
  • update2 error.jpg
    update2 error.jpg
    112.2 KB · Views: 51
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,843
Members
449,051
Latest member
excelquestion515

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