AllExperts > Excel 
Search      
Excel
Volunteer
Answers to thousands of questions
 Home · More Excel Questions · Answer Library  · Encyclopedia ·
More Excel Answers
Question Library

Ask a question about Excel
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Tom Ogilvy
Expertise
Worked with the program for many years - provided assistance on MS Excel Newsgroups since 1997. Have received the Microsoft MVP award annually since 1999. I don't answer questions on using Excel in a browser Since I have no way to test this. Prefer not to answer charting questions. I consider myself to be particularly knowledgeable about using VBA internal to Excel but have no problems with formulas and pivot tables either.

Experience
Have Used Excel for 15 - 20 years. Answered in excess of 70,000 Excel related questions in MS Excel newsgroups. Unless obvious, please specify whether you want a worksheet function or macro/VBA solution.

Education/Credentials
BS General Engineering (concentration in Industrial Engineering) MS Operations Research Systems Analysis

 
   

You are here:  Experts > Computing/Technology > Microsoft Software > Excel > How to change connection information for a query in Excel

Excel - How to change connection information for a query in Excel


Expert: Tom Ogilvy - 7/8/2009

Question
QUESTION: I don't know vb very well and I'm getting this error when running the attached code and have no idea what to do about it.
The error is:  "Compile error:  For without Next"

I'm attaching my code for your review.

Thank you
Sue

Sub QueryChange()
   Dim sh As Worksheet, qy As QueryTable
   Dim pt As PivotTable, pc As PivotCache
   Dim oldpath As String, newpath As String
   Dim rng As Range
   
   'Replace the following paths with the original path or server name
   'wher eyour adatabase resided, and the new path or server name where
   'your database now resides.
   oldpath = "c:\data"
   newpath = "h:\databases"
   
   For Each ws In ActiveWorkbook.Sheets
       For Each qy In ws.QueryTables
           qy.Connection = _
               Application.substitue(qy.Connection, _
               oldpath, newpath)
           qy.CommandText = _
               stringtoarray(Application.Substitute(qy.CommandText, _
               oldpath, newpath))
           qy.Refresh
       Next qy
       
 '      For Each pt In ws.PivotTables
 '          pt.PivotCache.Connection = _
 '              Application.subsitute(pt.PivotCache.Connection, _
 '              oldpath, newpath)
 '          On Error Resume Next
 '          pt.PivotCache.CommandText = _
 '              stringtoarray(Application.Substitute(pt.pivotchache.CommandText, _
 '              oldpath, newpath))
 '          If Err.Number <> 0 Then
 '              Err.Clear
 '              On Error GoTo 0
 '              Application.ScreenUpdating = False
 '              Set rng = pt.TableRange2
 '              pt.TableRange2.Copy Workbooks.Add(xlWorksheet).Worksheets(1) _
 '                  .Range("A1")
 '              ActiveCell.PivotTable.PivotCache.CommandText = _
 '                  stringtoarray(Application.Substitute(pt.PivotCache.CommandText, _
 '                  oldpath, newpath))
 '              ActiveCell.PivotTable.TableRange2.Copy pt.TableRange2
 '              ActiveWorkbook.Close False
 '              Set pt = rng.PivotTable
 '              Application.ScreenUpdating = True
 '          End If
 '          pt.PivotCache.Refresh
 '      Next pt
   End Sub
   
   
   
   Function stringtoarray(query As String) As Variant
   
       Const strlen = 127
       Dim numelems As Integer
       Dim temp() As String
       
       numelems = (Len(query) / strlen) + 1
       ReDim temp(1 To numelems) As String
       
       For i = 1 To numelems
           temp(i) = Mid(query, ((i - 1) * strlen) + 1, strlen)
       Next i
       
       stringtoarray = temp
   End Function
   
               
               
               
End Function



ANSWER: Sue,

Sub QueryChange()
  Dim sh As Worksheet, qy As QueryTable
  Dim pt As PivotTable, pc As PivotCache
  Dim oldpath As String, newpath As String
  Dim rng As Range
  
  'Replace the following paths with the original path or server name
  'wher eyour adatabase resided, and the new path or server name where
  'your database now resides.
  oldpath = "c:\data"
  newpath = "h:\databases"
  
  For Each ws In ActiveWorkbook.Sheets
      For Each qy In ws.QueryTables
          qy.Connection = _
              Application.substitue(qy.Connection, _
              oldpath, newpath)
          qy.CommandText = _
              stringtoarray(Application.Substitute(qy.CommandText, _
              oldpath, newpath))
          qy.Refresh
      Next qy
  Next ws    '<===  Line added
      
'      For Each pt In ws.PivotTables
'          pt.PivotCache.Connection = _
'              Application.subsitute(pt.PivotCache.Connection, _
'              oldpath, newpath)
'          On Error Resume Next
'          pt.PivotCache.CommandText = _
'              stringtoarray(Application.Substitute(pt.pivotchache.CommandText, _
'              oldpath, newpath))
'          If Err.Number <> 0 Then
'              Err.Clear
'              On Error GoTo 0
'              Application.ScreenUpdating = False
'              Set rng = pt.TableRange2
'              pt.TableRange2.Copy Workbooks.Add(xlWorksheet).Worksheets(1) _
'                  .Range("A1")
'              ActiveCell.PivotTable.PivotCache.CommandText = _
'                  stringtoarray(Application.Substitute(pt.PivotCache.CommandText, _
'                  oldpath, newpath))
'              ActiveCell.PivotTable.TableRange2.Copy pt.TableRange2
'              ActiveWorkbook.Close False
'              Set pt = rng.PivotTable
'              Application.ScreenUpdating = True
'          End If
'          pt.PivotCache.Refresh
'      Next pt
  End Sub

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Thanks Tom!  I REALLY appreciate your quick response!
Now, I'm getting
Run-time error '428':
Object doesn't support this property or method

and it's highlighting this section

           qy.Connection = _
               Application.substitue(qy.Connection, _
               oldpath, newpath)


ANSWER: Sue,

It looks like you misspelled Substitute


qy.Connection = _
              Application.substitue(qy.Connection, _
              oldpath, newpath)


would be

qy.Connection = _
              Application.substitute(qy.Connection, _
              oldpath, newpath)

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: I want to permanently change the spreadsheet so it always looks for the database on the H:\ drive.

When I ran the code it error'd telling me the database needed to be on the c:\ drive so I put a copy back there and then it ran with no errors.
Then I deleted the database on the c:\ drive and tried to refresh data but the spreadsheet is still looking for the database on the c:\ drive.  

I'm looking for code that will permanently change the linking in this spreadshet from the C:\ to the H:\.  Is that was this code was supposed to do?


Answer
Sue,
------------ revision ----------
if you want to send me the workbook and the database (assuming it is ms access) I can take a look at it.  

twogilvy@msn.com

------------------------------------

Yes, it should change it permanently if you save the file after the changes.  The connection string establishes the correction to the database, so if it is set to look in H, it should look in H.  


Also, I believe you got that code from a Microsoft article that stated that that was the intent of the code.  They should be the authoritative source.

I have changed connection strings using code before and they always worked for me.

--
Regards,
Tom Ogilvy


Ask a Question


 
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.