It's a little surprising that the code worked in any circumstances as the Click event procedure does not have a Cancel argument. The error is understandable therefore, as is the solution of declaring the variable I can only assume that the class module
of the form in which it is working is missing the Option Explicit line from its declarations area, removing the necessity to declare variables.
More to the point, however, is that it is not necessary to include the Cancel variable in the procedure at all as it does nothing. You appear to be not actually deleting a record here, but assigning a Null to a FIL_NME_FST control to remove one item of data
only, so if the user responds No to the prompt a Null will not be assigned to the control, so in effect the 'deletion' will be cancelled without having do anything else.
So just remove any reference to the Cancel variable from the procedure and it should work as intended.
For an example of where you would set a return value for a Cancel argument, the following is some code from a form's module which deletes the current record at a button click. Note how the return value of the form's BeforeDelConfirm is set to True if
the deletion is not confirmed.
The button's event procedure firstly undoes any current edits if the record is unsaved. It then attempts to delete the record, but this might not be possible if the record is a new one which has never been saved, so the error handling detects and ignores this.
One thing about this code which might not be apparent at first sight is that the BeforeDelConfirm event procedure executes immediately after the line RunCommand acCmdDeleteRecord in the button's event, and the code execution in the button's event procedure
then resumes, so the DELETE_CANCELLED error is a result of the user not having confirmed the deletion in the BeforeDelConfirm event procedure, not the user's response to the earlier prompt in the button's event procedure.
Private Sub cmdDelete_Click()
Const DELETE_CANCELLED = 2501
Const CANT_DELETE = 2046
Const MESSAGETEXT = "Are you sure you wish to delete the current record?"
If Me.Dirty Then
If MsgBox(MESSAGETEXT, vbYesNo + vbQuestion, "Confirm") = vbYes Then
Me.Undo
End If
End If
On Error Resume Next
RunCommand acCmdDeleteRecord
Select Case Err.Number
Case 0
' no error
Case DELETE_CANCELLED
' anticipated error, so ignore
Case CANT_DELETE
' anticipated error, so ignore
Case Else
' unknown error, so inform user
MsgBox Err.Description & " (" & Err.Number & ")", vbExclamation, "Error"
End Select
End Sub
Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
Const MESSAGETEXT = "Are you sure you wish to delete the current record?"
If MsgBox(MESSAGETEXT, vbYesNo + vbQuestion, "Confirm") = vbNo Then
Cancel = True
End If
Response = acDataErrContinue
End Sub