Solved

Macro to update excel embedded sql query

Posted on 2014-09-14
6
720 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 48

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

717 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