Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling Delete Key

Status
Not open for further replies.

victory92

Programmer
Sep 18, 2002
35
US
Bear with me -- I'm still new at Access....

I am displaying a list. The user selects from this list to perform an update. I recently found out that the user can delete items off this list.

I want to have a way to either disable the delete key -- or through VBA code - to disable the delete ability.

When I look at the properties under detail of the form - there is not an "On Delete" that I can use for an event procedure.

Any ideas????

Thank You
Victory92
 
ption Compare Database
Option Explicit

Global gControlKeyPressed As Boolean
Global gCancelKeyPress As Boolean

Public Function ControlKeyDown(K_Code As Integer)
If K_Code = vbKeyControl Then
gControlKeyPressed = True
End If
If gControlKeyPressed = True Then
Select Case K_Code
Case vbKeyF 'Find
gCancelKeyPress = True
Case vbKeyH 'Replace
gCancelKeyPress = True
Case Else
gCancelKeyPress = False
End Select
End If
End Function

Public Function ControlKeyUp(K_Code As Integer)
If K_Code = vbKeyControl Then
gControlKeyPressed = False
End If
End Function
'-----------------------------------------------------------

'Paste the following into the form's key down event:

'ControlKeyDown (KeyCode)
'If gCancelKeyPress = True Then
'KeyCode = 0
'gCancelKeyPress = False
'End If
'-----------------------------------------------------------

'Paste the following into the form's key upevent:

'ControlKeyUp (KeyCode)
Cruz'n and Booz'n always.
This post shows what little I do at work.
 
Thank you for your prompt response. Unfortunately, I don't understand what the code is doing. I understand that the Key down event is invoked when a user presses a key and the Key Up is invoked when the user releases the key.

But I could not find any information on the Control and Cancel key Press variables or the Control Key Up and Down functions.

How does the program know the Delete key was pressed and it should not be permitted?

Again - I'm new here....bear with me.

Thanks

Victory92
 
see the following for more information;


Microsoft access help: Keyboard Events

And you have to modify the code a little bit. vbkeyControl = control key, replace that with vbkeydelete. I'm really sorry about not being more clear.

all you have to do is change the following line:

If K_Code = vbKeyControl Then

with

If K_Code = vbKeyDelete Then

This is a piece of generic code that i have that I modify for certain circumstances. I posted a little too quickly and didn't clarify. My apologies.

For clarity you can rename the variables so they are better descriptions. Cruz'n and Booz'n always.
This post shows what little I do at work.
 
actually, there's alot of modifications, i'll modify it in the next couple of minutes.

the case statement isn't needed... Cruz'n and Booz'n always.
This post shows what little I do at work.
 
the following disables the delete key for a textbox named text2.
Hope this helps.

Option Compare Database
Option Explicit

Public Function ControlKeyDown(K_Code As Integer)
If K_Code = vbKeyDelete Then
MsgBox ("delete key has been disabled")
End If

End Function


Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
ControlKeyDown (KeyCode)
End Sub Cruz'n and Booz'n always.
This post shows what little I do at work.
 
for the entire form use:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
ControlKeyDown (KeyCode)
End Sub Cruz'n and Booz'n always.
This post shows what little I do at work.
 
Thank you for your responses. I have a much better understanding of what the code is now doing.

however, I'm trying to ensure the user does not delete a record -- not a particular field. I am getting the message from the msg box -- but then Access takes over with message box about "You are about to delete 1 record(s). If you want to...."

I have a list where the user is using the left margin controlled by Access to identify the record. When the delete key is pressed, Access executes the new code -- then continues to try to do a delete.

Does this make sense?
 
IF it's deleted records you are worried about, why not set the allow deletion property of the form to no??
hope this helps,
sdraper
 
allow delete is under the Data tab of the forms property pages
 
I think i understand what you're saying. You're saying that you want to keep a user from deleting a record by either:

1) Disabling the delete key
2) Disable the delete event

For disabling the key its self, we've already bantered. You can disable the entire delete key within a form by using the following code:

Option Compare Database
Option Explicit

Public Function ControlKeyDown(K_Code As Integer)
If K_Code = vbKeyDelete Then
MsgBox ("delete key has been disabled")
End If

End Function

'FORM KEY DOWN EVENT
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
ControlKeyDown (KeyCode)
End Sub

I'm not 100% sure about disabling the delete event, I do know that you can get around the actually deleting by using the delete event. see Microsoft Access Help topic: Delete, BeforeDelConfirm, AfterDelConfirm Events

You can on the Delete event notify the user that they MAY NOT delete a record. This happens before the beforedelconfirm event. (I believe) I don't have any experience with disabling the actually delete function.

I just toyed with these events and this worked for me:

Private Sub Form_Delete(Cancel As Integer)
MsgBox ("you may not delete")
End Sub

Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
Cancel = True
End Sub


or you can put it in one sub like the following, which is probably a better idea.

Private Sub Form_Delete(Cancel As Integer)
MsgBox ("you may not delete")
Cancel = True
End Sub


I really hope this helps... Cruz'n and Booz'n always.
This post shows what little I do at work.
 
Thanks...It's working perfectly. I appreciate all the help!!

Victory92
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top