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

How to Cancel with option Yes No

P: 56
on my form, I have a "Cancel" button which allows user to cancel updates and here is my code:
Expand|Select|Wrap|Line Numbers
  1. Dim db As Database
  2.     Dim frm As Form
  3.     Set db = CurrentDb
  4.     Set frm = Forms!frmReview
  5.  
  6.     If Me.Dirty Then 'Check to see if record has been updated.
  7.         DoCmd.RunCommand acCmdUndo
  8.         DoCmd.Close acForm, "frmReview"
  9.     Else
  10.         DoCmd.Close acForm, "frmReview"
  11.     End If
  12. End sub
  13.  
the above code will close the form without save the form when user click on the 'cancel' button. What I would like to do is to add option Yes or No when user click on Cancel button. When user chooses Yes, form will be closed without saving the changes. When user chooses No, message box close and form stay openw with the changes. Here is my code, but it doesn't work properly

Expand|Select|Wrap|Line Numbers
  1. Dim db As Database
  2.     Dim frm As Form
  3.     Set db = CurrentDb
  4.     Set frm = Forms!frmReview_V2
  5.     MsgBox "Cancel? Select Yes or No", vbYesNo, "Cancel Update!"
  6.     if vb=Yes then
  7.         DoCmd.RunCommand acCmdUndo
  8.         DoCmd.Close acForm, "frmReview"
  9.     elseif vb=No then
  10.         DoCmd.RunCommand acCmdUndo
  11.     End If
  12. end sub
  13.  
any help would be grealy appreciated.

bluemoon
Jul 6 '10 #1
Share this Question
Share on Google+
8 Replies


patjones
Expert 100+
P: 931
I think you're just a little confused about how the VB constants work. Try this...

Expand|Select|Wrap|Line Numbers
  1. Dim intCancel As Integer
  2.  
  3. intCancel = MsgBox ("Cancel? Select Yes or No", vbYesNo+vbQuestion, "Cancel Update!")
  4.  
  5. If intCancel = vbYes Then
  6.    DoCmd.RunCommand acCmdUndo
  7.    DoCmd.Close acForm, "frmReview"
  8. ElseIf  intCancel = vbNo Then
  9.    DoCmd.RunCommand acCmdUndo
  10. End If
  11.  
  12. End Sub
Jul 6 '10 #2

P: 56
@zepphead80
Hi,
I tried the code, but it doesn't seem to work properly as well. it worked the first round, but second clicks do not perform the way I want.
In addition, the No part seems to be wrong because even though I select No, the updates that I've made to the records did not stay.

thanks!

bluemoon
Jul 6 '10 #3

P: 56
@bluemoon9
I've tried this code, and it worked well with the No, but did not work for the Yes. I made some updates to the record, then click on Cancel, then choose yes, but the form still keep the updates even though I've assigned the form to be 'acSaveNo'

Expand|Select|Wrap|Line Numbers
  1. Dim db As Database
  2.     Dim frm As Form
  3.     Set db = CurrentDb
  4.     Set frm = Forms!frmReview
  5.     Dim intCancel As Integer
  6.  
  7. intCancel = MsgBox("Cancel? Select Yes or No", vbYesNo + vbQuestion, "Cancel Update!")
  8.  
  9. If intCancel = vbYes Then
  10.    DoCmd.Close acForm, "frmReview", acSaveNo
  11. ElseIf intCancel = vbNo Then
  12. End If
  13. end sub
  14.  
bluemoon
Jul 6 '10 #4

patjones
Expert 100+
P: 931
You are sort of contradicting what you first posted when you started the thread. There you were using DoCmd.RunCommand acCmdUndo to throw out the changes before closing the form. Why aren't you using it now?

Pat
Jul 6 '10 #5

P: 56
Thanks for the advice, i thought I could just use the command acSaveNo, then I do not need to use command acCmdUndo. I've placed the undo command back and it worked.
thanks!

bluemoon
Jul 7 '10 #6

nico5038
Expert 2.5K+
P: 3,072
Hi bluemoon,

I wonder why you add a confirmation popup to the Cancel button as I regard them as annoying.
Normally I use an [OK] and a [Cancel] button and when my form has many fields I also add a [Reset] button to restore the initial field values.

Only a "risky" [Delete] button will justify additional confirmation in my view.

Nic;o)
Jul 7 '10 #7

P: 56
@nico5038
I know, I would prefer the same way. That was how I programed the button in the first place; Once the user click on cancel, the form just close itself. However, my users have requested to add a code with the Y or N option because they said that sometimes they hit cancel button by mistake.

Bluemoon
Jul 7 '10 #8

nico5038
Expert 2.5K+
P: 3,072
I know this "user problem" all too well Bluemoon :-)
Guess within half a year they'll get bored and ask you to remove it ..... <LOL>

Success with your application !

Nic;o)
Jul 7 '10 #9

Post your reply

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