Results 1 to 12 of 12
  1. #1
    WCStarks is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    314

    Deleting a parent record


    Currently, when I delete a record in a parent form, I must first manually delete all the associated records in the child form. I would like to automate this task. Can someone suggest how I might code a function to accomplish that?

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  3. #3
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    I don't know what exactly is best practice but here is my approach. (Maybe best practice is a deleted flag field and not actually deleting the data...)

    I prefer to set up all my table relationships WITHOUT cascade delete enabled in order to avoid major accidents. My delete button asks the user TWICE if they're sure they want to delete the record(s) and my code goes like this:

    Code:
    Private Sub cmdDelete_Click()
    On Error GoTo ErrHandler
        Dim db As DAO.Database
        Dim qry As String
        
        If MsgBox("This will delete the current record as well as its children. Continue?", vbYesNo + vbQuestion + vbDefaultButton2, "Delete Records") = vbYes Then
            If MsgBox("Last chance, do you want to abort?", vbYesNo + vbQuestion, "Delete Records") = vbNo Then
                Set db = CurrentDb
                
                qry = "DELETE * FROM tblChildren WHERE ParentForeignKey=" & PARENT_KEY
                db.Execute qry, dbFailOnError
                
                qry = "DELETE * FROM tblParents WHERE ParentPrimaryKey=" & PARENT_KEY
                db.Execute qry, dbFailOnError
            End If
        End If
        
    
    ExitHandler:
        Set db = Nothing
        Exit Sub
        
    ErrHandler:
        MsgBox Err.Description, vbCritical, "Delete Error #" & Err.Number
        Resume ExitHandler
        
    End Sub
    I like to force a yes answer and a no answer to avoid the user mindlessly clicking through the message boxes.
    Last edited by kd2017; 02-15-2019 at 02:20 PM. Reason: Incorporating tip from pbaldy on default msgbox response

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Deleting data should be a rare event. Once it's gone, it's gone. So why are you deleting?
    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
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by kd2017 View Post
    I like to force a yes answer and a no answer to avoid the user mindlessly clicking through the message boxes.
    I default the yes/no message box to no and only take action on yes, so if they're not paying attention and just hit enter nothing happens.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by pbaldy View Post
    I default the yes/no message box to no and only take action on yes, so if they're not paying attention and just hit enter nothing happens.
    Ah, thanks for the tip! The msgbox line then becomes:
    Code:
    If MsgBox("This will delete the current record as well as its children. Continue?", vbYesNo + vbQuestion + vbDefaultButton2, "Delete Records") = vbYes Then

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem. I deal with a bunch of "head down" data entry people who aren't necessarily looking at the screen, so i use that a lot.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    WCStarks is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    314
    Thank you. This is also helpful. While it is good to know about the Cascade Delete feature, I was a little nervous about using it. For one thing, while I have been developing this db, I have had Forced Integrity turned off on most of the relationships in the interim.

  9. #9
    WCStarks is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    314
    Your right, and I don't allow it in most situations. The database needs to preserve the history. When members become inactive, for example, I never delete them, as they are part of the history. There is one situation, however, where I have individuals linked as participants in events. This is the most used relationship. I have sometimes accidentally created a duplicate event, while reconstructing historical data. In this case, it is usually better to delete one or the other event. I have been doing it manually, when needed. This caused me to ask how to automate this task.

    This database was developed over 3 decades after the organization was formed. While developing the db to improve management of the organization, I have also gone back and recovered as much history as I could from the existing records. As you can imagine, this has been a major undertaking. I asked this question, initially, just to learn how to do it. As I am nearly finished with reconstructing the history, it may not be that important any more. But, at least I have learned two ways to do it. Thanks, all.

  10. #10
    WCStarks is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    314
    Just one question. Where does PARENT_KEY come from? With all caps, is it some kind of system variable?

  11. #11
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    No it's just a place holder for that generic code, you'd replace it with a variable that references the record you're trying to delete.

    You'll need to modify the rest of that code to suit your situation for that matter. If you want more specific code for your situation you'll need to post more details...

  12. #12
    WCStarks is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    314
    That's ok. The all caps in that one case and not in the others confused me. I was wondering if it had something to do with a value obtained from the MsgBox function, which I couldn't see in the definition of the function. I know what to do. Between you and others, I ended up with two good solutions for the task. Thank you all for your contributions.

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

Similar Threads

  1. Replies: 17
    Last Post: 07-21-2023, 12:09 AM
  2. Replies: 2
    Last Post: 09-25-2018, 05:33 PM
  3. Go To Next Record of Parent Subform
    By PicoTTS in forum Access
    Replies: 1
    Last Post: 09-28-2016, 04:12 PM
  4. Replies: 1
    Last Post: 04-12-2013, 03:15 PM
  5. Deleting Record Contents, not complete record...
    By Charles Waters in forum Access
    Replies: 2
    Last Post: 06-24-2008, 12:00 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