Modifying Delete Button code

M

Mishanya

In my form I have Delete Button coded by the wizard with some modification as
follows:

Private Sub btnDeleteAsset_Click()
On Error GoTo Err_btnDeleteAsset_Click

If MsgBox("Are You sure You want to delete this asset?", _
vbYesNo + vbExclamation, "Attention!") = vbNo Then
Me.Undo
Cancel = True

Else

DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
MsgBox "The deletion is completed"

Exit_btnDeleteAsset_Click:
Exit Sub

Err_btnDeleteAsset_Click:
MsgBox "Your deletion attempt is aborted"
Resume Exit_btnDeleteAsset_Click

End If

End Sub

The events caused by pressing the button go as this:
1) My message "Are You sure You want to delete this asset?"

if NO selected - this is it

if YES selected
2) Access message box "You are about to delete 1 record", while the record
I'm trying to delete is changed by the next record

if NO selected
3) My message "Your deletion attempt is aborted", while the recordset is
back to the attempted

if YES selected
3) My message "The deletion attempt is completed"

The step 2) is quite confusing - firstly, it doubles my message, secondly -
next record is already presented, wich makes the user hesitate what is he
about to delete.

How can I modify the code so the whole thing will work properly:
1) no Access messages but defined by me
2) no next record popping up in the step 2 (ideally - I still see the
attempted record)
3) after the deletion is completed the form is in New record mode (again, no
next record popping up)?
 
A

Allen Browne

Here's the approach I use:

Private Sub btnDeleteAsset_Click()
On Error GoTo Err_btnDeleteAsset_Click

If Me.Dirty Then Me.Undo
If Not Me.NewRecord Then RunCommand acCmdDeleteRecord

Exit_btnDeleteAsset_Click:
Exit Sub

Err_btnDeleteAsset_Click:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_btnDeleteAsset_Click
End Sub

This is the way Access works:
a) It shows the confirmation dialog, respecting the fact that the user may
have turned this off (so they don't get a dialog), and you still have the
option to use the form's BeforeDelConfirm event if you want a custom
message.

b) It deletes the record inside a transaction, and shows the user what it
will look like with the record gone. If the user cancels the delete, it
cancels the transaction, and so the record returns.

It's probably best for you to adapt to the way Access works, rather than try
to manipulate it to do something else. Your alternative might make sense to
you, but would not be what an experienced Access user would expect.

If you really want to delete the record without respect for the way Access
works, without respect for the user's settings regarding deletion warnings,
and just do your own thing, you can delete from the RecordsetClone of the
form (after setting the Bookmark) to bypass the form's events and warning
messages. But I did not recommend that. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top