+ Reply to Thread
Results 1 to 21 of 21

Power Query Experts Add file picker and pass data to the query issue

  1. #1
    Forum Contributor
    Join Date
    01-31-2015
    Location
    East Midlands
    MS-Off Ver
    Excel 2019
    Posts
    132

    Power Query Experts Add file picker and pass data to the query issue

    Excel 2019

    I needed a power Query that imports a delimited text file and performs the same query but on different files as needed using a file picker.

    I recorded the query as a macro as it does everything I need except to pass the user selected file name and other related data from the file picker to the locations indicated in the code and the Parts 1 - 6 below.

    I have been able to add a file picker dilogue and added some code to the query to pass the data to it but it still does not work.
    When I run the sub the file picker opens, I can choose and click on any text file but the code still opens the file that is hard coded in to the query by the recorded macro.

    As you will see from the sub name, I am on revision 8 and I am out of my depth with Power Query hence the title of the post.

    I attach a small workbook and text file of fake data.

    From what I can tell...
    Part 1.
    In the code below at this line of code "ActiveWorkbook.Queries.Add Name:="Fake Test Data 1", Formula:=" the sub needs to pass the user selected file name from the file picker and replace "Fake Test Data 1" with the name of the user selected file, the file suffix is NOT REQUIRED.

    Part 2.
    In the code below at this line of code "Source = Csv.Document(File.Contents(""C:\Test File\Fake Test Data 1.txt"")," the sub needs to pass the full path and file name of the user selected file with suffix from the file picker.

    Part 3.
    In the code below at this line of code ".CommandText = Array("SELECT * FROM [Fake Test Data 1]")" ' the sub needs to pass the user selected file name from the file picker and replace "Media Pro Tools NEW" with the name of the user selected file, the file suffix is NOT REQUIRED.

    Part 4.
    In the code below at this line of code "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Fake Test Data 1"";Extended Properties=""""" _ ' the sub needs to pass the user selected file name from the file picker and replace "Media Pro Tools NEW" with the name of the user selected file, the file suffix is NOT REQUIRED.

    Part 5.
    In the code below at this line of code "#.ListObject.DisplayName = "Fake Test Data 1"" 'the sub needs to pass the user selected file name from the file picker and replace "Media Pro Tools NEW" with the name of the user selected file, the file suffix is NOT REQUIRED and any SPACES in the file name must be replaced with a Underscore "_".

    Part 6.
    In the code below at this line of code "ActiveSheet.ListObjects("Fake_Test_Data_1").Unlist" 'the sub needs to pass the user selected file name from the file picker and replace "Media Pro Tools NEW" with the name of the user selected file, the file suffix is NOT REQUIRED and any SPACES in the file name must be replaced with a Underscore "_".

    This is the code from the macro recorded query.
    Please Login or Register  to view this content.

    This is the file picker and extra that I added to the query.
    Please Login or Register  to view this content.

    This is the full sub code (such as it is) with the new code added.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,883

    Re: Power Query Experts Add file picker and pass data to the query issue

    Is this you too? http://www.vbaexpress.com/forum/show...lid-characters
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Contributor
    Join Date
    01-31-2015
    Location
    East Midlands
    MS-Off Ver
    Excel 2019
    Posts
    132

    Re: Power Query Experts Add file picker and pass data to the query issue

    Quote Originally Posted by romperstomper View Post
    Hi romperstomper

    Yes, that was a question that I had about an error message "query name contains invalid characters" and it was answered and closed so I did not think
    it qualified as a cross post as the post here, although about a query, is about how to pass the file name details from the file picker to the various parts
    of the query code.

    Hope this clears that up.

  4. #4
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Power Query Experts Add file picker and pass data to the query issue


  5. #5
    Forum Contributor
    Join Date
    01-31-2015
    Location
    East Midlands
    MS-Off Ver
    Excel 2019
    Posts
    132

    Re: Power Query Experts Add file picker and pass data to the query issue

    Quote Originally Posted by JEC. View Post
    Hi JEC.

    Yeah that is "kind of" a simular question but that OP's needs are lesser than mine.

    I know very little about Power Query (hence asking for PQ Experts) but in my code the query needs specific information passing to it from the file picker which I listed in my OP but for completeness...

    From what I can tell...
    Part 1.
    In the code below at this line of code "ActiveWorkbook.Queries.Add Name:="Fake Test Data 1", Formula:=" the sub needs to pass the user selected file name from the file picker and replace "Fake Test Data 1" with the name of the user selected file, the file suffix is NOT REQUIRED.

    Part 2.
    In the code below at this line of code "Source = Csv.Document(File.Contents(""C:\Test File\Fake Test Data 1.txt"")," the sub needs to pass the full path and file name of the user selected file with suffix from the file picker.

    Part 3.
    In the code below at this line of code ".CommandText = Array("SELECT * FROM [Fake Test Data 1]")" ' the sub needs to pass the user selected file name from the file picker and replace "Media Pro Tools NEW" with the name of the user selected file, the file suffix is NOT REQUIRED.

    Part 4.
    In the code below at this line of code "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Fake Test Data 1"";Extended Properties=""""" _ ' the sub needs to pass the user selected file name from the file picker and replace "Media Pro Tools NEW" with the name of the user selected file, the file suffix is NOT REQUIRED.

    Part 5.
    In the code below at this line of code "#.ListObject.DisplayName = "Fake Test Data 1"" 'the sub needs to pass the user selected file name from the file picker and replace "Media Pro Tools NEW" with the name of the user selected file, the file suffix is NOT REQUIRED and any SPACES in the file name must be replaced with a Underscore "_".

    Part 6.
    In the code below at this line of code "ActiveSheet.ListObjects("Fake_Test_Data_1").Unlist" 'the sub needs to pass the user selected file name from the file picker and replace "Media Pro Tools NEW" with the name of the user selected file, the file suffix is NOT REQUIRED and any SPACES in the file name must be replaced with a Underscore "_".

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,883

    Re: Power Query Experts Add file picker and pass data to the query issue

    All you really need is a parameter query with a named range for the file path. Then all your code needs to do is updated the named range with the new file path and simply refresh the existing query.

  7. #7
    Forum Contributor
    Join Date
    01-31-2015
    Location
    East Midlands
    MS-Off Ver
    Excel 2019
    Posts
    132

    Re: Power Query Experts Add file picker and pass data to the query issue

    Quote Originally Posted by romperstomper View Post
    All you really need is a parameter query with a named range for the file path. Then all your code needs to do is updated the named range with the new file path and simply refresh the existing query.
    Thank romperstomper

    and is this suitable for using with a VBA file picker?

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,883

    Re: Power Query Experts Add file picker and pass data to the query issue

    Yes - it doesn't matter how the value of the named range is changed, whether by code or manually.

  9. #9
    Forum Contributor
    Join Date
    01-31-2015
    Location
    East Midlands
    MS-Off Ver
    Excel 2019
    Posts
    132

    Re: Power Query Experts Add file picker and pass data to the query issue

    Quote Originally Posted by romperstomper View Post
    All you really need is a parameter query with a named range for the file path. Then all your code needs to do is updated the named range with the new file path and simply refresh the existing query.
    Ah ok. I have spent a couple of hours researching your suggestion as I had no idea what it meant, like I said, PQ is not my area so its all new to me.


    From what little I have been able to find, I do not think that this is good for me.
    The text files will all be named differently each time and may well be stored in a different location so the flexability of using a file picker is why I am going down that route.

    Thanks anyway.

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,883

    Re: Power Query Experts Add file picker and pass data to the query issue

    I don't think you understood what I was suggesting. The file picker allows you to pick a file, then that file path is assigned to the named range. The query then uses whatever value is in that named range as the source path. I do this with pretty much every query that uses external data precisely to make it easier to adjust things when IT inevitably move folders/servers or remap drives. So you change the file path, refresh the query, and it picks up the new data.

  11. #11
    Forum Contributor
    Join Date
    01-31-2015
    Location
    East Midlands
    MS-Off Ver
    Excel 2019
    Posts
    132

    Re: Power Query Experts Add file picker and pass data to the query issue

    And that is EXACTLY what I am trying to achieve.

    My whole OP is how to get the VBA file picker to then pass on the various bits of data the the query requires.

    Are you talking about setting up a table into which a user must 1. find the path to the file, 2. update a cell in the table with the file path?
    If so, that is not what I am talking about and why your replys were confusing me.

    This is a file picker which is in the 2nd code snip down in my OP.

    Please Login or Register  to view this content.
    This is giving me a headache.
    Last edited by AliGW; 07-24-2024 at 03:51 AM. Reason: Please don't quote unnecessarily - use the Quick Reply button instead.

  12. #12
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 VersionVersion 2502 Win 11
    Posts
    24,522

    Re: Power Query Experts Add file picker and pass data to the query issue

    Look at this link on ways to build your parameter query.

    https://exceloffthegrid.com/power-qu...ng-parameters/
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  13. #13
    Forum Contributor
    Join Date
    01-31-2015
    Location
    East Midlands
    MS-Off Ver
    Excel 2019
    Posts
    132

    Re: Power Query Experts Add file picker and pass data to the query issue

    Hi Alan and thank you but I do not want to use a parameter query as that adds an extra step for users and some may not know how to correctly get the file path.

    A file picker is a GUI that everyone would be used to using and is more eligent.
    Last edited by AliGW; 07-24-2024 at 03:51 AM. Reason: Please don't quote unnecessarily - use the Quick Reply button instead.

  14. #14
    Forum Contributor
    Join Date
    01-31-2015
    Location
    East Midlands
    MS-Off Ver
    Excel 2019
    Posts
    132

    Re: Power Query Experts Add file picker and pass data to the query issue

    So I have made some progress.

    The code that I have added now seems to pass the file name path directly to the query but I am now getting a the error...
    Run-time error '-2147024809 (80070057) The query name 'Fake Test Data 1.txt' contains characters that are not valid.

    Can anyone suggest a cure?

    Please Login or Register  to view this content.

  15. #15
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,883

    Re: Power Query Experts Add file picker and pass data to the query issue

    Quote Originally Posted by Belinea2010 View Post
    that adds an extra step for users and some may not know how to correctly get the file path.

    A file picker is a GUI that everyone would be used to using and is more eligent.
    No, it doesn't add an extra step. You use the file picker to update the cell that is used for the parameter. It's exactly the same number of steps for the user (click a button) and a lot less code.

  16. #16
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,883

    Re: Power Query Experts Add file picker and pass data to the query issue

    Anyway, you could do something like this:

    Please Login or Register  to view this content.

  17. #17
    Forum Contributor
    Join Date
    01-31-2015
    Location
    East Midlands
    MS-Off Ver
    Excel 2019
    Posts
    132

    Re: Power Query Experts Add file picker and pass data to the query issue

    Quote Originally Posted by romperstomper View Post
    Anyway, you could do something like this:

    Hi romperstomper

    Thank you for your suggestion but the code gives the Run-time error '1004': [Expression.Error] The import Csv.DocumentFile.Contents matches no exports. Did you miss a module reference?
    In code line ".Refresh BackgroundQuery:=False ' Refresh the query without running in the background"

    Please Login or Register  to view this content.


    Quote Originally Posted by romperstomper View Post
    No, it doesn't add an extra step. You use the file picker to update the cell that is used for the parameter. It's exactly the same number of steps for the user (click a button) and a lot less code.
    I have gave this some thought over night and I apoligise if I can across as argumentitive which I know that I can do but I did not mean it.
    When I have spent so long on something I kinda get tunnel vision as I have not being able to do finish something that I have started.

    Maybe PQ is a step to far for me right right now.

    If I may ask.

    So I could have a marcro with a file picker, the user selects what ever file, and the file picker passes the file path, as a an example, "C:\Some Folder\Some File.txt to the field on the Paramter query table and would there be code in the macro that then runs / refress the query based on the new file regardless of ean file being on any path or having any name, as long as the data within that file is in the same format / same named columns and so on.

    Would this work if the worksheet with the parameter table was hidden?

  18. #18
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 VersionVersion 2502 Win 11
    Posts
    24,522

    Re: Power Query Experts Add file picker and pass data to the query issue

    The best way to find out is to test it. I cannot imagine it not working. Afterall, the parameter is a table that is linked to the PQE and represented in your main query. So logic says, "YES!"

  19. #19
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,883

    Re: Power Query Experts Add file picker and pass data to the query issue

    Quote Originally Posted by Belinea2010 View Post
    Thank you for your suggestion but the code gives the Run-time error '1004': [Expression.Error] The import Csv.DocumentFile.Contents matches no exports. Did you miss a module reference?
    That's because your code is not my code. You deleted a bracket after Csv.Document

  20. #20
    Forum Contributor
    Join Date
    01-31-2015
    Location
    East Midlands
    MS-Off Ver
    Excel 2019
    Posts
    132

    Re: Power Query Experts Add file picker and pass data to the query issue

    Thank you everyone for your replies.

    I am going to mark this thread as solved so I can post in a different place as I am getting no where here
    and do not want to risk someone being over zealous and accusing me of cross posting.

    For the record, it is perfectly acceptable to ask the same question in a different place if the Op closes
    the original question due to not receiving the help they seek.

    I apprecate everyones time but on the other hand I do not have weeks to sort this issue.

    It is taking up an inordinate amount of time and it is impacting my family time.

    For what it is worth, this is not a work project and I am not a student seeking to have my assignments
    completed for me.

    I have researched to the limits of my extremly modest knowledge and abilitys and I came here seeking
    help but I found none.

    I did exactly what the forum rules ask for, I created a example workbook and provided test data so
    what more was expected of me I do not know.

    I have been a member here since 2015 and this forum used to be a friendly helpful community where
    people gave real straight answers insted of vague hints. It is a shame, it really is.

    If I knew how to do something then I would not be here asking for help.

  21. #21
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,883

    Re: Power Query Experts Add file picker and pass data to the query issue

    You should try reading the rules. They do not say that you can't cross-post.

    I'm not sure how you think that providing working code is "giving vague hints" or how you can claim that you got no help here, but good luck elsewhere.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Power Query Run Query on New File Monthly
    By knarf in forum Excel General
    Replies: 5
    Last Post: 11-15-2023, 12:43 PM
  2. [SOLVED] Why is my Excel file so big even though I uploaded the data using Power Pivot/Power Query?
    By Olivia Ludwig in forum Excel Charting & Pivots
    Replies: 12
    Last Post: 09-21-2023, 05:18 AM
  3. Updating a central file using power query question/issue
    By Tradesman in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 09-20-2023, 11:55 AM
  4. Replies: 1
    Last Post: 07-14-2023, 10:15 AM
  5. Replies: 8
    Last Post: 07-23-2020, 03:09 AM
  6. Excel Power Query Refresh or Access Query - 2nd Query Run is faster
    By Steveapa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-03-2020, 10:16 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1