Results 1 to 8 of 8
  1. #1
    Gauchey is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    4

    Query to delete across tables

    I think this is a pretty basic question. I am trying to delete records in two different tables. I get the error message:
    Specify the table containing the records you want to delete.


    The table MSShipment will be the reference source for the records I would like to delete across both tables.

    DELETE MSShipment.BoxNumber AS MSShipment_BoxNumber, MedicalSort.BoxNumber AS MedicalSort_BoxNumber
    FROM MSShipment
    INNERJOIN MedicalSort
    ON MSShipment.[BoxNumber]= MedicalSort.[BoxNumber];

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    you can only delete from one table at a time. this would normally be managed by setting your relationship between the two tables as maintaining referential integrity and cascade delete records. Then deleting the one side of the relationship (MSShipment?) will automatically delete the many side of the relationship. So your query would be

    DELETE * FROM MSShipment

    Note you can only set this relationship if it already has integrity - if you have records in MedicalSort that don't relate back to MSShipment then you won't be able to set the relationship anyway.

    To find out if you have unrelated records in MedicalSort then run this query

    Code:
    SELECTMedicalSort.*
    FROM MedicalSort 
    LEFT JOIN MSShipment 
    ONMedicalSort.[BoxNumber]=MSShipment.[BoxNumber]
    WHERE MSShipment.[BoxNumber] is null;

  3. #3
    Gauchey is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    4
    Thank you for responding so quickly. I am not able to set integrity for these two tables at least as I understand it. There are records that will be in Medicalsort that will not exist in Msshipment. When I try to enforce that it tells me that there are records in one table that do not exist in the other. That being said, is there a way to delete the records from one table and use a second table as the reference for those that should be deleted? I have a relationship established just not referential integrity. Thanks for your help with this.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Will have to delete the 'dependent' records first. Maybe like:

    DELETE FROM Medicalsort WHERE [BoxNumber] IN (SELECT [BoxNumber] FROM Medicalsort INNER JOIN Msshipment ON Medicalsort.[BoxNumber] = Msshipment.[BoxNumber])

    Since this is deleting complete records there is no need to explicitly list fields in the DELETE action.
    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.

  5. #5
    Gauchey is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    4
    June7 thank you so much!

  6. #6
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    I was going to suggest

    Code:
    DELETE * FROM Medicalsort WHERE [BoxNumber] IN (SELECT [BoxNumber] FROM Msshipment)

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Yeah, I guess that is all that is needed. Are all records also to be deleted from Msshipment? There will be orphan records left in Medicalsort?

    Don't even need the * wildcard.
    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.

  8. #8
    Gauchey is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    4
    June7, I tested it and it works as needed. It is intentional to have orphan records. When we ship medical supplies it is not always possible to fit everything into the container and thus we need to know what we have left. Thanks again

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

Similar Threads

  1. Replies: 12
    Last Post: 04-25-2013, 01:32 PM
  2. Replies: 2
    Last Post: 01-24-2012, 02:16 PM
  3. Error: Could not delete from specified tables
    By kaledev in forum Queries
    Replies: 1
    Last Post: 03-03-2011, 02:07 PM
  4. Replies: 3
    Last Post: 07-16-2010, 12:32 PM
  5. Could not delete from specified tables.
    By bullwinkle55423 in forum Queries
    Replies: 0
    Last Post: 08-31-2006, 08:14 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