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
Disable delete of current record
-
- Administrator
- Posts: 78474
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Disable delete of current record
1) Declare a variable near the top of the form's code module, above all procedures:
2) Create event procedures for the Before Del Confirm and On Delete events of the form:
3) Modify the On Click event procedure of the delete button. I used cmdDelete in the following example:
Code: Select all
Private CanDelete As Boolean
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
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
Hans
-
- 2StarLounger
- Posts: 104
- Joined: 22 Feb 2022, 09:04
Re: Disable delete of current record
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.
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.
-
- Administrator
- Posts: 78474
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Disable delete of current record
What if they click the Delete button on the ribbon?
Best wishes,
Hans
Hans
-
- BronzeLounger
- Posts: 1488
- Joined: 05 Feb 2010, 22:25
Re: Disable delete of current record
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.
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.
-
- Administrator
- Posts: 78474
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Disable delete of current record
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- BronzeLounger
- Posts: 1488
- Joined: 05 Feb 2010, 22:25
Re: Disable delete of current record
I never use it so never dawned on me to disable it?
-
- 2StarLounger
- Posts: 104
- Joined: 22 Feb 2022, 09:04
Re: Disable delete of current record
Does that not do the same thing?What if they click the Delete button on the ribbon?
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.
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.
-
- Administrator
- Posts: 78474
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Disable delete of current record
Does hijacking the Delete key automatically also hijack the Delete button on the ribbon? Perhaps I'm missing your point...
Best wishes,
Hans
Hans
-
- BronzeLounger
- Posts: 1488
- Joined: 05 Feb 2010, 22:25
Re: Disable delete of current record
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
If I try to delete from the ribbon the code works the same way and blocks it so this is good.
Thanks again!
Leesha