bcooler
Programmer
- Jun 13, 2009
- 132
Ok, this one is really weird. I am having problems deleting a record using my own code, so I:
1.) created a temporary command button using the wizard) to delete a record
2.) ran it a few times (worked)
3.) pasted this code into my code and ran it. The difference being that my code has a small if/then structure to ask the user if they are sure (lines 1-3 and last one before the end).
At this point, I started seeing a problem. It started giving me errors when trying to find the previous control saying it couldn't find it.
I said, "Ok, I don't really know why I need to move to the previous control anyway, so I'll comment it out." I did this, then got a Error 2046 "The command or action 'DeleteRecord' isn't available now." during the DoCmd.RunCommand acCmdDeleteRecord action.
So, I can turn on and off this error by commenting out my portions of the code (turning the code back to the code given by the wizard).
Any ideas what I am doing wrong? I need to ask before I delete the file, so I don't just want to do it as given by the command wizard.
1.) created a temporary command button using the wizard) to delete a record
2.) ran it a few times (worked)
3.) pasted this code into my code and ran it. The difference being that my code has a small if/then structure to ask the user if they are sure (lines 1-3 and last one before the end).
At this point, I started seeing a problem. It started giving me errors when trying to find the previous control saying it couldn't find it.
I said, "Ok, I don't really know why I need to move to the previous control anyway, so I'll comment it out." I did this, then got a Error 2046 "The command or action 'DeleteRecord' isn't available now." during the DoCmd.RunCommand acCmdDeleteRecord action.
So, I can turn on and off this error by commenting out my portions of the code (turning the code back to the code given by the wizard).
Any ideas what I am doing wrong? I need to ask before I delete the file, so I don't just want to do it as given by the command wizard.
Code:
Private Sub cmdCancel_Click()
If MsgBox("Do you wish to cancel this entry?", vbYesNo, "Cancel Confirmation") = vbNo Then
Exit Sub
Else
'On Error Resume Next
DoCmd.GoToControl Screen.PreviousControl.Name
'Err.Clear
If (Not Form.NewRecord) Then
DoCmd.RunCommand acCmdDeleteRecord
End If
If (Form.NewRecord And Form.Dirty) Then
DoCmd.RunCommand acCmdUndo
End If
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
DoCmd.Close , ""
End If
End Sub