Change connection of query table

jaryszek

Board Regular
Joined
Jul 1, 2016
Messages
213
Hi Guys,

i am using this code to create listobject with oledb connection from external web source.
It is working nice.

VBA Code:
Sub test()

QName = "HanaTable"

MFormula = ActiveSheet.[E1].Value
ThisWorkbook.Queries.Add Name:="PQ" & QName, Formula:=MFormula

          With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
                "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & "PQ" & QName & ";Extended Properties=""""" _
                , Destination:=Range("$A$1")).QueryTable
                .CommandType = xlCmdSql
                .CommandText = Array("SELECT * FROM " & "PQ" & QName)
                .RowNumbers = False
                .FillAdjacentFormulas = False
                .PreserveFormatting = True
                .RefreshOnFileOpen = False
                .BackgroundQuery = True
                .RefreshStyle = xlInsertDeleteCells
                .SavePassword = False
                .SaveData = True
                .AdjustColumnWidth = True
                .RefreshPeriod = 0
                .PreserveColumnInfo = True
                .ListObject.DisplayName = QName
                .Refresh BackgroundQuery:=False
            End With
          
              With ActiveSheet.ListObjects(1).QueryTable
'                .WorkbookConnection.Name = "PQ" & QName
                .WorkbookConnection.RefreshWithRefreshAll = False
                .WorkbookConnection.OLEDBConnection.BackgroundQuery = False
            End With
          
          
End Sub

Screenshot_252.png

This is result.

The issue is that i had to change all the names from camel cases to upper cases for power queries names, so in this case it is "HanaTable" and should be replaced to "HANATable".
After replacing i am getting error:
1638869851898.png


I renamed this manually (i suppose in VBa it is not possible).
But why it is no more connected with workbook?

steps to reproduce:
1) renamte "hanaTable" to "HANAtable".
2) Try to refresh power query.
3) See the error.

I need code to do this or workaround...
Please help,

link to example workbook:

Best,
Jacek
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Yes, it seems to work fine with a completely different name.
 
Upvote 0
Ok Rory,

after few more tests seems that problem is not that general.

Tried with code:

Rich (BB code):
Sub CheckQT()

Dim qt As WorkbookQuery
Dim qtT As QueryTable
Set qtT = ThisWorkbook.Sheets("HANAtable").ListObjects(1).QueryTable
qtT.EnableRefresh = True
qtT.Connection = "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=PQnewName;Extended Properties="
qtT.CommandText = "SELECT * FROM [PQnewName]"

ActiveWorkbook.Queries(1).Name = "PQnewName"

End Sub

and error occured once again. But connection has changed and sql command also.
Connection was lost...

Can you please check maybe you will find why?
Or maybe another forum guys have idea how to fix it?

Best,
Jacek
 
Upvote 0
You should change the query name before you change anything that refers to it.
 
Upvote 0
ech i also tried to change query name as first (still error occurs) and when you are doing this manually you do not have to even change any connection.

In vba after renaming you are getting our error.
So i suppose it is a bug.

Best,
Jacek
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top