Question : The command  or action 'DeleteRecord' is not available now.

In Access 2007, I've got a function in a module that deletes the currently selected record when the Delete Record menu item is selected.   In previous versions of Access, we used SendKeys "{DELETE}", True   - that does nothing now except make the record dirty (edit icon appears).  So, I switched to DoCmd.RunCommand acCmdDeleteRecord.  That causes the error "The command or action DeleteRecord is not available now".

I've confirmed that the form allows deletions, done an undo if the form is dirty, made sure that the form is not read only. It's not a subform, it's a standalone continuous form, and the problem also occurs with single forms.

I'm stuck - any ideas that anyone's got would be much appreciated.  Thanks.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
Public Function DeleteRecord()
'This function permits record deletions when the user explicity requests a
'delete via the DeleteRecord menu item.


   On Error GoTo ErrorHandler
   Const strProcedureName = "DeleteRecord"
   Dim frm              As Form
   
   'Make sure there is an active form.
   On Error Resume Next
   Set frm = Screen.ActiveForm
   If Err.Number <> 0 Then
      GoTo ExitRoutine
   End If
   On Error GoTo ErrorHandler
   
   'If form has no RecordSource, no need to continue.
   If Format$(frm.RecordSource) = "" Then
      GoTo ExitRoutine
   End If
   
   'If this is a new record, no need to continue.
   If frm.NewRecord Then
      GoTo ExitRoutine
   End If
   
   'All if well, so do what we came here to do.
   gstrActiveFormForDeleteRecord = frm.Name
   
   'Send F2 to ensure that the user has not selected all records.
   SendKeys "{F2}", True
   
   'Select the current record (and ensure only one is selected!)
   On Error Resume Next
   RunCommand acCmdSelectRecord
   If Err.Number <> 0 Then
      GoTo ExitRoutine
   End If
   On Error GoTo ErrorHandler
   
   'Do the regular "Delete Record" menu option.
   'On Error Resume Next
   
   If frm.Dirty = True Then
    frm.Undo
   End If
   
 
   DoCmd.RunCommand acCmdDeleteRecord
   'SendKeys "{DELETE}", True

   gobjLastError.Save Err, strProcedureName
   On Error GoTo ErrorHandler
   Select Case gobjLastError.Number
      Case 0      'No error
         'Do nothing
      Case Else
         'Give a system error for anything else.
         gobjLastError.Raise gobjLastError.Number & ": " & gobjLastError.Description
   End Select
   
ExitRoutine:
   On Error Resume Next
   gstrActiveFormForDeleteRecord = ""
   Exit Function
ErrorHandler:
   gobjLastError.Save Err, strProcedureName
   Select Case gobjLastError.Number
      Case Else
         gobjLastError.Show
         Resume ExitRoutine
   End Select
End Function

Answer : The command  or action 'DeleteRecord' is not available now.

Use this instead
1:
2:
3:
4:
   Dim rs As Recordset
   Set rs = frm.Recordset
   rs.Delete
   rs.Requery
Random Solutions  
 
programming4us programming4us