473,792 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cancel won't close a form if no changes have been made

105 New Member
Hi everyone!!

I have a problem. I have a form for entering new data. If the user clicks the button on my frmMain (cmdAdd), the form for entering new data appears. However, IF the user decides NOT to enter any data and click the cancel button instead of the Save button and error pops up. How do I keep this from happening? I've implemented some error checking but still no results. Here's my code...

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCancel_Click()
  2. On Error GoTo errHandle
  3. Dim intProcced As Integer
  4.  
  5. 'Warn the user that any information entered will be lost, then if the user responds that it is
  6. 'ok, erase the record and close the form
  7.  
  8. intProcced = MsgBox("Warning: Any changes made or any information entered will not be saved.", vbOKCancel, "Warning")
  9.     If intProcced = 2 Then
  10.         Exit Sub
  11.     End If
  12.  
  13. DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer1X
  14.  
  15. DoCmd.Close
  16.  
  17. Exit Sub
  18.  
  19. errHandle:
  20. If Err.Number = 2046 Then
  21.     DoCmd.Close
  22.     Exit Sub
  23. Else
  24.     'Print the error message and send an email to Administrator
  25.     SendErrorMsg "cmdCancel_Click on frmEnterNewFeeder", Err.Number, Err.Description
  26.     MsgBox Err.Description
  27. End If
  28. Exit Sub
  29.  
  30. End Sub
Thanks for any and all help!!!
Mar 10 '08 #1
2 2411
Stewart Ross
2,545 Recognized Expert Moderator Specialist
...
Expand|Select|Wrap|Line Numbers
  1. DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer1X
  2. ...
Hi. When no change has been made there is nothing to undo, and executing the Docmd causes an error.

You can test specifically whether or not a change has been made to the underlying record using the form's Dirty property. You can also substitute the form's Undo method for the menu command you have used. The revised undo code is then just
Expand|Select|Wrap|Line Numbers
  1. If Me.Dirty then
  2.     Me.undo
  3. Endif
-Stewart
Mar 10 '08 #2
jmarcrum
105 New Member
Hi. When no change has been made there is nothing to undo, and executing the Docmd causes an error.

You can test specifically whether or not a change has been made to the underlying record using the form's Dirty property. You can also substitute the form's Undo method for the menu command you have used. The revised undo code is then just
Expand|Select|Wrap|Line Numbers
  1. If Me.Dirty then
  2.     Me.undo
  3. Endif
-Stewart
Hey thanks! That worked great!

-Joseph
Mar 10 '08 #3

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

Similar topics

4
7100
by: Corepaul | last post by:
I am working in Windows 2000 and Access 2000 and am a rookie in Access. Coding merrily along, I stop periodically to test that everything works. After adding a small amount of code to several events, when I try to run the form suddenly won't run. I get the message "The expression On Open you entered as the event property setting produced the following error: Constant Expression Required." Two possible reasons follow on the screen:
4
1831
by: Deano | last post by:
Alot of my forms are bound. I would like to offer a Cancel option so that they can make as many changes as they like and still Cancel out without making any changes. I have one idea of how to do this for new records; tag each record with a boolean value. On clicking OK, these are all set to True and the form closes. If they Cancel the False records are deleted and the form closes.
3
2778
by: Charles Law | last post by:
Under what circumstances would e.Cancel be set to True on entry to the Closing event of an MDI child form? I have found that this is why my application won't close properly. I can explicitly set the value to False, but I would have expected it to be False on entry. TIA Charles
4
7828
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!
1
1789
by: bg_ie | last post by:
Hi, I have an event handler to handle the closing of a form. In this handler (Form1_FormClosing), I ask the user if they wish to save updates made to a file. I wish to add the option to the message box to allow the user to cancel so that the form doesn't actually close. This is the behaviour you would see if you closed a word document - Save changes to X.doc - Yes,No,Cancel. Thanks for your help,
2
11870
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.
2
30690
gregerly
by: gregerly | last post by:
I'm working on a corporate intranet site and am trying to create a page that will warn a user on window unload if they have not saved thier work. Basically I just select all inputs, and on change, set var changes = true; On window unload if changes==true, they have made changes that are not yet saved, and I want a confirm box to pop up, asking if they really want to leave or stay on the page? At this point is it to late to cancel the window...
8
4801
by: jmarcrum | last post by:
Hello all, i have a continuous form that displays about 1000 records and opens when I click a button. When the form opens, the user has the option of checking a checkbox that is located beside each record. The checkbox is labeled "Move" (for moving multiple records at a time to another year). Of course, the checkbox is set as a control "moveRecord" on a table that the query is pulling from. So if the user checks one record, then clicks to...
5
9882
by: jmarcrum | last post by:
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: Private Sub cmdCancel_Click()...
0
9518
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
10211
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10159
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
9033
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
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
5436
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...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4111
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
3
2917
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.