Solved

Validating entry on form closure

Posted on 1998-08-31
5
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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…

705 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