Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel queryTable connection string query table missing? 1

Status
Not open for further replies.

tchor7

Programmer
Jul 19, 2004
24
US
Looks like it cannot find my query tables??? below is an almost identical thread to this issue:


Many searches on the internet proposed Skips solution:

For Each ws In ThisWorkbook.Worksheets
ws.select
For Each qt In ws.QueryTables
qt.Connection = sConnection
Next
Next

It never gets to the qt.Connection line

When I test in the immediate window, I get an error:

Worksheets(1).QueryTables(1).count

I must be missing something very simple. Please advise.

Thanks,
Thoeum
 


Do you have 2007+ Excel?

If so...
Code:
dim lo as listobject

For Each ws In ThisWorkbook.Worksheets
    ws.select
    For Each lo In ws.listobjects
        lo.querytable.Connection = sConnection
    Next
Next


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hi Skip - thanks for the reply. It did loop through, but now getting error: 1004

My code:

sPath = ThisWorkbook.Path
sDB = ThisWorkbook.Name

sConnection = "DSN=Excel Files;DBQ=" & sPath & "\" & sDB & ";DefaultDir=" _
& sPath & "\" _
& ";DriverId=1046;MaxBufferSize=2048;PageTimeout=5;"

For Each ws In ThisWorkbook.Worksheets
ws.Select
For Each lo In ws.ListObjects
lo.QueryTable.Connection = sConnection
Next
Next

current queryTable connection string:

DSN=Excel Files;DBQ=H:\QueryTest.xlsm;DefaultDir=H:\;DriverId=1046;MaxBufferSize=2048;PageTimeout=5;

sConnection value under "watch window":

"DSN=Excel Files;DBQ=C:\Documents and Settings\tc074h\Desktop\Book1.xlsx;DefaultDir=C:\Documents and Settings\tc074h\Desktop\;DriverId=1046;MaxBufferSize=2048;PageTimeout=5;"

Thanks,
Thoeum
 

You do not want a \ after the path in assgning the DefaultDir...
Code:
    sConnection = "DSN=Excel Files;"
    sConnection = sConnection & "DBQ=" & sPath & "\" & sDB & ";"
    sConnection = sConnection & "DefaultDir=" & sPath & ";"
    sConnection = sConnection & "DriverId=1046;MaxBufferSize=2048;PageTimeout=5;"

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Thanks Skip - I made the changes you suggested. Just had use the replace() fn and it works like a charm! much thanks.

lo.QueryTable.Connection = Replace(lo.QueryTable.Connection, stOldConnection, sConnection)

-Thoeum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top