Question : Change MS Query Data Source In Excel

Recently I our corporate SQL database was moved onto a new server with a new name. Now, all of the Excel spreadsheets we had set up to query from this database look for the old datasource and cannot find it. The database is the same, just not the name of the machine on which it resides. So far, all of my attempts to point the spreadsheets in the right direction using the data source wizrd in Excel have failed.  I have a feeling there is some easy way to do this but I can't seem to figure it out... There are a bunch of spreadsheets to be "re-directed" to the new server so if there is some way I can manually change the database name in a connection string of some kind that would be best.

Thanks,
Andrew

Answer : Change MS Query Data Source In Excel

Hi ocah,

Jeroen is correct in what he says - you need to change it programatically. Here is some code which should set you down the right path.

This code takes a folder and then opens every Excel file in that folder (not subfolders) one at a time. With the open file, it then looks at every sheet and for every query table on that sheet gives you a message box with the current connection string, and asks if you want to change the connection string to the one you have specified. If you choose yes it will change the connection string. It then saves and closes the file and moves on to the next one.

There are two variables you have to define -
1. myFolder: the folder you want to search through.
2. NewConnection: The new connection string (the one to the new position of your database). Change both of these to suit your needs.

The code has the conn string for an Access dbase. You obviously will need the correct SQL one.

############WARNING##############
Once you have changed the connection it will be permanantly changed, so make sure you are set up correctly before hand, and that you only click the "Yes" button for ones you are sure you want to change.

Sub UpdateQTs()

Dim fs, fl, f
Dim wb As Workbook, ws As Worksheet, qt As QueryTable

Dim NewConnection As String
NewConnection = "ODBC;" & _  '<                "DBQ=F:\myDB.mdb;" & _
                "DefaultDir=F:\;" & _
                "Driver={Microsoft Access Driver (*.mdb)};" & _
                "DriverId=281;" & _
                "FIL=MS Access;" & _
                "MaxBufferSize=2048;" & _
                "MaxScanRows=8;" & _
                "PageTimeout=5;" & _
                "SafeTransactions=0;" & _
                "Threads=3;" & _
                "UID=admin;" & _
                "UserCommitSync=Yes;"

Dim myFolder As String
myFolder = "C:\path\"   '<
Set fs = CreateObject("Scripting.FileSystemObject")
Set fl = fs.GetFolder(myFolder)

For Each f In fl.files
    If LCase(Right(f.Name, 3)) = "xls" Then
        Set wb = Workbooks.Open(f.Path, False)
        For Each ws In ThisWorkbook.Worksheets
            If ws.QueryTables.Count > 0 Then
                For Each qt In ws.QueryTables
                    If MsgBox("Current Connection String:" & Chr(10) & Chr(10) & _
                                Left(qt.Connection, CInt(Len(qt.Connection))) & Chr(10) & _
                                Mid(qt.Connection, CInt(Len(qt.Connection)) + 1) & Chr(10) & Chr(10) & _
                                "Do you wish to change this to:" & Chr(10) & Chr(10) & _
                                Left(NewConnection, CInt(Len(NewConnection))) & Chr(10) & _
                                Mid(NewConnection, CInt(Len(NewConnection)) + 1), vbYesNo, "Connection String Change") = vbYes Then
                        qt.Connection = NewConnection
                    End If
                Next
            End If
        Next
        wb.Close True
    End If
Next

Set fl = Nothing: Set fs = Nothing: Set wb = Nothing

End Sub


Cheers,
MalicUK.

Random Solutions  
 
programming4us programming4us