Results 1 to 7 of 7
  1. #1
    jpg250 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2013
    Posts
    26

    How to test if delete successfull


    I have some code to delete a record from a table, which does not delete the record, presumably because of referential integrity settings. That is fine of course, that's what should happen. However I want to be able to notify the user if a delete did not succeed. How can I check this? I tried this code, but it did not return an error.
    Code:
                strsql = "DELETE * FROM " & tblAddress & " WHERE GenAddressID = " & Me.tb_edtID
                Err.Clear
                CurrentDb.Execute (strsql)
                If Err.Number <> 0 Then
                    MsgBox (Err.Description)
                End If

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Look at RecordsAffected; you may need to use a variable for the CurrentDb.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jpg250 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2013
    Posts
    26
    Thanks pbaldy, that was exactly what I was looking for. That works now. Do you also know where I might pull the appropriate message from if the delete could not be done - for example if I try to delete that same record by opening the table in datasheet view, Access gives me a nice message "The record cannot be deleted or changed because table 'Client' includes related records".

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Well, what you have will probably fail silently on that type of thing. add the bit at the end

    CurrentDb.Execute strsql,
    dbFailOnError

    If that just gives you a call failed error, this may help:

    http://support.microsoft.com/kb/161288

    I haven't used that myself yet, but I was intending to try it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    jpg250 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2013
    Posts
    26
    Thank you very much. I'll try it out and let you know how it went - maybe tomorrow as I'm busy the rest of today.

  6. #6
    jpg250 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2013
    Posts
    26
    I tried it. Adding the dbFailOnError gives me a run-time error, which I can catch and display it's message in a messagebox. Perfect!

  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,652
    Excellent! Sometimes you want it to fail silently, which I guess is why it's an optional parameter.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Test
    By goatamus in forum Access
    Replies: 1
    Last Post: 02-02-2012, 02:44 PM
  2. Replies: 2
    Last Post: 01-24-2012, 02:16 PM
  3. test box prompt
    By neil123williams in forum Reports
    Replies: 13
    Last Post: 01-10-2012, 05:48 PM
  4. Trying to Delete record using delete query
    By MooseOTL in forum Access
    Replies: 13
    Last Post: 10-04-2011, 02:30 AM
  5. Keying in Test answers to Access DB from Written Test
    By CityOfKalamazoo in forum Access
    Replies: 3
    Last Post: 03-01-2010, 08:58 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