Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    taholmes160 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Location
    Lester, Ohio
    Posts
    67

    Removing Items from a table that have no data in another table


    HI Folks:

    I am working on a medium size database (72,000+ records) in the particular table I am listing there are over 11,000 different commodities listed, as you can see in the attached pic, some have companies linked, others have no companies linked. I would like to find all the commodities that have no company linked and delete them. I can do it by hand, but its a bit tedious, so I am hoping for a "automated" solution. The first image below is a listing of the commodities table, the second picture shows the relationships that are established in the database. The data from this database will be exported as Excel files for use in another application.

    Click image for larger version. 

Name:	Access Question.jpg 
Views:	24 
Size:	137.3 KB 
ID:	40338

    Click image for larger version. 

Name:	access question 2.jpg 
Views:	24 
Size:	75.8 KB 
ID:	40339

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    A quick question -- How and Why are the commodities with no associated Companies did these records get added??
    What you ask can probably be done, but you should have a "business rationale" for deleting any records.

  3. #3
    taholmes160 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Location
    Lester, Ohio
    Posts
    67
    This database has not had data maintenance in a long time, and what has been done was very poorly done, so in many cases, there were several commodities that were the same thing but with different spelling -- for example

    agricultural equipment
    Agricultural Equipment
    ag eq
    ag equip
    Ag Imp
    ag implements
    etc etc etc

    im trying to get things cleaned up and working

    TIM

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I think many here can relate to your situation. Something you may consider is to use a standard, authoritative table to identify your commodities. The idea is to have uniquely identified commodities, and to use their unique identifier in related tables, and making that commodities table a lookup table. This approach can standardize and streamline your maintenance - and reduce variations in spelling for the same commodity.
    Good luck with your project.

  5. #5
    taholmes160 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Location
    Lester, Ohio
    Posts
    67

    Table

    Quote Originally Posted by orange View Post
    I think many here can relate to your situation. Something you may consider is to use a standard, authoritative table to identify your commodities. The idea is to have uniquely identified commodities, and to use their unique identifier in related tables, and making that commodities table a lookup table. This approach can standardize and streamline your maintenance - and reduce variations in spelling for the same commodity.
    Good luck with your project.
    Hey Orange, thanks for the advice, thats exactly what I am working on doing, trying to get the commodities table cleaned up -- so far, Im doing it by hand and using update queries -- there are about 10000 commodities, and im up into the 200's I was able to select all of the ones with null companies, but I cannot get them to delete, so I'll keep working by hand -- theres no timeline on this project

    Thanks a bunch

    TIM

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Post query SQL for selecting commodities.

    Delete action might be:

    DELETE FROM tblCommodities WHERE CommodityID IN (SELECT CommodityID FROM tblCommodities LEFT JOIN tblCompanies ON tblCommodities.CommodityID=tblCompanies.CommodityI D WHERE CompanyID Is Null);
    Last edited by June7; 12-01-2019 at 01:58 AM.
    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.

  7. #7
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,979
    Or a simpler version of the same idea with no subquery

    Code:
    DELETE DISTINCTROW tblCommodities.* 
    FROM tblCompanies RIGHT JOIN tblCommodities ON tblCompanies.CommodityID = tblCommodities.CommodityID
    WHERE tblCompanies.ConCompany Is Null;
    Once you have deleted all orphaned records, you should establish referential integrity between tables so that no orphans will occur in future
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  8. #8
    taholmes160 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Location
    Lester, Ohio
    Posts
    67

    SQL Code

    Quote Originally Posted by June7 View Post
    Post query SQL for selecting commodities.

    Delete action might be:

    DELETE FROM tblCommodities WHERE CommodityID IN (SELECT CommodityID FROM tblCommodities LEFT JOIN tblCompanies ON tblCommodities.CommodityID=tblCompanies.CommodityI D WHERE CompanyID Is Null);

    Heres the code for the current select query --

    SELECT TblCommodities.CommodityID, TblCommodities.Commodity
    FROM TblCommodities LEFT JOIN TblCompanies ON TblCommodities.[CommodityID] = TblCompanies.[CommodityID]
    WHERE (((TblCompanies.CommodityID) Is Null));


    Im headed off to work momentarially, so I'll give these code options a try when I get a few minutes later today or tomorrow - THANK YOU!!!!

    TIM

  9. #9
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,979
    Your select query is checking for unmatched CommodityID. Although that may be useful, it isn't what you asked for originally - you said you wanted commodityID values in both tables wth no ConCompany record - in other words where the Subdatasheet is blank
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  10. #10
    taholmes160 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Location
    Lester, Ohio
    Posts
    67
    Quote Originally Posted by isladogs View Post
    Your select query is checking for unmatched CommodityID. Although that may be useful, it isn't what you asked for originally - you said you wanted commodityID values in both tables wth no ConCompany record - in other words where the Subdatasheet is blank

    Yep, you are right, I tried using the query builder and looks like it backfired -- SQL is not my strong suite!

    Thanks for the help

    TIM

  11. #11
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,979
    Have you tried the code I suggested?
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  12. #12
    taholmes160 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Location
    Lester, Ohio
    Posts
    67
    Thanks guys, that worked -- cutting my workload down considerably -- ive gotten 500 of the now 9000 records done -- I'll keep working on it

    TIM

  13. #13
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,979
    Glad to see its working for you but I'm slightly confused.
    If you're using my code, it should delete all the unwanted records at once - not just 500 of them
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  14. #14
    taholmes160 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Location
    Lester, Ohio
    Posts
    67

    It worked

    Quote Originally Posted by isladogs View Post
    Have you tried the code I suggested?
    Yep, Sure did -- worked a treat -- thanks

    TIM

  15. #15
    taholmes160 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Location
    Lester, Ohio
    Posts
    67
    Hey colin, I miss spoke, it deleted all 1700+ of the unwanted records, now I am working through the remaining 9000+ of the remaining records. Cleaning them up, consolidating records etc. I keep re-using the sql after I fix a bunch to get rid of the newly empty ones -- its a bit time consuming, but thankfully theres no real rush, and hopefully, I'll get things done in the next couple weeks

    TIM

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 11
    Last Post: 03-17-2015, 05:10 PM
  2. Replies: 3
    Last Post: 07-29-2011, 09:30 AM
  3. Replies: 6
    Last Post: 07-10-2011, 05:33 PM
  4. Replies: 1
    Last Post: 06-08-2011, 04:43 AM
  5. Highlighting Items in List Box from Table Data
    By swalsh84 in forum Programming
    Replies: 2
    Last Post: 01-25-2010, 08:55 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