Trapping "The DoMenuItem action was cancelled" on No to Delete Record (1 Viewer)

whpacejr

Registered User.
Local time
Yesterday, 21:49
Joined
May 26, 2006
Messages
4
:confused: Anyone know how to trap/prevent the msg "The DoMenuItem action was cancelled" after answering no to deleting a record? I followed the instructions at the following thread but no luck:

http://www.access-programmers.co.uk...t=53206&highlight=DoMenuItem+action+cancelled

Here's the code:

----------------------------------
Private Sub Delete_Click()
On Error GoTo Err_Delete_Click
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
Exit_Delete_Click:
Exit Sub
Err_Delete_Click:
MsgBox Err.Description
Resume Exit_Delete_Click
End Sub

-----------------------------------
Private Sub Form_Delete(Cancel As Integer)
DoCmd.SetWarnings False
If MsgBox("Are you sure you want to delete this record?", vbYesNo) = vbNo Then
Cancel = True
Else
MsgBox "Record deleted!"
End If
End Sub
-------------------------------------

Thanks in advance.
 
Last edited:

Smart

Registered User.
Local time
Today, 03:49
Joined
Jun 6, 2005
Messages
436
Do you have error trapping in your code like this.

The MsgBox Err.Number & vbCrLf & Err.Description part will give you the error number and then you can check for this error number (where the ???? are and then either resume and exit or display a msg box etc


On Error GoTo Err_Record_Click

Code goes here

Exit_Record_Click:
Exit Sub

Err_Record_Click:

If Err.Number = ???? Then
Resume Next
Else
MsgBox Err.Number & vbCrLf & Err.Description
Resume Exit_Record_Click
End If
 

ghudson

Registered User.
Local time
Yesterday, 22:49
Joined
Jun 8, 2002
Messages
6,195
You are making this harder than it should be for such a simple task. Remove the code you have in the Forms OnDelete event and use this in the OnClick event of your Delete button. Modify the command button name to fit your need. Also, dump the DoCmd.DoMenuItem commands that the outdated wizard creates.

Code:
Private Sub bDelete_Click()
On Error GoTo Err_bDelete_Click
    
    If MsgBox("Are you sure you want to delete the current user record?", vbQuestion + vbYesNo, "Delete Current Record?") = vbYes Then
        DoCmd.SetWarnings False
        DoCmd.RunCommand acCmdSelectRecord 'this command is not normally needed
        DoCmd.RunCommand acCmdDeleteRecord
    End If

Exit_bDelete_Click:
    DoCmd.SetWarnings True
    Exit Sub

Err_bDelete_Click:
    If Err.Number = 2046 Then 'The command DeleteRecord isn't available now - No records to delete
        DoCmd.SetWarnings True
        MsgBox "There is no record to delete.", vbCritical, "Invalid Delete Request"
        Exit Sub
    ElseIf Err.Number = 2501 Then 'The RunCommand action was canceled
        DoCmd.SetWarnings True
        Exit Sub
    Else
        MsgBox Err.Number & " - " & Err.Description
        Resume Exit_bDelete_Click
    End If
    
End Sub

Searching the forum is a great way to discover and learn the answers to your Access programming questions.
 

Users who are viewing this thread

Top Bottom