Solved

Macro to update excel embedded sql query

Posted on 2014-09-14
6
622 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 45

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now