• 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
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Check if power query is valid using VBA code|Power Query|Excel Forum|My Online Training Hub

You are here: Home / Check if power query is valid using VBA code|Power Query|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
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 QueryCheck if power query is valid using…
sp_PrintTopic sp_TopicIcon
Check if power query is valid using VBA code
Avatar
jaryszek
Member
Members
Level 0
Forum Posts: 183
Member Since:
February 1, 2019
sp_UserOfflineSmall Offline
1
February 23, 2021 - 8:35 pm
sp_Permalink sp_Print sp_EditHistory

Hi Guys,

this is cross posted from here:

https://www.excelforum.com/exc.....valid.html

https://www.mrexcel.com/board/.....st-5643754

I have DataSourceError:

Screenshot_124-1.pngImage Enlarger

error in power query - assume that source query was removed. 

And my code is adding queries from VBA:

Sub Macro1() ActiveWorkbook.Queries.Add Name:="Table1", Formula:= _ "let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(File.Contents(""D:\Pulpit\Newest Pull request\Importing PQ new way\ExampleTable.xlsx""), null, true)," & Chr(13) & "" & Chr(10) & " Table1_Table = Source{[Item=""Table1"",Kind=""Table""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table1_Table,{{""Col1"", Int64.Type}, {""Col2"", Int64.Type}, {""Col3"", Int64.Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type""" End Sub

The issue is that VBA will add queiry but it will be invalid. 
What i want to achevie is to check if PQ has error before loading into worksheet. 

It is possible via VBA? 
Maybe checking formula ? 

Please help, i am loosing hope,
Best wishes
Jacek

sp_PlupAttachments Attachments
  • sp_PlupImage Screenshot_124.png (166 KB)
  • sp_PlupImage Screenshot_124-1.png (166 KB)
sp_AnswersTopicSeeAnswer See Answer
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
February 26, 2021 - 2:36 pm
sp_Permalink sp_Print

I guess you want to rebuild the Power Query Intellisense in VBA, to test if the syntax is correct before running the query?

It's not realistic, this feature took a lot of time to be built by Microsoft team.

Just add the query in a different way:

let Result = try "your query here" otherwise null in Result

If the query result is null, then something was wrong, but you will not know why until you inspect the query.

sp_AnswersTopicAnswer
Answers Post
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
3
February 26, 2021 - 2:58 pm
sp_Permalink sp_Print

If it's just the file existence you want to check, use:

 

Sub Macro1()
Dim TextString As String, FilePath As String, Pos As Long
TextString = "let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(File.Contents(""D:\Pulpit\Newest Pull request\Importing PQ new way\ExampleTable.xlsx""), null, true)," & Chr(13) & "" & Chr(10) & " Table1_Table = Source{[Item=""Table1"",Kind=""Table""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table1_Table,{{""Col1"", Int64.Type}, {""Col2"", Int64.Type}, {""Col3"", Int64.Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
Pos = InStr(1, TextString, "File.Contents", vbTextCompare)
FilePath = Mid(TextString, Pos + 15, InStr(Pos + 15, TextString, ")", vbTextCompare) - Pos - 16)
If CreateObject("Scripting.FileSystemObject").FileExists(FilePath)=False then
MsgBox "File Does not exist!"
Exit Sub
End If

ActiveWorkbook.Queries.Add Name:="Table1", Formula:=TextString
End Sub

Avatar
jaryszek
Member
Members
Level 0
Forum Posts: 183
Member Since:
February 1, 2019
sp_UserOfflineSmall Offline
4
February 26, 2021 - 7:40 pm
sp_Permalink sp_Print

thank you Catalin.

2) solution is nice.

let Result = try "your query here" otherwise null in Result

This is interesting. I could do something like that in M language but still i could not pass it into VBA result yes?

Best,
Jacek

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
5
February 26, 2021 - 9:16 pm
sp_Permalink sp_Print

What do you mean? "i could not pass it into VBA result yes?"

Do you want to replicate the Power Query engine to run the query text in VBA as a "virtual query"? Microsoft worked on it for many years, i'm afraid it's not possible.

A workaround is possible, if you add the query to a new book just for testing, refresh the new book to load the query results to sheet. If the result is null, you will know that query failed, at this point you will know if there is an error.

Why test the entire query, if you only need the path to be checked?

Avatar
jaryszek
Member
Members
Level 0
Forum Posts: 183
Member Since:
February 1, 2019
sp_UserOfflineSmall Offline
6
February 27, 2021 - 12:16 am
sp_Permalink sp_Print

Thank you Catalin,

you have right. I am just checking all possibilities. 

I will check if file is available first.
Best,
Jacek

Avatar
jim hubbard

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
April 10, 2022
sp_UserOfflineSmall Offline
7
September 5, 2022 - 6:04 pm
sp_Permalink sp_Print

There's another answer in another thread here that may also help - https://www.myonlinetraininghu.....-of-data 

"RefreshAll works different in VBA than from user interface, in VBA will not raise error message when a query fails.

Loop through connections and refresh them one by one:

For Each Connection In ActiveWorkbook.Connections
If Connection.Type = xlConnectionTypeOLEDB Then
Connection.OLEDBConnection.BackgroundQuery = False
On Error GoTo 1
Connection.Refresh
End If
2:
Next Connection
Exit Sub
1:
If Err.Number <> 0 Then
'write to a cell: ActiveWorkbook.Name & " failed to refresh!"
End If
Resume 2
sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Scott Miller, Roy Lutke, Jeff Krueger
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
John Chisholm
vexokeb sdfg
John Jack
Malcolm Toy
Ray-Yu Yang
George Shihadeh
Naomi Rumble
Uwe von Gostomski
Jonathan Jones
drsven
Forum Stats:
Groups: 3
Forums: 24
Topics: 6212
Posts: 27236

 

Member Stats:
Guest Posters: 49
Members: 31889
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.