Group:  Microsoft Access ยป microsoft.public.access.formscoding
Thread: Modifying Delete Button code

Geek News

Modifying Delete Button code
Mishanya 12/24/2008 10:55:00 AM
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)?


Re: Modifying Delete Button code
"Allen Browne" <AllenBrowne[ at ]SeeSig.Invalid> 12/24/2008 11:50:59 AM
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. :-)

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Mishanya" <Mishanya[ at ]discussions.microsoft.com> wrote in message
news:A54615FA-DD92-4409-9811-AEE810F92F25[ at ]microsoft.com...
[Quoted Text]
> 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)?

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net