+ Reply to Thread
Results 1 to 14 of 14

create a query to filter on a field on all tables

  1. #1
    Registered User
    Join Date
    10-13-2012
    Location
    newcastle
    MS-Off Ver
    Excel 2007
    Posts
    68

    create a query to filter on a field on all tables

    Hello,

    Access 2007

    I would like to query each table in my database, on a specific criteria in field 1. The criteria is that the end of the string is .RAMB. The resultant query would should show F1 and F3 from each table in the database.

    There are lots of tables - so it would be good if it would just look at all tables in the database, rather than specifying all.

    How would i do this?

    Regards,
    Last edited by squirrely; 09-08-2013 at 03:43 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: create a query to filter on a field on all tables

    Your request is a bit confusing. Do you wish to query each table individually? Can the tables be joined on a common field? Are you looking to include or exclude those records ending with .RAMB? Which field (does it have a name?) contains this information? Are the tables all set up the same? Please provide more information so that we can help you.

    From the info provided, assuming you wish to include records ending in .RAMB from F1, then try this:
    Please Login or Register  to view this content.
    Last edited by alansidman; 09-08-2013 at 06:03 PM.
    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

  3. #3
    Registered User
    Join Date
    10-13-2012
    Location
    newcastle
    MS-Off Ver
    Excel 2007
    Posts
    68

    Re: create a query to filter on a field on all tables

    Hello, i ended up doing something very similar - i just used a UNION format
    The code i used is:

    SELECT [F1],[F2],[F3]
    FROM [2013-09-01]
    WHERE [F1] LIKE "*.RAMB"

    UNION ALL SELECT [F1],[F2],[F3]
    FROM [2013-09-02]
    WHERE [F1] LIKE "*.RAMB";

    However i dont really want to write FROM[yyyy-mm-dd] for every single date, is there a way to write FROM all tables instead?

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: create a query to filter on a field on all tables

    If you want to join all the tables, you will definitely have to id the table name in your SQL statement. When I need to edit SQL statements, I usually copy them to WordPad or NotePad and make the edit changes there and then copy it back to Access.

  5. #5
    Registered User
    Join Date
    10-13-2012
    Location
    newcastle
    MS-Off Ver
    Excel 2007
    Posts
    68

    Re: create a query to filter on a field on all tables

    Quote Originally Posted by alansidman View Post
    If you want to join all the tables, you will definitely have to id the table name in your SQL statement. When I need to edit SQL statements, I usually copy them to WordPad or NotePad and make the edit changes there and then copy it back to Access.
    Really, so everyday when the new .csv file is linked to the database i have to add to the code to account for the new date?

    I was really hoping there would be a 'blanket' bit of code which covers all tables.

    What if i were to create the sql code to cover dates way into the future so i dont have to add to the sql view everyday?
    Will there be a problem if the table doesnt exist? i.e will it crash when i run the code?

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: create a query to filter on a field on all tables

    If you are getting a new .csv file everyday that you want to add to your query then I think an easier way would be to
    1. Take your existing tables and do a Union Query one time.
    2. With the Union Query as your record source, do a make table query and save it. This is your new record source. Delete the old tables that were imported from .csv. Delete the Union Query.
    3. With each new .csv file you import, after importing append it to the new table you made and delete the imported .csv table.
    4. Now run your query for the fields you want with the criteria you want.
    5. Steps 3 and 4 can be automated with VBA so that all you need to do is click on a button and in an input box, identify the file to import.
    6. Most important step. Make sure you backup your db before you start this in case you make a mistake you can reconstruct.

    will it crash when i run the code?
    You will get an error message and the Query will not run.
    Alan
    Last edited by alansidman; 09-09-2013 at 07:27 PM.

  7. #7
    Registered User
    Join Date
    10-13-2012
    Location
    newcastle
    MS-Off Ver
    Excel 2007
    Posts
    68

    Re: create a query to filter on a field on all tables

    Quote Originally Posted by alansidman View Post
    If you are getting a new .csv file everyday that you want to add to your query then I think an easier way would be to
    1. Take your existing tables and do a Union Query one time.
    2. With the Union Query as your record source, do a make table query and save it. This is your new record source. Delete the old tables that were imported from .csv. Delete the Union Query.
    3. With each new .csv file you import, after importing append it to the new table you made and delete the imported .csv table.
    4. Now run your query for the fields you want with the criteria you want.
    5. Steps 3 and 4 can be automated with VBA so that all you need to do is click on a button and in an input box, identify the file to import.
    6. Most important step. Make sure you backup your db before you start this in case you make a mistake you can reconstruct.

    You will get an error message and the Query will not run.
    Alan

    Hi Thanks for the pointers.
    I am not actually importing any of the files. The .csv files sit in a folder and i link to them using this code:


    Please Login or Register  to view this content.


    Does this make any difference with what you suggested?
    Last edited by alansidman; 09-10-2013 at 08:49 AM.

  8. #8
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: create a query to filter on a field on all tables

    You could probably do the same. I have not done this with linked tables but there is no reason that you couldn't build a make table query and then append it with the data from the latest linked table.

    Make sure to do a backup before you attempt it. Post back with your successes or issues.

  9. #9
    Registered User
    Join Date
    10-13-2012
    Location
    newcastle
    MS-Off Ver
    Excel 2007
    Posts
    68

    Re: create a query to filter on a field on all tables

    Great i will have a go and report back

  10. #10
    Registered User
    Join Date
    10-13-2012
    Location
    newcastle
    MS-Off Ver
    Excel 2007
    Posts
    68

    Re: create a query to filter on a field on all tables

    I am completely stuck, I created the append query and run it however it only says two rows will be uploaded to the table, the same 2 that are in the union query. I am not sure what i am doing wrong. Any help?

  11. #11
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: create a query to filter on a field on all tables

    Did you create the new table with a make table query first? If so, what is your SQL statement for the append query to that table?

  12. #12
    Registered User
    Join Date
    10-13-2012
    Location
    newcastle
    MS-Off Ver
    Excel 2007
    Posts
    68

    Re: create a query to filter on a field on all tables

    Hi, I have given up on this and chose to pursue this in a different way. Instead of creating a table for each csv file in my desktop folder. I have now created a single link to all files in the folder. So now i have one table with all .csv data in. Which i think may be better?

    I now just use a where clause on the entire table to filter out what i want.

    The code i used to link is below for your reference:


    Please Login or Register  to view this content.
    Last edited by alansidman; 09-10-2013 at 01:49 PM. Reason: code tags added

  13. #13
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: create a query to filter on a field on all tables

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    I have added the code tags in this case, but in the future, you should add them yourself.
    Glad that you have resolved the issue.

    If this solved your issue, please mark your thread as solved.

  14. #14
    Registered User
    Join Date
    10-13-2012
    Location
    newcastle
    MS-Off Ver
    Excel 2007
    Posts
    68

    Re: create a query to filter on a field on all tables

    didnt know that, i will do that in the future. Thanks for your help, and i will mark this solved.

+ 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. Replies: 2
    Last Post: 02-01-2013, 04:21 PM
  2. [SOLVED] Loop through list, perform web query and save each query on its own page
    By anrichards22 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-07-2012, 07:50 AM
  3. Problem with selecting range with in query table after query refresh
    By shooter in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-31-2012, 11:55 AM
  4. Web Query - Change a word in query to form a new query
    By scottymelloty in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-25-2011, 04:13 AM
  5. Excel 2003/MS Query - Query Defn Lost
    By ksp1 in forum Excel General
    Replies: 0
    Last Post: 10-12-2005, 06:05 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