Solved

Validating entry on form closure

Posted on 1998-08-31
5
254 Views
Last Modified: 2012-06-27
I use the BeforeUpdate event on a form to validate data entry and cancel the event if the data is missing. This works fine when the user tries to move off the record.
My problem is if a user tries to close the form. The BeforeUpdate event traps the error however the close action still continues and the user is advised that "You can't save this object at this time .... Do you want to close?" YES/NO.

I would like to be able to get rid of this message and prevent the user from closing until the correct data is added or the user selects to undo the record then close

is this possible?
0
Comment
Question by:mutrus
5 Comments
 

Author Comment

by:mutrus
ID: 1960757
Edited text of question
0
 
LVL 10

Expert Comment

by:brewdog
ID: 1960758
What I've done is take a suggestion from a developer's handbook and refuse to let the user close the form without going through one of my buttons.

I just set up a boolean (or integer, depending on your Access version) variable like "fClose", then make it false on Form_Load. When the user clicks one of my close buttons, I make fClose true; on the Form_Unload event, I check the value of fClose -- if it's true, fine; if it's false, I cancel the unload.

brewdog
0
 
LVL 6

Expert Comment

by:devtha
ID: 1960759
What you can do is to check for each control for valid values in the ONClose event.
Basically take all before update code for each control and paste them in close event. This way you can have if statement for each control.
if text1 <> "Hello" then
msgbox "please put bla bla in text1"
docmd.cancelevent
exit sub
end if

Something like this. I am putting this here from the top of my head so please check the syntax.

0
 

Author Comment

by:mutrus
ID: 1960760
For Devtha
your answer will work only if I have a close button on the form. I use the inbuilt toolbar close button which first carries out the Before Update Event (but doesn't cancel the event), then goes on and displays the original error message (see my original question) then runs the On close Event (also doesn't cancel the event)then closes the form.

For Brewdog
I like your approach however need for infomation. I can't quite get it to work. Also how can this method be applied to stopping the form being close when the user clicks either the close window X or the close application X on the title bar(s)

Thanks
0
 
LVL 1

Accepted Solution

by:
ljb earned 50 total points
ID: 1960761
You can only cancel the unload event, not the close, so if you put devtha's code into there it will work.  You could call your before update code from there and because you can cancel the event, it will cancel if you have a docmd.cancel event in your before update event code.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question