Solved

Macro to update excel embedded sql query

Posted on 2014-09-14
6
709 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 47

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

Industry Leaders: 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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

738 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