473,480 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

BeforeUpdate vbYesNoCancel

59 New Member
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.
Mar 4 '08 #1
13 6989
Stewart Ross
2,545 Recognized Expert Moderator Specialist
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
Mar 4 '08 #2
blad3runn69
59 New Member
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
Mar 4 '08 #3
dima69
181 Recognized Expert New Member
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
Mar 4 '08 #4
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Thanks, Dima69, and apologies to Blad3runner69 for misleading you over other steps that would be needed.

-Stewart
Mar 4 '08 #5
blad3runn69
59 New Member
cool stuff, thankyou very much for your help dima69, it is muchly appreciated, no apologies needed Stewart, thank you it all helps! :)
Mar 4 '08 #6
blad3runn69
59 New Member
ah ok I need to use the unload event, sorry my bad thanks again for your help
Mar 12 '08 #7
blad3runn69
59 New Member
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...
Mar 19 '08 #8
MikeTheBike
639 Recognized Expert Contributor
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
Mar 19 '08 #9
blad3runn69
59 New Member
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 :).
Mar 19 '08 #10
blad3runn69
59 New Member
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 :)
Mar 20 '08 #11
dreidy
1 New Member
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?
Mar 20 '08 #12
jennyfifi
1 New Member
I was registered at your forum. I have printed the test message. Do not delete, please.
Aug 18 '09 #13
Frinavale
9,735 Recognized Expert Moderator Expert
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
Sep 3 '09 #14

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

Similar topics

4
4017
by: William Wisnieski | last post by:
Hello Everyone, Access 2000 I have a form with multiple pages on it. There is one text field on the third page of the form that I need the user to complete before leaving the form or moving...
4
11499
by: bob bob via AccessMonster.com | last post by:
I'm sure this is a simple one. I have a combo box that, when its data is changed, will trigger a second form to open for input. If the operator declines input, I want the combo box to return to...
9
3350
by: simonmarkjones | last post by:
I want to call a function which does this when the next record button is pressed (calling it from before update) if textboxes are empty then Message box you must fill text box
0
1634
by: Deano | last post by:
My beforeupdate event on the main form fires when a change has been made and the record has not been saved (2 variables are used to track this). Works great but if they make a change in my...
2
14789
by: PC Datasheet | last post by:
In a form/subform I have an unbound combobox in the form header that sets the value of a field in the subform so that it does not have to be entered for each record. In the BeforeUpdate event of...
6
3110
by: lorirobn | last post by:
Hi, I have a form with a continuous subform. I am working on putting validations in for the subform's required fields. Being somewhat new to Access (or rather, an antiquated mainframe...
6
7581
by: tlyczko | last post by:
I have a BeforeUpdate where I need to ensure that no matter what, the first four fields on the form (one text box, 3 combo box lists) have data entered in them before the user closes the form or...
3
3192
by: tlyczko | last post by:
Hello, I think what I'm asking for may not be possible, but I'll try anyway. I have MainForm with 3 subforms, one SubForm requires data entry, the other two don't (optional). MainForm has...
11
3432
by: ChipR | last post by:
I have a generic AfterUpdate function applied to my 14 text boxes in a continuous subform, but I can't figure out the syntax to do BeforeUpdate with Cancel. Is this even possible? Private Sub...
0
7048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7088
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
183
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.