Results 1 to 6 of 6
  1. #1
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038

    Renaming table names does NOT update UNION query (table reference)


    Hello Experts:

    I need some assistance with *tweaking* a UNION query so that a change of table name will be reflected in the UNION query.

    Background:
    - In this example, I have two (2) tables named [complete_LK_IMPACT] and [complete_LK_INJURY_TYPE]
    - Also, I have 2 individual/separate queries as shown below:

    Code:
    SELECT "LK_IMPACT" AS LK_IMPACT, complete_LK_IMPACT.TARGET_TABLE, complete_LK_IMPACT.TARGET_FIELD
    FROM complete_LK_IMPACT
    GROUP BY "LK_IMPACT", complete_LK_IMPACT.TARGET_TABLE, complete_LK_IMPACT.TARGET_FIELD;
    Code:
    SELECT "INJURY_TYPE" AS INJURY_TYPE, complete_LK_INJURY_TYPE.TARGET_TABLE, complete_LK_INJURY_TYPE.TARGET_FIELD
    FROM complete_LK_INJURY_TYPE
    GROUP BY "INJURY_TYPE", complete_LK_INJURY_TYPE.TARGET_TABLE, complete_LK_INJURY_TYPE.TARGET_FIELD;
    - Next, I also have a UNION query. The SQL is shown below:

    Code:
    SELECT "LK_IMPACT" AS LK_IMPACT, complete_LK_IMPACT.TARGET_TABLE, complete_LK_IMPACT.TARGET_FIELD
    FROM complete_LK_IMPACT
    GROUP BY "LK_IMPACT", complete_LK_IMPACT.TARGET_TABLE, complete_LK_IMPACT.TARGET_FIELD;
    
    UNION 
    
    SELECT "INJURY_TYPE" AS INJURY_TYPE, complete_LK_INJURY_TYPE.TARGET_TABLE, complete_LK_INJURY_TYPE.TARGET_FIELD
    FROM complete_LK_INJURY_TYPE
    GROUP BY "INJURY_TYPE", complete_LK_INJURY_TYPE.TARGET_TABLE, complete_LK_INJURY_TYPE.TARGET_FIELD;
    Current problem:
    - When I change the table names from [complete_LK_IMPACT] AND [complete_LK_INJURY_TYPE] to [LK_IMPACT] AND [LK_INJURY_TYPE], the 2 individual queries are automatically updated and continue to execute.
    - However, the UNION query does not reflect the new table name and does NOT execute any longer.

    My question:
    - How should I modify the UNION query so that any renaming of the table names will also be updated in the UNION query?
    Attached Thumbnails Attached Thumbnails 2 Individual Queries.JPG  

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Hi Tom,

    Not sure if you already figured it out as the post is marked as Solved but you can use the SQL property of the QueryDef object to edit it:

    Code:
    Dim sSQL as string, qdf as DAO.QueryDef
    Set qdf=CurrentDb.QueryDefs("qryYourUnionQuery")
    sSQL =qdf.SQL 'retrieve the existing SQL
    sSQL=Replace(sSQL,"complete_","") 'update the SQL
    qdf.SQL=sSQL 'save it to the query
    Set qdf=Nothing
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    Vlad -- wow... that worked perfectly!! I added to code to my renaming function and voila... all is working incl. the UNION query. Sweet!

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114

    Glad to hear Tom!
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Wouldn't
    Code:
    Select * from Query1
    Union
    Select * from Query2
    worked just as well?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    No, it would not.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Renaming Table based on Query Results
    By Oxygen Potassium in forum Access
    Replies: 6
    Last Post: 10-11-2019, 01:14 PM
  2. Query to update a field from a reference table
    By PCartland in forum Queries
    Replies: 8
    Last Post: 02-03-2017, 02:29 PM
  3. Renaming Files using data from a table or query
    By enzokevin in forum Programming
    Replies: 7
    Last Post: 10-17-2014, 05:42 AM
  4. how to update in a table union all query
    By learning_graccess in forum Queries
    Replies: 4
    Last Post: 03-31-2012, 07:32 AM
  5. Renaming Fields on Table or Query
    By jo15765 in forum Access
    Replies: 4
    Last Post: 12-09-2010, 01:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums