Results 1 to 3 of 3
  1. #1
    ittechguy is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Aug 2015
    Posts
    218

    How to Query multiple tables and see unrelated results

    Not sure if I have a database design flaw or SQL so sorry if I'm in the wrong section.



    I have a union query which combines two queries: One that searches tblCustomer and tblBuilding and displays all information for customers who are a facility manager for a building, and another query which searches tblCustomer and tblRooms and displays all information for customers who are points of contact for a room. The SQL is below.

    Code:
    SELECTtblcustomer.lastname,
    tblcustomer.firstname,
    tblcustomer.organizationfk,
    tblcustomer.shopnamefk,
    tblcustomer.officesymfk,
    tblfacilitymgr.customerfk,
    tblfacilitymgr.buildingfk,
    tblrooms.roomspk
    FROM(tblbuilding
    INNERJOINtblrooms
    ONtblbuilding.buildingpk=tblrooms.buildingfk)
    INNERJOIN(tblcustomer
    INNERJOINtblfacilitymgr
    ONtblcustomer.customerpk=
    tblfacilitymgr.customerfk)
    ONtblbuilding.buildingpk=tblfacilitymgr.buildingfk
    UNION
    SELECTtblcustomer.lastname,
    tblcustomer.firstname,
    tblcustomer.organizationfk,
    tblcustomer.shopnamefk,
    tblcustomer.officesymfk,
    tblroomspoc.customerfk,
    tblrooms.buildingfk,
    tblroomspoc.roomsfk
    FROMtblrooms
    INNERJOIN(tblcustomer
    INNERJOINtblroomspoc
    ONtblcustomer.customerpk=tblroomspoc.customerfk)
    ONtblrooms.roomspk=tblroomspoc.roomsfk;
    Here is my table relationships:
    Click image for larger version. 

Name:	relationship2.0.png 
Views:	10 
Size:	46.8 KB 
ID:	22322

    I use the union query as the recordsource for a form and then I filter the form using several search boxes. Organization, Shop Name, Office Symbol, Last Name, First Name, Building Name, and Room Name.

    All this works great. However I also need to be able to search by equipment information (such as Brand, Type, Name, Serial Number, IP address, etc). So I expanded a union query to look like this:

    Code:
     SELECT tblcustomer.organizationfk,      
           tblcustomer.shopnamefk,
           tblcustomer.officesymfk,
           tblcustomer.lastname,
           tblcustomer.firstname,
           tblfacilitymgr.buildingfk,
           tblrooms.roomspk,
           tblbuilding.buildingname,
           tblrooms.roomname,
           tblcabinet.cabinetname,
           tblequipment.cabinetfk,
           tblequipment.equipmentnamefk,
           tblequipment.equipmentbrandfk,
           tblequipment.equipmentnetworktypefk
    FROM   ((tblbuilding
             INNER JOIN tblrooms
                     ON tblbuilding.buildingpk = tblrooms.buildingfk)
            INNER JOIN (tblcustomer
                        INNER JOIN tblfacilitymgr
                                ON tblcustomer.customerpk =
                                   tblfacilitymgr.customerfk)
                    ON tblbuilding.buildingpk = tblfacilitymgr.buildingfk)
           INNER JOIN (tblcabinet
                       INNER JOIN tblequipment
                               ON tblcabinet.cabinetpk = tblequipment.cabinetfk)
                   ON tblrooms.roomspk = tblcabinet.roomsfk
    UNION
    SELECT tblcustomer.organizationfk,
           tblcustomer.shopnamefk,
           tblcustomer.officesymfk,
           tblcustomer.lastname,
           tblcustomer.firstname,
           tblrooms.buildingfk,
           tblrooms.roomspk,
           tblbuilding.buildingname,
           tblrooms.roomname,
           tblcabinet.cabinetname,
           tblequipment.cabinetfk,
           tblequipment.equipmentnamefk,
           tblequipment.equipmentbrandfk,
           tblequipment.equipmentnetworktypefk
    FROM   ((tblbuilding
             INNER JOIN tblrooms
                     ON tblbuilding.buildingpk = tblrooms.buildingfk)
            INNER JOIN (tblcabinet
                        INNER JOIN tblequipment
                                ON tblcabinet.cabinetpk = tblequipment.cabinetfk)
                    ON tblrooms.roomspk = tblcabinet.roomsfk)
           INNER JOIN (tblcustomer
                       INNER JOIN tblroomspoc
                               ON tblcustomer.customerpk = tblroomspoc.customerfk)
                   ON tblrooms.roomspk = tblroomspoc.roomsfk;
    This shows the following results:
    Click image for larger version. 

Name:	newquery.jpg 
Views:	10 
Size:	86.7 KB 
ID:	22323

    The issue I'm having is that the only way this will show a customer and the buildings/rooms they're assigned to is if I have a value in tblEquipment.CabinetFK. This cannot work because not all rooms have equipment. I need to be able to search for a name, building, room, etc and see records even if there is no equipment assigned to that room. I also need to be able to search for equipment that IS assigned to a room (or more accurately, a cabinet which is assigned to a room).

    As it is now, there are many rooms in my example database which have customers assigned to them, but they do not have any cabinets in them (yet) and thus they do not have any equipment in them. My first query would display all information, but my second only displays information if the equipment is assigned to a room. I need to be able to see all records for my search whether there is a cabinet in a room or not, and whether there is equipment in a cabinet or not. BUT, I also need to be able to search by equipment which is in a cabinet.

    I hope I've been clear enough on what I need to do. What can I do to fix this?
    Last edited by ittechguy; 10-07-2015 at 09:46 PM.

  2. #2
    ittechguy is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Aug 2015
    Posts
    218
    Is it possible perhaps I just need to change my query? Perhaps using a left/right join instead of inner join? I do not yet know enough about SQL to know how to do this, but I'm doing all the research I can do.

  3. #3
    ittechguy is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Aug 2015
    Posts
    218
    Figured it out! All I had to do was change the join from tblCabinets to tblEquipment from INNER JOIN to LEFT JOIN.

    Code:
    SELECT tblcustomer.organizationfk,             tblcustomer.shopnamefk,
           tblcustomer.officesymfk,
           tblcustomer.lastname,
           tblcustomer.firstname,
           tblfacilitymgr.buildingfk,
           tblrooms.roomspk,
           tblbuilding.buildingname,
           tblrooms.roomname,
           tblcabinet.cabinetname,
           tblequipment.cabinetfk,
           tblequipment.equipmentnamefk,
           tblequipment.equipmentbrandfk,
           tblequipment.equipmentnetworktypefk
    FROM   ((tblbuilding
             INNER JOIN tblrooms
                     ON tblbuilding.buildingpk = tblrooms.buildingfk)
            INNER JOIN (tblcustomer
                        INNER JOIN tblfacilitymgr
                                ON tblcustomer.customerpk =
                                   tblfacilitymgr.customerfk)
                    ON tblbuilding.buildingpk = tblfacilitymgr.buildingfk)
           INNER JOIN (tblcabinet
                       INNER JOIN tblequipment
                               ON tblcabinet.cabinetpk = tblequipment.cabinetfk)
                   ON tblrooms.roomspk = tblcabinet.roomsfk
    UNION
    SELECT tblcustomer.organizationfk,
           tblcustomer.shopnamefk,
           tblcustomer.officesymfk,
           tblcustomer.lastname,
           tblcustomer.firstname,
           tblrooms.buildingfk,
           tblrooms.roomspk,
           tblbuilding.buildingname,
           tblrooms.roomname,
           tblcabinet.cabinetname,
           tblequipment.cabinetfk,
           tblequipment.equipmentnamefk,
           tblequipment.equipmentbrandfk,
           tblequipment.equipmentnetworktypefk
    FROM   ((tblbuilding
             INNER JOIN tblrooms
                     ON tblbuilding.buildingpk = tblrooms.buildingfk)
            LEFT JOIN (tblcabinet
                        LEFT JOIN tblequipment
                                ON tblcabinet.cabinetpk = tblequipment.cabinetfk)
                    ON tblrooms.roomspk = tblcabinet.roomsfk)
           INNER JOIN (tblcustomer
                       INNER JOIN tblroomspoc
                               ON tblcustomer.customerpk = tblroomspoc.customerfk)                ON tblrooms.roomspk = tblroomspoc.roomsfk;

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

Similar Threads

  1. Replies: 3
    Last Post: 02-02-2015, 11:32 AM
  2. Replies: 3
    Last Post: 07-31-2014, 07:23 AM
  3. Multiple Tables, Multiple Filters + Search
    By michael.legge@rbc.com in forum Access
    Replies: 1
    Last Post: 05-09-2013, 03:15 PM
  4. Search Across Multiple Tables
    By Tomfernandez1 in forum Access
    Replies: 8
    Last Post: 02-25-2011, 09:11 PM
  5. How can I search multiple tables?
    By botts121 in forum Access
    Replies: 4
    Last Post: 02-02-2010, 06:39 AM

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