+ Reply to Thread
Results 1 to 6 of 6

How to write code to delete record based on field value?

  1. #1
    Forum Contributor
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    680

    How to write code to delete record based on field value?

    Please Login or Register  to view this content.


    I have two tables: TableGroup & TableGroupDeleted

    TableGroup: A group can have one or multiple administrators, or NO administrator.

    Take attached two screenshots for example, in first screenshot TableGroup, I would like to delete any groups which do not have administrator. For this screenshot, I will need to need Group3, and there are two records associated with Group3; therefore, the last two records will need to be deleted. But before being deleted, those two records will need to be copied to TableGroupDeleted, which has similar data structure except last field (Copy 5 fields and add current date for last field DateDeleted).

    How can I write the code?

    Thanks.
    Attached Images Attached Images

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: How to write code to delete record based on field value?

    you can't do it in 1 query. write 2. first one is APPEND:

    https://support.microsoft.com/en-us/...8-8ef649cf3596

    2nd one is a DELETE query:

    https://support.microsoft.com/en-us/...0-c052cd4c3ec5

    that is all you need. for your "no admin" spec, obviously you need a WHERE:
    Please Login or Register  to view this content.
    field = "administrator check box"
    0 = not checked for that field

  3. #3
    Forum Contributor
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    680

    Re: How to write code to delete record based on field value?

    Thank you, but Delete * from table WHERE Administrator = 0 is not accurate statement. I said "delete any groups which do not have administrator", not "delete any records which are not administrator". I do not want to delete third record (Group2 Person1), since Group2 has two administrators.
    Last edited by AliGW; 12-29-2020 at 07:37 AM. Reason: PLEASE don't quote unnecessarily!

  4. #4
    Forum Contributor
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    680

    Re: How to write code to delete record based on field value?

    Can someone move my thread to Sub Forums: Access Programming / VBA / Macros ?

    Thanks.

  5. #5
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: How to write code to delete record based on field value?

    Well my apologies it's been so long since I worked with Microsoft Access or written any non-based webquaries like this. This might work with a group by clause but I'm not sure. Look that up. it is here....

    https://support.microsoft.com/en-us/...a-002bb65ef3a0

    Furthermore,

    This is what I'm thinking....

    "" Delete *from table where administrator equals false group by groupname"".

    sorry I can't give you the exact answer but I'm not at my machine to run a test for you.

  6. #6
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: How to write code to delete record based on field value?

    But now that I think about it I think writing too stacked queries is the best for you. The first one would group the results by group name and possibly return distinct records where administrator equals false. and the second one would be pulling from that first query and deleting them all where it finds the corresponding group name.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to delete record based on date?
    By VAer in forum Access Programming / VBA / Macros
    Replies: 2
    Last Post: 10-06-2020, 05:02 AM
  2. Replies: 1
    Last Post: 06-19-2015, 09:23 AM
  3. [SOLVED] UserForm in Excel- need to write code to auto fill a text field
    By PANTECH in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-09-2013, 10:58 AM
  4. How to write VBA code to filter a field when opening a workbook?
    By checkup2012 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-22-2012, 11:29 AM
  5. delete duplicate records after totaling a field from each record
    By HarrySpencer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-10-2007, 09:49 AM
  6. Replies: 1
    Last Post: 10-12-2005, 02:05 PM
  7. [SOLVED] delete record based on a value
    By Anauna in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-27-2005, 04:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1