Results 1 to 8 of 8
  1. #1
    Markb384 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    44

    Using SQL to populate Row Source from Multiple Tables

    Hi guys.

    I have a selection of tables, each containing an equipment ID, a date and then some other fields specific to that table. Each table represents a different type of test that was performed.

    For a combobox I have on another form, I have some SQL code to populate the box with unique ID numbers using the format below, which works perfectly (particularly as I know that all the unique IDs are definitely unique due to other code elsewhere).
    Code:
    SELECT tableName.fieldName
    FROM tableName
    ORDER BY tableName.fieldName
    I know have another combobox, in which I want to do something similar. I want to pull out all of the dates from a variety of tables where another field equals a value (in this case, the ID of the equipment), hide duplicates such that each date only appears once and order in chronological order. I know there is SQL code for only retrieving dates which match the ID (using WHERE fieldName = textbox.value for instance) and I have seen something somewhere about hiding duplicates (although I can't remember it at this exact moment). The bit I am struggling with is the SELECT... FROM aspect of the code, as I want to search several tables.

    Anyone have any ideas on how to do this efficiently, without having to resort to VBA code to populate and sort arrays etc. (I've been down that route before for Excel and it was a nightmare...)



    Thanks in advance.

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Maybe incorporate GROUP BY? You could start by using the query builder and use the Totals option.

  3. #3
    Markb384 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    44
    Thank you ItsMe

    Unfortunately, I can't get my head around using the Group By function, as my reading around seems to imply it needs some sort of sum or count, and I don't want that.

    Just to clarify, my data is structured like the following...

    ID Date Some other fields

    and I have several tables of a similar structure, each with different "other fields" depending on the test.

    I want to populate a combobox with all of the dates from each table where the ID matches a user input, but without displaying each date in the list more than once. For instance, in one table, equipment ID A1 might have results on dates 1/1/11 and 2/1/11 and in another table A1 might have results for 2/1/11 and 3/1/11. I want my combobox to contain the dates 1/1/11, 2/1/11 and 3/1/11 only when the user selects A1 as the equipment ID.

    My current SQL code is
    Code:
    SELECT DISTINCT TableA.[SDate], TableB.[SDate], TableC.[SDate]FROM TableA, TableB, TableC;
    which doesn't work (obviously). I am unsure of how to use a WHERE when I am checking the criteria in several tables and ORDER BY as I am not clear on which part to ORDER BY

  4. #4
    Markb384 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    44
    Sorry to double post, but I have solved 99% of the problem using a UNION command, e.g.

    Code:
    SELECT DISTINCT TableA.[SDate]
    FROM TableA
    WHERE TableA.[Equip ID] = [Forms]![Form_Name]![Textbox_Name]
    UNION
    SELECT DISTINCT TableB.[SDate]
    FROM TableB
    WHERE TableB.[Equip ID] = [Forms]![Form_Name]![Textbox_Name]
    ;
    Now to try and get it in descending order rather than ascending...

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    In the query builder there is an option to add an ORDER BY statement. Select the ascending or descending option for the appropriate column then view the results in SQL view.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    A UNION query can't be shown in the query builder, so just add this at the end:

    ORDER BY SDate DESC
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Missed that, thanks Paul.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    No problem. You're only allowed one slip per month though...
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. One form to populate multiple tables
    By justair07 in forum Forms
    Replies: 5
    Last Post: 11-19-2013, 01:49 PM
  2. populate a field from multiple source fields using a combobox
    By tranquillity in forum Database Design
    Replies: 8
    Last Post: 09-13-2013, 06:46 PM
  3. Replies: 4
    Last Post: 01-14-2013, 09:35 PM
  4. Replies: 1
    Last Post: 10-28-2012, 07:23 PM
  5. Replies: 4
    Last Post: 01-20-2011, 10:05 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