Connecting Tech Pros Worldwide Forums | Help | Site Map

BeforeUpdate vbYesNoCancel

Member
 
Join Date: Jul 2007
Posts: 59
#1: Mar 4 '08
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
#2: Mar 4 '08

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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Formname_BeforeUpdate(Cancel As Integer) 
  2.       Cancel=True
  3.       (other code to handle event)
  4. End Sub
-Stewart
Member
 
Join Date: Jul 2007
Posts: 59
#3: Mar 4 '08

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
dima69's Avatar
Expert
 
Join Date: Sep 2006
Location: Israel
Posts: 181
#4: Mar 4 '08

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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel  as integer)
  2. If MsgBox("Do you want to save ...") = vbYes then
  3.     NoCloseFlag = FALSE
  4. Else
  5.     NoCloseFlag = TRUE
  6. End If
  7. End Sub
  8.  
  9. Private Sub Form_Unload(Cancel  as integer)
  10. Cancel = NoCloseFlag
  11. End Sub
Moderator
 
Join Date: Feb 2008
Location: Beauly, near Inverness, Scotland
Posts: 1,576
#5: Mar 4 '08

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
#6: Mar 4 '08

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
#7: Mar 12 '08

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
#8: Mar 19 '08

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
#9: Mar 19 '08

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?
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Dim bCancelClose As Boolean
  4.  
  5. Private Sub Form_BeforeUpdate(Cancel As Integer)
  6.     If Me.Dirty Then
  7.         If MsgBox("Cancel Close?", vbYesNoCancel + vbQuestion, "Close") = vbCancel Then bCancelClose = True
  8.     End If
  9. End Sub
  10.  
  11. Private Sub Form_Unload(Cancel As Integer)
  12.     Cancel = bCancelClose
  13.     bCancelClose = False
  14. End Sub
??

MTB
Member
 
Join Date: Jul 2007
Posts: 59
#10: Mar 19 '08

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
#11: Mar 20 '08

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
#12: Mar 20 '08

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
#13: Aug 18 '09

re: BeforeUpdate vbYesNoCancel


I was registered at your forum. I have printed the test message. Do not delete, please.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#14: Sep 3 '09

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
Reply