Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Macro to update excel embedded sql query

Posted on 2014-09-14
6
Medium Priority
?
840 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
5 Comments
 
LVL 18

Accepted Solution

by:
krishnakrkc earned 2000 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 50

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

564 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