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

Allow user to close a form without saving

beacon
100+
P: 579
Hi everybody,

I'm using Access 2003 and I have a form that has a custom cmdClose button. When pressed, a message box appears asking if the user wants to go back to a previous form.

The problem is that if the user has typed in anything, the record is getting saved and I'm trying to avoid that, if possible.

Here's my code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCancel_Click()
  2.     Dim canVal As Integer
  3.     canVal = MsgBox("Would you like to begin a new assessment?", vbYesNoCancel + vbQuestion)
  4.         If canVal = vbCancel Then
  5.             DoCmd.CancelEvent
  6.         ElseIf canVal = vbNo Then
  7.             MsgBox "You will now be returned to the Main screen", vbOKOnly + vbExclamation
  8.             DoCmd.Close acForm, Me.Name
  9.             DoCmd.OpenForm "Switchboard", acNormal
  10.         ElseIf canVal = vbYes Then
  11.             If (Me.Dirty = True) Then
  12.                 Me.Undo
  13.             End If
  14.             DoCmd.OpenForm "Switchboard", acNormal
  15.             DoCmd.Close acForm, Me.Name
  16.         End If
  17. End Sub
  18.  
Am I missing something or is this possible?

Thanks...
Sep 25 '08 #1
Share this Question
Share on Google+
11 Replies


DonRayner
Expert 100+
P: 489
You could try addig this between lines 7 & 8

DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
Sep 25 '08 #2

beacon
100+
P: 579
You could try addig this between lines 7 & 8

DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
What does that do? Does it prevent the record from getting saved? I'm not familiar with the DoMenuItem command.
Sep 25 '08 #3

missinglinq
Expert 2.5K+
P: 3,532
The DoMenuItems really should be avoided these days. They're kept for backwards compatibility. This particular one is the same as

Me.Undo

which has the added benefit of being much shoter to enter.

Linq ;0)>
Sep 25 '08 #4

missinglinq
Expert 2.5K+
P: 3,532
I'm a little confused. Is your purpose here to never save a record if this button is clicked? Currently you're not saving the record if the user responds Yes. If you place code as Don suggested you'll be dumping the record if the user chooses Yes or No.

Maybe it would help us help you if you explained your goal here in plain English.

Linq ;0)>
Sep 25 '08 #5

beacon
100+
P: 579
I'm a little confused. Is your purpose here to never save a record if this button is clicked? Currently you're not saving the record if the user responds Yes. If you place code as Don suggested you'll be dumping the record if the user chooses Yes or No.

Maybe it would help us help you if you explained your goal here in plain English.

Linq ;0)>
I have another command that I use to submit the form. That's the only command that I want to use to save the form.

This command is going to be used to return to the main menu without saving the record on the table. Basically, it's an easy way for the user of the app to "start over" without adding any wrong information.

I'm posting the code again because there was a mistake in line number 15:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCancel_Click()
  2.     Dim canVal As Integer
  3.     canVal = MsgBox("Would you like to begin a new assessment?", vbYesNoCancel + vbQuestion)
  4.         If canVal = vbCancel Then
  5.             DoCmd.CancelEvent
  6.         ElseIf canVal = vbNo Then
  7.             MsgBox "You will now be returned to the Main screen", vbOKOnly + vbExclamation
  8.             DoCmd.Close acForm, Me.Name
  9.             DoCmd.OpenForm "Switchboard", acNormal
  10.         ElseIf canVal = vbYes Then
  11.             If (Me.Dirty = True) Then
  12.                 Me.Undo
  13.             End If
  14.             DoCmd.OpenForm "frmCampusSelect", acNormal
  15.             DoCmd.Close acForm, Me.Name
  16.         End If
  17. End Sub
  18.  
Sep 25 '08 #6

missinglinq
Expert 2.5K+
P: 3,532
Then just add lines 11, 12 and 13 of your posted code between lines 7 and 8. I think you also need to either reverse lines 14 and 15, or change line 15 to replace the Me.Name in

DoCmd.Close acForm, Me.Name.

with the actual name of your first form. Once you open frmCampusSelect it becomes the Me of Me.Name, so you need to explicitly name the form you're closing.

Linq ;0)>.
Sep 25 '08 #7

beacon
100+
P: 579
Then just add lines 11, 12 and 13 of your posted code between lines 7 and 8. I think you also need to either reverse lines 14 and 15, or change line 15 to replace the Me.Name in

DoCmd.Close acForm, Me.Name.

with the actual name of your first form. Once you open frmCampusSelect it becomes the Me of Me.Name, so you need to explicitly name the form you're closing.

Linq ;0)>.
So, theoretically, couldn't I just swap line 14 and 15 to ensure that the correct form is closed?

Also, should I put the...
Expand|Select|Wrap|Line Numbers
  1. if (me.dirty = true) then
  2.      me.undo
  3. end if
  4.  
...in each section of the conditional statements to ensure that the form is clear if the user returns to it?

And, just to be sure...if the user selects the Close command, the form should close without saving, right? Is it a matter of having information in the form when the form is closed that determines whether or not a row gets saved?
Sep 25 '08 #8

beacon
100+
P: 579
I think I answered my own question. I decided to reword the first message box to reduce the number of options the user has, which subsequently reduced the number of conditions I have to code for.

Here's what I ended up with, in case some needs it in the future:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCancel_Click()
  2.     Dim canVal As Integer
  3.     canVal = MsgBox("You have elected to close the form without saving." & vbCr & vbCr & "Are you sure you want to continue?", vbYesNo + vbExclamation, "Close Form")
  4.         If canVal = vbNo Then
  5.             DoCmd.CancelEvent
  6.         ElseIf canVal = vbYes Then
  7.             MsgBox "You will now be returned to the Main screen", vbOKOnly + vbInformation, "Returning to Main"
  8.             If (Me.Dirty = True) Then
  9.                 Me.Undo
  10.             End If
  11.             DoCmd.Close acForm, Me.Name
  12.             DoCmd.OpenForm "Switchboard", acNormal
  13.         End If
  14. End Sub
  15.  
Thanks for the help everybody...I'll probably be back in the next few days with some more questions once I stumble upon them.

Until then...
Sep 25 '08 #9

missinglinq
Expert 2.5K+
P: 3,532
"So, theoretically, couldn't I just swap line 14 and 15 to ensure that the correct form is closed?"

If you re-read the post, you'll see that's what I said.

"And, just to be sure...if the user selects the Close command, the form should close without saving, right?"

Yes, anytime you invoke Me.Undo it deletes any data entered, if a new record, and any data added since the last "save" if it's a previously existing record.

"Is it a matter of having information in the form when the form is closed that determines whether or not a row gets saved?"

If a new record has been created or an existing record edited, closing the form, in most cases, will result in the record being saved. Notice that I said in most cases!

One of many quirks Access has involves the use of DoCmd.Close to close a form. If a table has one or more fields that are defined as required or you have validation code in your form to insure that fields are populated or that their data is formatted in a certain way, and they do not conform to these validations or requirements, and you use DoCmd.Close, Access will dump the record, without issuing any warnings that is about to do so, and then close the form!

In your case this doesn't matter, because you want the record dumped. But in a situation where you wanted the record saved and want a warning message if the data doesn't comply, you need to always explicitly save the record before closing the form.

Expand|Select|Wrap|Line Numbers
  1. If Me.Dirty = True Then Me.Dirty = False
  2. DoCmd.Close
When the record is saved, warnings about required fields or validation errors will be shown, allowing for correction before closing the form.

Your latest code looks like it should work for you just fine, now.

Linq ;0)>
Sep 26 '08 #10

beacon
100+
P: 579
Sorry Linq...I read that you said to reverse lines 14 and 15, but I don't think it registered that that's what you meant. It took me trying what you suggested for it to make sense, so please don't think I was being a jerk.

Thank you for all of your help. I know this post may have seemed a little strange, but those are the kinds of things I get to deal with when my company wants to change from using something like FoxPro to Access, but still want the exact same functionality.

Ciao for now...
Sep 26 '08 #11

missinglinq
Expert 2.5K+
P: 3,532
No problem, beacon! "Suits" that want to use one product but make it look exactly like another product are the bane of all developers, along with those who don't know squat about programming but are going to tell you exactly how you need to do something!

Linq ;0)>
Sep 26 '08 #12

Post your reply

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