Cancel delete

I have a datagrid with select from multiple tables.  I allow for deletion of a row but want to catch the delete event before it happens and cancel it so I can do the delete manually.  I've added Cancel=True to the BeforeDelete sub but everytime I try and delete a record the app just crashes.
Can anyone help?
LVL 58
GaryAsked:
Who is Participating?
 
senthil_msvConnect With a Mentor Commented:
I worked out that its something to do with the code I had in the sub to do the actual delete, when I took it out it worked fine, but so the problem is how to do that delete and cancel the delete that was initiated by pressing the DEL button

Private Sub DataGrid1_BeforeDelete(Cancel As Integer)
    If Cancel = 0 Then  ' AllowDelete = true
        If MsgBox("Are you sure want to delete?", vbQuestion + vbYesNo) = vbYes Then
            'Here Delete process goes
        Else
            'Here Delete Cancel Process goes
            Cancel = 1  'Cancel the Delete Operation
        End If
    End If
End Sub

0
 
gecko_au2003Commented:
What happens if you put Cancel = 1 instead of Cancel = True in that event ?

Does that make any difference ?
0
 
GaryAuthor Commented:
Already tried that and the same thing happens
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
gecko_au2003Commented:
what about using an if statement to catch it ie

If Cancel = True then
MsgBox "Cancel Delete was catched"
'code that you want to run if someone trys to delete something
Else
End If

Other then that I am not sure, sorry I cant be of further help !
0
 
GaryAuthor Commented:
Already tried..
0
 
senthil_msvCommented:
Private Sub DataGrid1_BeforeDelete(Cancel As Integer)
    If Cancel = 0 Then  ' AllowDelete = true
        Cancel = 1  'Cancel the Delete Operation
    End If
End Sub
0
 
GaryAuthor Commented:
I worked out that its something to do with the code I had in the sub to do the actual delete, when I took it out it worked fine, but so the problem is how to do that delete and cancel the delete that was initiated by pressing the DEL button
0
 
gecko_au2003Commented:
Go into the key press or keydown event of that component ie the data grid or w/e you are using and call the BeforeDelete function if app detects that the delete key was pressed, you will need to figure out by doing some testing what code the key ascii is when you press the delete key which you can do by doing this :

Private Sub DataGrid1_KeyPress(KeyAscii As Integer)
MsgBox KeyAscii
End Sub

I am not sure of exact coding but it will be either the keypress or keydown event, then once you know what the key code or keyascii for the delete button is you can have an select case something like the following :


Private Sub DataGrid1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case {KeyAscii For the delete button}:
Call DataGrid1_BeforeDelete()
End Select
End Sub

This is just to give you an idea, im not sure of exact coding but hopefully this points you in the right direction :)
0
 
GaryAuthor Commented:
Will have a look at that later today, busy with other stuff now
0
 
gecko_au2003Commented:
np, it will either be KeyAscii or KeyCode, depending on the event you choose to use :) also with regards to this line :

Case {KeyAscii For the delete button}:

Once you know the actual keyascii or keycode it will be the actual numerical value obviously :)

If you get stuck or need further help :) post back with ya problems :)
0
 
senthil_msvCommented:
My Answer is correct and it worked fine for me.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.