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

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

100+
P: 105
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
Share this Question
Share on Google+
2 Replies


Expert Mod 2.5K+
P: 2,545
...
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

100+
P: 105
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

Post your reply

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