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 create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You just need to change the connection string to use Location=PQHANATable
 
Upvote 0
thank you so much Rory,

i will try,

one more question, the only way to rename power query is manual renaming?

Best,
Jacek
 
Upvote 0
No, you can do it in code - for example:

Code:
activeworkbook.queries(1).Name = "NEWname"
 
Upvote 0
thank you!

How can i take from query table the name of the workbook query?

ThisWorkbook.Sheets("HANAtable").ListObjects(1).QueryTable.name = "NewName" is not working.

Best,
Jacek
 
Upvote 0
You can only directly get the connection name - eg ThisWorkbook.Sheets("HANAtable").ListObjects(1).QueryTable.WorkbookConnection.Name but from that you can generally extract the query name.
 
Upvote 0
Thank you,

ok i have checked and unfortunately it is not working,

so code:

VBA Code:
Sub CheckQT()

Dim qt As WorkbookQuery
Dim qtT As QueryTable

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

Set qtT = ThisWorkbook.Sheets("HANAtable").ListObjects(1).QueryTable
qtT.EnableRefresh = True
qtT.Connection = "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=PQHANATable;Extended Properties="
End Sub

It is changing name perfectly but setting connection is not working.
Why?

How to set up this to make this working ?

after this step i am getting error from above (download failed):
Rich (BB code):
ActiveWorkbook.Queries(1).Name = "PQHANATable"

i tried also to reverse the order - first changing connection and after query name but it is the same.

Best,
Jacek
 
Last edited:
Upvote 0
I'll have to dig into it. A macro recorded while successfully changing the connection then doesn't work if you rename the query and replay the macro. The connection string doesn't actually change.
 
Upvote 0
Ok thank you for digging in into. Let me know please,

i am struggling with issue 2 days :(

Best,
Jacek
 
Upvote 0
Hi Rory,

i think i have found another bug.
So if you are changing cases and string is the same --> you will have this error. But if tableName1 <> tableNameToReplace --> code will work. Without changing connection even jsut changing power query name.

What do you think?
I am in constant contanct with Excel developemt Team manager so i will ask him about fixing it.

Best,
Jacek
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
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