Results 1 to 5 of 5
  1. #1
    jhollingsh615 is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Apr 2011
    Posts
    30

    Query with many-to-many relationship

    I'm fairly new to A2007 and still have difficulty with joins. I have the following tables in a many-to-many relationship:


    Companies: PK-CompanyID
    Contacts: PK-Contact ID
    CompanyContacts: FK-CompanyID, FK-ContactID and PK-ContactsCompanies

    It works fine in my main form where I need to show each of multiple companies at which a single contact works. Master-child tables set up fine.

    But, now I need to also provide a way to do the reverse. I need to make a query and report in which every single company is listed, along with each of the multiple contacts attached to that company. I'm assuming I need a left outer join. But, I'm at a loss how to do it. Hoping it's nothing more than Monday mental fuzziness and someone can point me toward an explanation suitable for a newbie.....Thanks in advance.

  2. #2
    NTC is offline VIP
    Windows Vista Access 2007
    Join Date
    Nov 2009
    Posts
    2,392
    What happens when you make a query that has the Companies table joined to the CompanyContacts table?

  3. #3
    jhollingsh615 is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Apr 2011
    Posts
    30
    A query with just tblCompanies and tblCompanyContacts gives me what I guess is called the cartesian results. Every single company is listed, repeated for each record that is found in tblContacts. If there are 4 contacts at a company, the company's listing appears 4 times.




    Quote Originally Posted by NTC View Post
    What happens when you make a query that has the Companies table joined to the CompanyContacts table?

  4. #4
    NTC is offline VIP
    Windows Vista Access 2007
    Join Date
    Nov 2009
    Posts
    2,392
    well - its not clear to me whether this is a bad thing. assuming the join is correct you want to see every contact by company...and this displays that...yes? is it returning invalid results? I am thinking not......

    in terms of display you may not want to display that same company info over and over - and that is easily removed at the query level by going to an AggregateQuery with a group-on.....or you can group using Reports....

    actually you probably can get what you want with a report/form object simply by making the main form/report based on Companies table and inserting a sub form/report based on the other tables....another approach...

  5. #5
    jhollingsh615 is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Apr 2011
    Posts
    30
    Actually, NTC, I am getting weird data results when I connect them. When do a form/subform with companies/contacts, not all of the contacts appear in the subform. For some companies, all is fine. For other companies, some contacts are missing. For example, one company has 4 contacts listed in the junction table. But, only two of those contacts are appearing in the subform. It had me fooled for a second until I noticed the missing contacts.

    I have tried basing the forms on both forms and directly on the tables. The same missing results. Would that mean I have a problem in the relationships or where might it be screwing up? Thanks for the help......


    Quote Originally Posted by NTC View Post
    well - its not clear to me whether this is a bad thing. assuming the join is correct you want to see every contact by company...and this displays that...yes? is it returning invalid results? I am thinking not......

    in terms of display you may not want to display that same company info over and over - and that is easily removed at the query level by going to an AggregateQuery with a group-on.....or you can group using Reports....

    actually you probably can get what you want with a report/form object simply by making the main form/report based on Companies table and inserting a sub form/report based on the other tables....another approach...

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

Similar Threads

  1. Relationship
    By ClownKiller in forum Database Design
    Replies: 13
    Last Post: 12-21-2010, 05:49 PM
  2. One to one relationship
    By ManvinderKaur in forum Database Design
    Replies: 2
    Last Post: 06-28-2010, 05:37 PM
  3. one to many relationship?
    By cowboy in forum Access
    Replies: 3
    Last Post: 06-16-2010, 02:37 PM
  4. Query including Null relationship?
    By David Criniti in forum Database Design
    Replies: 0
    Last Post: 08-14-2009, 09:10 PM
  5. Relationship
    By pcandns in forum Access
    Replies: 2
    Last Post: 04-01-2009, 09:32 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