473,698 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cancel and undo commands aren't working right

105 New Member
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 9868
Rabbit
12,516 Recognized Expert Moderator MVP
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 New Member
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 Recognized Expert New Member
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 New Member
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 Recognized Expert New Member
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
3337
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 option group. If MsgBox("Change?", vbYesNo) = vbNo Then Cancel = True End If Thanks!
3
11630
by: babylon | last post by:
any facilities in csharp that can help me implmenting undo/redo in my application? thx
4
7822
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 change made in this form. Hopefully someone has a clue for me. thanks a lot!
2
11861
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 these commands and they work perfect.
1
2751
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 click event button: Me.BindingContext(ds1.Tables("MyTable")).EndCurrentEdit() Me.BindingContext(ds1.Tables("MyTable ")).AddNew() On Cancel
7
5407
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 struct */ node { char *info1;
1
4046
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 commands are not working for me. IMenuCommandService menusrv= HostControl.HostSurface.GetService(typeof(IMenuCommandService)) as IMenuCommandService; if (menusrv != null) { switch (editstring) ...
2
7967
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 captures a KeyDown event (Ctrl+D) to delete the row. I can tell if the row is new. I just don't know how to cancel the new row.
1
2362
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 Confirmed_BeforeUpdate(Cancel As Integer) If gcfHandleErrors Then On Error GoTo PROC_ERR PushCallStack "ConfirmDeployment" Dim strfield As String strfield = Me.ConsultantID & ""
0
8610
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.