By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,940 Members | 614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,940 IT Pros & Developers. It's quick & easy.

cancel and undo commands aren't working right

100+
P: 105
Hey everybody!

I've created 2 buttons that I've placed on all my forms: a Save button & a Cancel button.

The cancel button I have a question about.

I used the wizard to create the button and I selected the Record Operations/Undo Record option. I edited the vba code to have the button also close the form after it undoes any changes. Here is the actual code for the OnClick event for the cancel button:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCancel_Click()
  2. On Error GoTo errHandle
  3.  
  4. Dim intProcced As Integer
  5.  
  6. 'Warn the user that any information entered will be lost, then if the user responds that it is
  7. 'ok, erase the record and close the form
  8.  
  9. intProcced = MsgBox("Warning: Any changes made or any information entered will not be saved.", vbOKCancel, "Warning")
  10.     If intProcced = 2 Then
  11.         Exit Sub
  12.     End If
  13.  
  14. DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer1X
  15.  
  16. DoCmd.Close
  17.  
  18. Exit Sub
  19.  
  20. errHandle:
  21. If Err.Number = 2046 Then
  22.     DoCmd.Close
  23.     Exit Sub
  24. Else
  25.     'Print the error message and send an email to Administrator
  26.     SendErrorMsg "cmdCancel_Click on frmEnterNewFeeder", Err.Number, Err.Description
  27.     MsgBox Err.Description
  28. End If
  29.  
  30. Exit Sub
  31.  
  32. End Sub
The problem I am having is when someone gets into the form and makes NO changes (doesn't type anything at all). If they click the Cancel button at that point, this message pops up:
"The command or action Undo isn't available now."

Which is true, nothing was done to "undo". What I want the button to do, is undo only if a change has been made. How can I (in the vba code) check first that a change has been made on the form and only execute the undo if a change has been made, otherwise I just want to close the form.

The Error checking and check for the error condition and ignore it code that I have there doesn't work!

Thank so much for your help!
Jan 11 '08 #1
Share this Question
Share on Google+
5 Replies


Rabbit
Expert Mod 10K+
P: 12,357
The error trapping should work if that's the right error number. You could also try checking to see if it's dirty first.
Jan 11 '08 #2

100+
P: 105
for some reason, the error trapping isn't working correctly, is there something else I could try with the errhandle? That onDirty command you mentioned...how would I do that?
Jan 14 '08 #3

Minion
Expert 100+
P: 108
Try altering the top half of your code to read the following:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCancel_Click()
  2. On Error GoTo errHandle
  3.  
  4. Dim intProcced As Integer
  5.  
  6. 'Warn the user that any information entered will be lost, then if the user responds that it is
  7. 'ok, erase the record and close the form
  8.  
  9. '------------------------------------
  10. ' Add this piece here   
  11. ' -----------------------------------
  12. If Me.Dirty = True Then
  13. DoCmd.Close acForm
  14. Exit Sub
  15. End IF
  16. ' ==================
  17.  
  18.  
  19.  
  20. intProcced = MsgBox("Warning: Any changes made or any information entered will not be saved.", vbOKCancel, "Warning")
  21.     If intProcced = 2 Then
  22.         Exit Sub
  23.     End If
  24.  
See if that does the trick for you.

- Minion -

for some reason, the error trapping isn't working correctly, is there something else I could try with the errhandle? That onDirty command you mentioned...how would I do that?
Jan 14 '08 #4

100+
P: 105
Hey thanks Minion,

Your tip worked beautifully. I ended up modifying my code to look like this...

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCancel_Click()
  2. On Error GoTo errHandle
  3.  
  4. Dim intProcced As Integer
  5.  
  6. 'Warn the user that any information entered will be lost, then if the user responds that it is
  7. 'ok, erase the record and close the form
  8.  
  9. intProcced = MsgBox("Warning: Any changes made or any information entered will not be saved.", vbOKCancel, "Warning")
  10.     If intProcced = 2 Then
  11.         Exit Sub
  12.     End If
  13.  
  14. If Me.Dirty = True Then
  15.     DoCmd.SetWarnings False
  16.     DoCmd.OpenQuery "qryMoveFeedersFalse"
  17.     DoCmd.SetWarnings True
  18.     DoCmd.Close
  19.     Exit Sub
  20. Else
  21.     DoCmd.SetWarnings False
  22.     DoCmd.OpenQuery "qryMoveFeedersFalse"
  23.     DoCmd.SetWarnings True
  24.     DoCmd.Close
  25.     Exit Sub
  26. End If
  27.  
  28. errHandle:
  29. If Err.Number = 2046 Then
  30.     DoCmd.Close
  31.     Exit Sub
  32. Else
  33.     'Print the error message and send an email to Administrator
  34.     SendErrorMsg "cmdCancel_Click on frmEnterNewFeeder", Err.Number, Err.Description
  35.     MsgBox Err.Description
  36. End If
  37.  
  38. Exit Sub
  39.  
  40. End Sub
Jan 14 '08 #5

Minion
Expert 100+
P: 108
I'm glad to hear that it got you moving in the right direction. Sometimes all we need to find the answer is a nudge.

- Minion -
Jan 14 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.