Undo Action on Command Button

StacyStacy

A "Californian" at heart!
Local time
Today, 12:52
Joined
Jan 29, 2003
Messages
159
I think this is rather simple, but I created an 'undo' button via the wizard. When pressed in the form, it does not work and gives an error msg: "The command or action 'Undo" isn't available now".

Here's my code:

Private Sub cancel_record_and_return_to_menu_Click()
On Error GoTo Err_cancel_record_and_return_to_menu_Click


DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
Me.Undo
Cancel = True

Exit_cancel_record_and_return_to_menu_Cl:
Exit Sub

Err_cancel_record_and_return_to_menu_Click:
MsgBox Err.Description
Resume Exit_cancel_record_and_return_to_menu_Cl

End Sub


I added the following and it still did not help:

Me.Undo
Cancel=True

Thanks a million!
 
That is because the record is not dirty and there is no undo neccessary. Try this instead...
Code:
Private Sub bUndo_Click()
On Error GoTo Err_bUndo_Click
    
    'Resets the record if it has been modified by the user.
    If Me.Dirty Then
        Beep
        DoCmd.RunCommand acCmdUndo
        Form_Current
    Else
        Beep
        MsgBox "There were no modifications made to the current record.", vbInformation, "Invalid Undo"
    End If
    
Exit_bUndo_Click:
    Exit Sub
    
Err_bUndo_Click:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_bUndo_Click
    
End Sub
HTH
 
I had an error message with the following command:

Compile Error: Sub or Function Not Defined:

Form_Current


What should I do?
 
Please post your code (all of it for that sub) and also the name of the command button that you assigned the code to and we will look at it.
 
Here's the code again:

Private Sub cancel_record_and_return_to_menu_Click()
On Error GoTo Err_cancel_record_and_return_to_menu_Click


DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
Me.Undo
Cancel = True

Exit_cancel_record_and_return_to_menu_Cl:
Exit Sub

Err_cancel_record_and_return_to_menu_Click:
MsgBox Err.Description
Resume Exit_cancel_record_and_return_to_menu_Cl

End Sub
 
That's a rather long name for a command button...

A few things:

1. Your event is a command button click which does not have the option to Cancel the event.

2. The DoMenuItem command is outdated and can be replaced by: DoCmd.RunCommand acCmdDeleteRecord

3. From the code given previously, it is obvious that you have no Current event on your form. Remove the line: Form_Current.

It should work then.
 
Stacey's trying to undo the record, not delete it Mile
If Me.Dirty Then
Me.Undo
Else
MsgBox"there is nothing to undo"
End If
 
Jeez, my eyes are more tired than yours and AO's rich - maybe I need to start napping during the day too. :rolleyes:
 
You guys are cute! Is it that indepth to assign a button to cancel anything that was keyed in a form that also contains many required fields?

Also, let's add fuel to fire: How can I disable functions while they are entering data, such as, advancing to the next record accidently? Isn't this coded in the 'form' properties?

Thanks
 
StacyStacy said:
You guys are cute!


Even Rich? :confused:


StacyStacy said:
Is it that indepth to assign a button to cancel anything that was keyed in a form that also contains many required fields?

The fact that you are undoing with Me.Undo clears all chanes to the form; not just to a control. If you wanted to cancel a specific control, you'd use Me.ctlControl.Undo

StacyStacy said:
Also, let's add fuel to fire: How can I disable functions while they are entering data, such as, advancing to the next record accidently? Isn't this coded in the 'form' properties?

Set your form's Cycle property to CurrentRecord.
 

Users who are viewing this thread

Back
Top Bottom