"Run-time error 2501" is driving me crazy!

Eko

Registered User.
Local time
Today, 04:43
Joined
Oct 3, 2008
Messages
15
Wanna be my hero? Here's your chance!

I'm working on another database but to make this case simpler, I started from a scratch and created a new and really simple database. It only has one table (with 3 fields) and one form based on that table.
Then, I created a "delete" button on the form using the button wizard, for the purpose of having option to delete individual records (simple, right?). I then created 5 different records.
Now, if I use that button to delete a record, a message pops up that says "You are about to delete one record(s)! Are you sure..?" If I click YES, it deletes it like it should. If I click NO, I get an error message that says "Run-time error: 2501, The DoMenuItem action was canceled".
I looked everywhere and found a few solutions how to trap this error, but none of them worked for me.
frown.gif
I tried one that apparently worked for someone else, but not for me. The dreaded error message still pops up if I cancel delete.
banghead.gif


Code:
Private Sub btnDelete_Click()
On Error GoTo Err_btnDelete_Click

    DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
    DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
    If Err Then
        If Err.Number = 2501 Then
        Err.Clear
        End If
    End If

Exit_btnDelete_Click:
    Exit Sub

Err_btnDelete_Click:
    MsgBox Err.Description
    Resume Exit_btnDelete_Click
    
End Sub
I think that routine (for some reason) never reaches and executes the "If err" part. I simplified that part even further by replacing it with:

Code:
If Err Then
        MsgBox "test"
    End If
just to see if anything happens (the idea was to display a little "test" message box if any error occurs.) Still nothing
mad.gif

I don't know if this is some kind of bug in Access2000, and error 2501 just can't be handled, or I'm just missing something, but I'm slowly going crazy after spending a whole day on the stupid error message.

Thanks in advance.
 
Two things -

1. Don't use the DoMenuItem syntax - that is old, out of date, and deprecated (yes, I know the wizard still uses it but it shouldn't). Use the RunCommand syntax because it is also easier to figure out what it is doing.

2. All you need is to change to

Code:
Private Sub btnDelete_Click()
On Error GoTo Err_btnDelete_Click

   DoCmd.RunCommand acCmdSelectRecord
   DoCmd.RunCommand acCmdDeleteRecord
   
Exit_btnDelete_Click:
    Exit Sub

Err_btnDelete_Click:
    If Err.Number <> 2501 Then
        MsgBox Err.Description
        Resume Exit_btnDelete_Click
    End If
End Sub
 
No... :( Still getting the error message!
I swear I tried like 20 different combinations. Pretty much anything I could find on the net, including DoCmd.RunCommand. Is it possible that it is computer or software version dependent because I've come across similar cases where people claimed that code worked fine on their computers but those who asked for help, said it still wouldn't work for them (like myself here)?

Thanks for your suggestion, though. I appreciate the quick response. At least I learned that DoCmd.RunCommand is prefered way of doing similar things.
 
OK, here's the file.

But, as I said in my first post, it's just a really simple db that I created just to try to figure out how to suppress/trap that awful error 2501. It has your code in it. So, if it works for you without any additional modification, that will mean that some functions (very few, though) are actually computer/software dependent.

Thanks again, boblarson.
 

Attachments

Works for me - which version of Access are you using and what service pack level of Office?
 
I've recently updated with latest service pack for my version of Access, so now I have

MS Access 2000 (9.0.6926 SP-3)

I'm not totally VB proficient so my next question may be a little off, but is it possible that I don't have all necessary libraries checked under my References (or maybe Priorities are in wrong order)? Would that have anything to do with how errors are handled?
 
I would try first importing everything into a new database file to see if that will help.
 
Tried it, no difference. Still getting the error message.
However, I discovered that if I make the .mde file, than it works like it should (no error message after canceling delete). But it's so inconvenient to make the .mde file after every little change I make, just so I could test it.
I just can't figure out why I'm getting the "Run-time error 2501" when working with .mdb file, but not when working with same file converted to the .mde.
Thanks for your suggestions.
 

Users who are viewing this thread

Back
Top Bottom