I have tried this on my form but it always comes up with the message "The command or action 'DeleteRecord' isn't available now" - Any ideas?
This will happen with a new record, i.e. a record that hasn't been saved yet. There are a few ways you could handle the UI, but I think this should work:
Private Sub Form_Current()
HandleEnabling
End Sub
Private Sub Form_AfterUpdate()
HandleEnabling
End Sub
Private Sub HandleEnabling()
Me.btnDelete.enabled = not Me.NewRecord
' enable/disable other controls here...
End Sub
Edit: I think you can also get this error if you try to delete a dirty record. You may need to put Me.Refresh just before the DoCmd.RunCommand acCmdDeleteRecord. Also, if it's a new record, calling Me.Refresh will save the record, so if you do that, you can ignore the part above about disabling the button...
Something like this should do the trick...
Private Sub btnDelete_Click()
If MsgBox("Are you sure you want to permanently delete this Record?", vbYesNo + vbDefaultButton2 + vbQuestion, "My App") = vbYes Then
Me.Refresh
' If the record is completely blank, refresh won't do anything, but there should be nothing to delete...
If Me.NewRecord Then Exit Sub
DoCmd.RunCommand acCmdDeleteRecord
End If
End Sub