• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Error When Refreshing Power Query Connections Using VBA|Power Query|Excel Forum|My Online Training Hub

You are here: Home / Error When Refreshing Power Query Connections Using VBA|Power Query|Excel Forum|My Online Training Hub

vba course banner

Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumPower QueryError When Refreshing Power Query C…
sp_PrintTopic sp_TopicIcon
Error When Refreshing Power Query Connections Using VBA
Avatar
Ryan Kenny
Member
Members
Level 0
Forum Posts: 12
Member Since:
November 2, 2021
sp_UserOfflineSmall Offline
1
November 8, 2021 - 10:50 pm
sp_Permalink sp_Print sp_EditHistory

Hi everyone,

I'm getting a strange error when trying to refresh one of my Power Query connections using some VBA (apologies if this should be in the VBA forum - I wasn't sure where to post it!).

The line "ActiveWorkbook.Connections("MyQueryName").Refresh" gets "Run-time error 1004: Initialization of the data source failed"

To try to troubleshoot, I stripped out all of the other code so my sub consists of just that line - same error. Then I  switched on the Macro Recorder and refreshed the query using the "Queries and Connections" pane. That didn't throw any errors, and seems to have refreshed the data successfully. But when I checked the output from the Macro Recorder it had recorded precisely the same line as I had used in my code! Then, when I tried to run my code a second time - no error. But, if I close the file and re-launch it, then try to run my code again - I get the same run-time error. In other words, this line seems to work fine, but only if I have used the 'Refresh Connection' button in Excel to do the same thing first.

There are other connections in the workbook, and all of them refresh fine using the same line of VBA - it is just this one that fails in this way. The strange thing is that this is the only one that does does not have any external connections. The previous queries (which refresh fine using this code) do, and this one (which fails) is a merge of all of the previous ones.

I'm baffled by this. Any tips on what might be causing it and how I can resolve / work around? I need to be able to do this refresh programmatically because (when functional!) it will be part of a more elaborate macro pulling data in from many workbooks

Thanks in advance (and if this should be in the other forum, obviously please feel free to move it!)

Ryan

sp_AnswersTopicSeeAnswer See Answer
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1826
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
November 9, 2021 - 2:31 pm
sp_Permalink sp_Print

If works from excel, it should work from vba as well.

Try with background query set to false, otherwise vba will not wait until the query finishes. You can change that setting from query settings.

Avatar
Ryan Kenny
Member
Members
Level 0
Forum Posts: 12
Member Since:
November 2, 2021
sp_UserOfflineSmall Offline
3
November 10, 2021 - 3:26 am
sp_Permalink sp_Print

Thanks Catalin! I knew it should - but could not see why it wasn't!

 

That seems to have sorted it, thanks for your help

Avatar
Ryan Kenny
Member
Members
Level 0
Forum Posts: 12
Member Since:
November 2, 2021
sp_UserOfflineSmall Offline
4
November 16, 2021 - 3:38 am
sp_Permalink sp_Print

Sorry Catalin - I thought your answer had resolved the problem, but I've just returned to the file and tried to run the code again and found that it has in fact persisted. All of the queries have background refresh set to false, but it doesn't help.

Still the same (maddening!) issue - when I refresh the query using the refresh button in the Queries and Connections pane in Excel, it works fine, but when I try to do it via VBA (even using the precise syntax generated by the macro recorder) I get that Initialization of data source error. This all only happens for the first instance that I try to refresh after the file opens. If I open the file and try to refresh via VBA - error. If I open and refresh via Excel - no error. If I open, refresh via Excel, and then again via VBA - no error.

This is driving me crazy! Any thoughts?

Avatar
Alan Sidman
Steamboat Springs, CO
Member
Members


Trusted Members
Level 0
Forum Posts: 150
Member Since:
October 18, 2018
sp_UserOfflineSmall Offline
5
November 16, 2021 - 7:16 pm
sp_Permalink sp_Print sp_EditHistory

Have you tried.

ActiveWorkbook.RefreshAll

sp_AnswersTopicAnswer
Answers Post
Avatar
Ryan Kenny
Member
Members
Level 0
Forum Posts: 12
Member Since:
November 2, 2021
sp_UserOfflineSmall Offline
6
November 17, 2021 - 3:13 am
sp_Permalink sp_Print

Thanks Alan! I feel very silly but RefreshAll seems to work! Obviously the simplest solution, which I should have thought of ages ago. Will monitor the file for next few days, but I'm sure it will be fine

(I used ThisWorkBook instead of ActiveWorkbook because there's some other parts of my code that open source workbooks in the background so I can unlock and load from password-protected sheets)

I still have no idea why my original version had that strange bug, but don't really care to be honest. Thanks both for your help

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Mohamed Jelle Hussein, Stanislas Louzier
Guest(s) 10
Currently Browsing this Page:
3 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 873
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 222
Jessica Stewart: 218
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Aminu Sule
Samuel Ramirez
Abdifatah Osman
mohad borhom
Abayomi Adedeji
Ganesh MVS
Gilbert Lemek
Ashleigh Farquharson
Jayz Luu
Fred Smith
Forum Stats:
Groups: 3
Forums: 24
Topics: 6364
Posts: 27829

 

Member Stats:
Guest Posters: 49
Members: 32346
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.