BeforeUpdate vbYesNoCancel | Member | | Join Date: Jul 2007
Posts: 59
| | |
vbYesNoCancel msgbox function gets called from sub Form_BeforeUpdate if user clicks form exit button or the form window X button and the form is dirty.
Howto halt the exit sub (ie. form close) if the user clicks canel?
thanks.
| | Moderator | | Join Date: Feb 2008 Location: Beauly, near Inverness, Scotland
Posts: 1,576
| | | re: BeforeUpdate vbYesNoCancel Quote:
Originally Posted by blad3runn69 vbYesNoCancel msgbox function gets called from sub Form_BeforeUpdate if user clicks form exit button or the form window X button and the form is dirty.
Howto halt the exit sub (ie. form close) if the user clicks canel?
thanks. Hi. Set the Cancel parameter to True within your BeforeUpdate event code: - Private Sub Formname_BeforeUpdate(Cancel As Integer)
-
Cancel=True
-
(other code to handle event)
-
End Sub
-Stewart
| | Member | | Join Date: Jul 2007
Posts: 59
| | | re: BeforeUpdate vbYesNoCancel
Hi Stewart, thank you for your help, you have helped me more than you know :).
If me.dirty then...
I insert Cancel = True into the beforeUpdate sub it doesn't work
the results are
1. close through form exit button sub
cancel still closes form,
if I try to put Cancel = True into the exit button sub I get a variable not defined error
2. close through window x form button
prompts msgbox You can't save the record @ this time... close form yes/no (close :))
thank you for your help
|  | Expert | | Join Date: Sep 2006 Location: Israel
Posts: 181
| | | re: BeforeUpdate vbYesNoCancel Quote:
Originally Posted by blad3runn69 Hi Stewart, thank you for your help, you have helped me more than you know :).
If I insert Cancel = True into the beforeUpdate sub it doesn't work
the results are
1. close through form exit button sub
cancel still closes form,
if I try to put Cancel = True into the exit button sub I get a variable not defined error
2. close through window x form button
prompts msgbox You can't save the record @ this time... close form yes/no (close :))
thank you for your help In order to prevent form closing you have to use Form_Unload event (it has Cancel argument). For instance: - Private Sub Form_BeforeUpdate(Cancel as integer)
-
If MsgBox("Do you want to save ...") = vbYes then
-
NoCloseFlag = FALSE
-
Else
-
NoCloseFlag = TRUE
-
End If
-
End Sub
-
-
Private Sub Form_Unload(Cancel as integer)
-
Cancel = NoCloseFlag
-
End Sub
| | Moderator | | Join Date: Feb 2008 Location: Beauly, near Inverness, Scotland
Posts: 1,576
| | | re: BeforeUpdate vbYesNoCancel
Thanks, Dima69, and apologies to Blad3runner69 for misleading you over other steps that would be needed.
-Stewart
| | Member | | Join Date: Jul 2007
Posts: 59
| | | re: BeforeUpdate vbYesNoCancel
cool stuff, thankyou very much for your help dima69, it is muchly appreciated, no apologies needed Stewart, thank you it all helps! :)
| | Member | | Join Date: Jul 2007
Posts: 59
| | | re: BeforeUpdate vbYesNoCancel
ah ok I need to use the unload event, sorry my bad thanks again for your help
| | Member | | Join Date: Jul 2007
Posts: 59
| | | re: BeforeUpdate vbYesNoCancel
meh... it seems ypu have to continually program agains the inherent nature of access, it is a data monster, still looing for a simple beforeupdate vbyesncancel...
| | Expert | | Join Date: Jun 2007 Location: Derbyshire, UK
Posts: 347
| | | re: BeforeUpdate vbYesNoCancel Quote:
Originally Posted by blad3runn69 meh... it seems ypu have to continually program agains the inherent nature of access, it is a data monster, still looing for a simple beforeupdate vbyesncancel... Hi
Not sure precisly what you are looking for but, following on from previous post, might this help? - Option Compare Database
-
Option Explicit
-
Dim bCancelClose As Boolean
-
-
Private Sub Form_BeforeUpdate(Cancel As Integer)
-
If Me.Dirty Then
-
If MsgBox("Cancel Close?", vbYesNoCancel + vbQuestion, "Close") = vbCancel Then bCancelClose = True
-
End If
-
End Sub
-
-
Private Sub Form_Unload(Cancel As Integer)
-
Cancel = bCancelClose
-
bCancelClose = False
-
End Sub
??
MTB
| | Member | | Join Date: Jul 2007
Posts: 59
| | | re: BeforeUpdate vbYesNoCancel
thanks for your help Mike The Bike, Stewart and dima69.
simple in theory, a form has exit/X buttons.
if the user clicks exit/X and the form is dirty then
msgbox 'save before exit?', yesnocancel
choosing cancel closes msgbox and returns user to the form in an unchanged state (eg. close notepad when there are unsaved changes & you are prompted with yesnocancel, click cancel the msgbox closes, no save, no close and no undo).
howto incorporate cancel into events? (holy moly batman :)
Thank you all again for all your help, I will look at your ideas+code, it is always muchly appreciated :).
| | Member | | Join Date: Jul 2007
Posts: 59
| | | re: BeforeUpdate vbYesNoCancel bump... 'alternativley, you can use a MsgBox with vbYesNoCancel, and handle the 3 states. In my view, that's less clear, but you can do it.'
helpa newbie yoda :)
| | Newbie | | Join Date: Mar 2008
Posts: 1
| | | re: BeforeUpdate vbYesNoCancel
Hi,
I am having a similar issue.
I have a form linked to a table. One of the fields is required. If the user tries to leave the record (either by navigating to another record or by closing the form) without entering information for that field, i post a message asking if they want to continue (and lose the data) or cancel (and remain in the form on that record so they can enter the required information). If the user continues, the BeforeUpdate event works properly, using a Me.Undo. However, if the user closes the form (X button) and gets the message and decides to cancel the action of closing the form so they can enter the required information, they get an error. The message states that they will need to fill in [table].[required_field] because it can not be set to null.
I tried Cancel = True in the BeforeUpdate event, and then i tried a suggestion from this thread by setting the Form_Unload event, Cancel = True. None of this seems to be working. Any other suggestions?
| | Newbie | | Join Date: Aug 2009
Posts: 1
| | | re: BeforeUpdate vbYesNoCancel
I was registered at your forum. I have printed the test message. Do not delete, please.
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,137
| | | re: BeforeUpdate vbYesNoCancel
Hi Jennyfifi,
Your questions are welcome here on bytes, but if you post links to websites as your signature you will not be able to participate on this forum since this is considered to be spam and will not be tolerated. Please refrain from posting links as your signature.
I hope you find all the help you need here.
-Moderator Frinny
|  | Similar Microsoft Access / VBA bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|