Solved

Validating entry on form closure

Posted on 1998-08-31
5
250 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now