Solved

Validating entry on form closure

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel if Match convert to Access 16 63
ms/access ftp / SFTP 3 32
VBA to alter Table in MS Access .mbd (2000-2003) database 5 27
Library not Registered 16 39
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
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…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

932 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

9 Experts available now in Live!

Get 1:1 Help Now