Results 1 to 8 of 8
  1. #1
    Peterz7 is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    6

    Two tables, many to many relationship, append fields

    Hi everyone. Relatively new Access user, first time poster.


    I have two tables with a many to many common field that I'm linking and want to append two unique fields from the second to the first. However, when create a query, what is happening is that my original table ends up containing duplicate records in the query result set. Obviously my simple JOIN is not doing what I want for it to do.

    I've attached a sample database of my issue. TableA contains financial records, TableB contains two fields I'd like to add to my result set. I've included a TableCDesiredResult table to show what I'm expecting to happen. By running Query1 you will see that while the 2 fields and values I'd like to append are added, my expected record count goes from 29 to 43. My production database contains over 500k records so this really becomes a problem.

    I believe I need a more complex or proper JOIN to accomplish this. Can anyone provide any suggestions or guidance on how I can move forward?
    Thanks so much for any and all suggestions.
    Regards,
    Peter
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    A many-to-many relationship requires a third table - a 'junction' table - that associates records of the other two tables. Form design options are:

    1. single form with comboboxes to select record from each of the other tables

    2. main form bound to table 1 and subform bound to junction table with combobox to select record from table 2

    3. main form bound to table 2 and subform bound to junction table with combobox to select record from table 1
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Never repeat data and all tables to contain one subject only. There are fields repeated all over the place. You need a table where rx_number is unique, a table of products with the productID only carried to other tables, a delivery method table, a last name table. Then a transaction table(s) to join them all together. See here for some really good advice on learning how to create a database (bottom of post #7): https://www.accessforums.net/showthread.php?t=65906

  4. #4
    Peterz7 is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    6
    Thanks for your suggestions. I will look into junction tables and how that might solve my issue.

  5. #5
    Peterz7 is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    6
    Thanks for replying. Unfortunately, these are not tables I created, but rather canned data extracts from a national, off-the-shelf pharmacy software package my firm uses. Their application does not supply all the analytics management would like to see, so I utilize three separate data extracts from the application, that I then import into an MS Access database and run queries and produce reports. I'm limited to these files at this point.
    Appreciate your willingness to help.

  6. #6
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    The records are duplicated due to the non-unique field rx_number. For instance, tbl A has two records with 2212436. So does tbl B. So there are 4 records:
    tblA record 1/tblB record 1
    tblA record 1/tblB record 2
    tblA record 2/tblB record 1
    tblA record 2/tblB record 2

  7. #7
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Add the word DISTINCT to your query:
    SELECT DISTINCT ....
    Replace the "*" by all the field names.

  8. #8
    Peterz7 is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    6
    This worked! Thank you so much for your help. It works perfectly in the simple sample I provided and gives me encouragement to put in the work to try it in our much more complex and data-laden production environment.
    You're awesome! Thanks.

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

Similar Threads

  1. Replies: 5
    Last Post: 11-26-2013, 11:11 PM
  2. Replies: 24
    Last Post: 02-20-2013, 12:49 PM
  3. RelationShip between Tables
    By joe357 in forum Forms
    Replies: 3
    Last Post: 08-20-2012, 06:01 PM
  4. Replies: 6
    Last Post: 03-09-2012, 01:07 PM
  5. Relationship between tables
    By kpk in forum Database Design
    Replies: 3
    Last Post: 10-14-2011, 11:49 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