Disable delete of current record

Leesha
BronzeLounger
Posts: 1488
Joined: 05 Feb 2010, 22:25

Disable delete of current record

Post by Leesha »

Hi,
I have frmPaymentAdjustments set to continuous form. Record selectors is set to true. There is a delete button that performs various code all of which works fine. The only issue is if the user hits the delete key on their computer and bypasses the delete button. The record is deleted but the code is not run. I tried putting the code that is in the delete button on the delete function of the form but ran into issues. I tried a msgbox that said that the record could only be deleted from the delete button and had Exit Sub after the msgbox but the record was deleted. i tried disabling the "allow deletions" but ran into issues. Is there a way to prevent the record from being deleted if the user hits the delete key vs the button?
Thanks,
Leesha

User avatar
HansV
Administrator
Posts: 78471
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Disable delete of current record

Post by HansV »

1) Declare a variable near the top of the form's code module, above all procedures:

Code: Select all

Private CanDelete As Boolean
2) Create event procedures for the Before Del Confirm and On Delete events of the form:

Code: Select all

Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
    ' Don't show the built-in delete prompt
    Response = acDataErrContinue
End Sub

Private Sub Form_Delete(Cancel As Integer)
    If Not CanDelete Then
        ' User tried to bypass the Delete button
        MsgBox "Please use the Delete button if you want to delete a record!", vbInformation
        Cancel = True
    End If
End Sub
3) Modify the On Click event procedure of the delete button. I used cmdDelete in the following example:

Code: Select all

Private Sub cmdDelete_Click()
    If MsgBox("Are you sure that you want to delete the selected record?", vbQuestion + vbYesNo) = vbYes Then
        ' Add code that you want to run before deleting the record here
        ' ...
        CanDelete = True
        RunCommand acCmdDeleteRecord
        CanDelete = False
        ' Add code that you want to run after deleting the record here
        ' ...
    End If
End Sub
Best wishes,
Hans

User avatar
Gasman
2StarLounger
Posts: 104
Joined: 22 Feb 2022, 09:04

Re: Disable delete of current record

Post by Gasman »

Why not have the Delete key run your Delete code?
Using Access 2007/2019.
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.
Please, please use code tags when posting code snippets, click the </>icon.
Debug.Print is your lifesaver.

User avatar
HansV
Administrator
Posts: 78471
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Disable delete of current record

Post by HansV »

What if they click the Delete button on the ribbon?
Best wishes,
Hans

Leesha
BronzeLounger
Posts: 1488
Joined: 05 Feb 2010, 22:25

Re: Disable delete of current record

Post by Leesha »

Hi Hans,
Thanks so much! The code is perfect. Don't laugh...................there is a delete key on the ribbon???

Gasman - I tried having the delete key run my code but I ran into issues.

User avatar
HansV
Administrator
Posts: 78471
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Disable delete of current record

Post by HansV »

Leesha wrote:
15 May 2022, 13:13
there is a delete key on the ribbon???
Yes, in the Records group:

S1435.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

Leesha
BronzeLounger
Posts: 1488
Joined: 05 Feb 2010, 22:25

Re: Disable delete of current record

Post by Leesha »

I never use it so never dawned on me to disable it?

User avatar
Gasman
2StarLounger
Posts: 104
Joined: 22 Feb 2022, 09:04

Re: Disable delete of current record

Post by Gasman »

What if they click the Delete button on the ribbon?
Does that not do the same thing?
Using Access 2007/2019.
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.
Please, please use code tags when posting code snippets, click the </>icon.
Debug.Print is your lifesaver.

User avatar
HansV
Administrator
Posts: 78471
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Disable delete of current record

Post by HansV »

Does hijacking the Delete key automatically also hijack the Delete button on the ribbon? Perhaps I'm missing your point...
Best wishes,
Hans

Leesha
BronzeLounger
Posts: 1488
Joined: 05 Feb 2010, 22:25

Re: Disable delete of current record

Post by Leesha »

Hi Hans,
If I try to delete from the ribbon the code works the same way and blocks it so this is good.
Thanks again!
Leesha