Solved

Macro to update excel embedded sql query

Posted on 2014-09-14
6
678 Views
Last Modified: 2014-10-12
Hi,
I have a number of workbooks that have embedded sql queries. We are in the process of changing databases and upgrading applications so I need to change the connection properties of these queries.

These queries can be found in the Data tab in Connections and look like the following:

Connection
Connection1
Connection 10
.....

connection specified for queries
Each connection has properties associated with it. Under the definition tab of connection properties there is a text box entry field called Connection_string:

Currently it has the following text in it:

In some cases it is:

DRIVER=SQL Server;SERVER=TEST;UID=123456;Trusted_Connection=Yes;APP=Microsoft Office XP;WSID=123456

in a few others it is:

Description=test;DRIVER=SQL Server;SERVER=TEST;UID=123456;Trusted_Connection=Yes;APP=Microsoft®Query ;WSID=123456

I need to change the text in both instances to:

Description=new;DRIVER=SQL Server;SERVER=NEW;UID=123456;Trusted_Connection=Yes;APP=Microsoft Office XP;WSID=123456

connection query properties
Since I have many workbooks with these queries and multiple queries in each workbook, I would like an automated procedure written in VBA that I could use to update all of them automatically,

Thanks,
0
Comment
Question by:scurvylion
  • 2
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
krishnakrkc earned 500 total points
ID: 40322746
Does this help you ?

    Dim Conns       As Connections
    Dim i           As Long
    
    Set Conns = ThisWorkbook.Connections
    
    For i = 1 To Conns.Count
        Conns.Item(i).OLEDBConnection.Connection = "Description=new;DRIVER=SQL Server;SERVER=NEW;UID=123456;Trusted_Connection=Yes;APP=Microsoft Office XP;WSID=123456"
    Next

Open in new window


Kris
0
 

Author Comment

by:scurvylion
ID: 40324595
Hi Kris,

The code is not working for me, I'm getting the following error:

Run-time error '445':
Object doesn't support this action

I've been looking around to see what might work and I'm curious to know if OLEDBConnection or QueryTables the correct way to approach this type of a problem? Won't OLEDBConnection just set up the connection and open it where I want to actually replace the server name and app in the connection string?
Thanks,
0
 
LVL 18

Expert Comment

by:krishnakrkc
ID: 40324662
what this line returns ?

MsgBox Conns.Item(i).OLEDBConnection.Connection

Open in new window

0
 

Author Comment

by:scurvylion
ID: 40324681
Hi Kris,

When I run the line you provided I get another error:

Run-time error '91':
Object variable or with block variable not set

I've figured it out , in part thanks to your stimulating my thinking of how things should work. The correct code is:

Sub QueryChange()
     Dim ws As Worksheet, qy As QueryTable
     Dim OldPath As String, NewPath As String
     Dim OldAPP As String, NewAPP As String
     
     'Replaces the old server name and old APP name
     'from old database to the new server name and APP name where
     'database now resides.
     OldPath = "TEST"
     NewPath = "NEW"
     OldAPP = "Microsoft®Query"
     NewAPP = "Microsoft Office XP"

     For Each ws In ActiveWorkbook.Sheets
       For Each qy In ws.QueryTables
            qy.Connection = _
                  Application.Substitute(qy.Connection, _
                  OldPath, NewPath)
             qy.Connection = _
                  Application.Substitute(qy.Connection, _
                  OldAPP, NewAPP)
       Next qy

Next ws
End Sub

I'd like to give you some points for helping me find the solution - how so I go about that ?
Thanks,
scurvylion
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40375498
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question