473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

cancel and undo commands aren't working right

105 100+
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
5 9821
Rabbit
12,516 Expert Mod 8TB
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
jmarcrum
105 100+
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
108 Expert 100+
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
jmarcrum
105 100+
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
108 Expert 100+
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

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

Similar topics

2
by: Corrine | last post by:
Is there a way to cancel a change in an option group? The option group still changes to the option clicked on when clicking on the NO button with the following code in the BeforeUpdate event of the...
3
by: babylon | last post by:
any facilities in csharp that can help me implmenting undo/redo in my application? thx
4
by: martin | last post by:
Hello, Is there a way to make a kind of "cancel" button on a form? Suppose you accidently changed or overwrote some data in a form, then I'd like to leave this form at once and cancel any...
2
by: Matuag | last post by:
Hi All, I want to create following command buttons on a Form which users can edit. Save ( Save Changes made) Cancel ( Undo data changes) Exit ( Close form) I am using Macros for each of...
1
by: inadsad | last post by:
Greetings Group: I got a form with multiple controls that are bound to a dataset, with two buttons & next/previous. Simply, add button adds a new record and cancel button undo changes. On add...
7
by: call_me_anything | last post by:
Hi, I am looking for a good algorithm (in C/C++) for implementing undo/ redo on a data structure. The data structure is basically a n-children tree somewhat of the form : class /* or...
1
by: anupam roy | last post by:
Hi All, I want to perform basic Edit menu functionalities on my custom design surface. While all the Cut/Copy/Paste/Deelete/Select functionalities working fine with code below,Undo/Redo standard...
2
by: Paul | last post by:
How can you cancel a row insert programmatically? I'm not talking about the Form_BeforeInsert(Cancel As Integer). I have a continuous form. A new row is started. Code for the continuous form...
1
by: pelicanstuff | last post by:
Below is my code. Me.Confirmed is a checkbox. Sometimes the cancel at line 37 works when the user clicks "no", someetimes it doesn't. Is this my fault or someting else? Private Sub...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.