Monday, July 15, 2013

VB6 DataGrid: Multiple Delete (Del Key)

Option Explicit 

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)

'-------------------------------------------------------------------------------
'http://khoiriyyah.blogspot.com
'-------------------------------------------------------------------------------

Dim DontResponseErrorTemporary As Boolean

Private Sub DeleteRows(dtGrid As DataGrid)
Dim varBmk As Variant
For Each varBmk In dtGrid.SelBookmarks
Adodc1.Recordset.Bookmark = varBmk
Adodc1.Recordset.Delete
Sleep 5 'miliseconds (as delay multiple delete animations)
dtGrid.Refresh
Next
End Sub

Private Sub DataGrid1_Error(ByVal DataError As Integer, Response As Integer)
If DontResponseErrorTemporary Then
Response = 0
DontResponseErrorTemporary = False
End If
End Sub

Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDelete Then
If Not DeleteConfirm Then
KeyCode = 0
Exit Sub
End If
DontResponseErrorTemporary = True
Call DeleteRows(DataGrid1)
KeyCode = 0
End If
End Sub

Private Function DeleteConfirm() As Boolean
If MsgBox("Are you sure want to delete this record?", vbQuestion + vbYesNo + vbDefaultButton2, "Delete Confirm") = vbYes Then
DeleteConfirm = True
End If
End Function