Results 1 to 9 of 9
  1. #1
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618

    Deleting Multiple records in a query

    Long ago I learnt how to set up a query that returns records found in table A but not in table B. I use the way through "Append Query" to add the records I wanted to add that was found in A but not yet in B. My challenge is that some records in A may get redundant and have no use. I want to learn how to delete them through "Delete query" or VBA. I just spent a couple of hours testing suggestions, and advice I got from the net. Like setting "Unique records" on the Property sheet to yes in a query. It did not work, maybe my lack of enough brain sells is holding me back again.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Delete queries can be quite unforgiving, so proceed with caution. Backup your database, or copy your current table and rename that copy.

    Do some testing. Create a table, add some records, then
    -use a Select (readOnly) query to identify records you would like to delete.
    -if the selected records match you expected records to be deleted, then
    -modify the query from Select to Delete
    -run the query
    -review your table to assure the appropriate records have been deleted.

    You can post your activities as outlined above to show readers what stage you are at, and ask more questions if necessary.

    Good luck.

  3. #3
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Thank you. I did what you suggest before you replied. Note that I am comparing a Query with a Table (They must return the same records) in a new Select query that returns the records I want to Delete from the Table. The select query returns one field which is in the Table. I then make it a delete query, setting "Unique Records" to yes on the property sheet. When I run it, the error is. "Could not Delete from specified tables".

    I also appended the query to a table. Table A and Table B is compared. I added 5 records to Table A and in the Select query the 5 display as I want them to. I then change the Select query to a delete query. When I run it the error is "Could not Delete from specified tables".

    Table A holds extra information, I can not clear the Table A, and append the query, else I loose the extra information.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    With a Delete query, you Delete RECORDS, not fields. The entire record is deleted.

    I recommend that you post your database and provide directions detailing the steps to get to the query and table involved. Your "comparing a Query with a Table" is a little confusing.

    Always advisable to post code you have tried along with any messages you receive.

    W3schools has some great tutorials re SQL/queries. Here's link to DELETE

  5. #5
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Thanks. I will have a look. I will import just a couple of queries and tables into a clean database and then post it as soon as time allows me.

  6. #6
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Hope I done it correct. I looked at the example you suggest. In my case I need to establish the records in Table B that are not in Table A and only delete them. In the posted example there are 12 records in B not in A as you can see in the query. Maybe I am missing something small.
    Attached Files Attached Files

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    SELECT query to identify the FormNameC values in TableB that are not in TableA

    Code:
    SELECT TableB.FormNameC
    FROM TableA RIGHT JOIN TableB ON TableA.FormName = TableB.FormNameC
    WHERE (((TableA.FormName) Is Null));


    Delete query to "tell Access" Delete the records in tableB that are identified in the SELECT query

    DELETE TableB.FormNameC
    FROM TableB
    where FormNameC IN
    (SELECT TableB.FormNameC
    FROM TableA RIGHT JOIN TableB ON TableA.FormName = TableB.FormNameC
    WHERE (((TableA.FormName) Is Null)));

    Click image for larger version. 

Name:	MsgWhenRunningDeleteQuery.png 
Views:	11 
Size:	8.8 KB 
ID:	48248

    Also Note: My description of designing a SELECT query, checking to make sure result is as expected, then convert to DELETE query --works when only 1 table is involved. Sorry if I was confusing.

    I tried to upload a GIF file, but the system kept changing it to jpg. So, I have put the gif into the attached zip file. Open the zip, and watch the gif animation. Good luck.!
    Attached Files Attached Files

  8. #8
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Thank you sir. That works brilliant.

  9. #9
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870

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

Similar Threads

  1. deleting multiple records from the table
    By talhaparvaiz@yahoo.com in forum Access
    Replies: 2
    Last Post: 06-27-2020, 02:36 PM
  2. Replies: 5
    Last Post: 03-11-2019, 02:35 PM
  3. Deleting Multiple Records
    By Eranka in forum Access
    Replies: 3
    Last Post: 06-04-2018, 08:37 AM
  4. Replies: 2
    Last Post: 05-15-2013, 09:03 PM
  5. Deleting multiple records selected in a Listbox
    By ankitmehtta in forum Access
    Replies: 4
    Last Post: 11-08-2011, 01:20 PM

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