I do this with a subform in datasheet view and Delete button on main form. Here is an extract of my code behind main form:
Code:
Option Compare Database
Option Explicit
Dim intHeight As Integer 'stores value for number of tests selected for deletion
Dim intTop As Integer 'stores value for position of the first selected record in Tests recordset
Private Sub ctrTests_Exit(Cancel As Integer)
'sets module variables starting values for use in deleting selected tests
intHeight = Me.ctrTests.Form.SelHeight
intTop = Me.ctrTests.Form.SelTop
End Sub
Private Sub RemoveTests_Click()
Dim N As Integer
With Me.ctrTests.Form.RecordsetClone
If .RecordCount < 1 Then
MsgBox "No tests have been saved. Delete action canceled.", , "RemoveTest Error"
ElseIf intHeight < 1 Then
MsgBox "No tests have been selected. Delete action canceled.", , "RemoveTest Error"
ElseIf MsgBox("This action may delete any saved test data. Proceed?", vbExclamation + vbOKCancel, "Delete Test?") = vbOK Then
For N = 1 To intHeight
.AbsolutePosition = intTop - 1 'AbsolutePosition property is 0 based counter so must -1 to get position within the recordset
.MoveFirst
CurrentDb.Execute "DELETE FROM Tests WHERE LabNum='" & Me.tbxLABNUM & "' AND TestNum='" & !TestNum & "'"
Me.ctrTests.Requery
Next
End If
End With
End Sub
Code behind the subform to disable keyboard delete:
Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
'suppress default Delete Confirm dialog box.
Response = acDataErrContinue
'cancel the automatic delete operation
Cancel = True
MsgBox "Must click Remove Test button to delete test from sample."
End Sub
Making me look at this again has me thinking about changing so the delete code is behind the subform and the main form button calls the subform procedure.