Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stop form from closing

Status
Not open for further replies.

laruajo

MIS
Sep 16, 2002
24
US
There is something I have been trying to figure out for quite sometime that could solve several problems I am having with my database. My question is, Is there anyway to prevent a form from closing when the 'X' button is pressed. I have code on the Before_Update() event of the form that 1. checks if the employee num that was entered was a valid employee num and 2. checks if a couple of fields have been left null. The code works and the message is displayed, however, when the user clicks ok on the error message another message comes up and says "The info can not be saved, would you still like to close the form?" If the user clicks yes, the form closes and the record is not saved (just as it says in the error message). This is causing problems within the company. Is there anyway to absolutely not allow the form to close using the 'X' until all the correct data is entered? I have used Cancel=True in the code, does not work. I have tried taking the 'X' off the form but then users just use the 'X' that closes Microsoft Access all together. I am stuck here and in need of a solution, I am just hoping one exists. Thank you in advance.

Laura
 
Hi,
Try using the code in the Form Unload event. I have been using it and it works fine. Hope it helps. Let me know what happens.
With regards,
PGK
 
This will interrupt the form's closure.


Private Sub Form_Unload(Cancel As Integer)

If MsgBox("Do you really want to close this form now?", vbYesNo, "CLOSURE CONTROL") = vbNo Then
DoCmd.CancelEvent
End If

End Sub


Regards
Rod
 
Well I did place the code in the Unload () event but something just occurred to me. What if a user opens a form on accident or decides they don't want to enter data at this time, they cannot close the form. Is there any way to detect if the form was just opened or no data has been entered and if so all the user to close the form?

tunsarod, thank you for the suggestion. I tried it but it still allowed the user to close the form, it just gave an extra error message. I appreciate the help though.

Thanks.
 
Hi,
I normally set the Cancel property to true in the Form_Unload event if I want to prevent a form from closing. You could check if the user has entered something in the mandatory fields in the form. If so, it means that the user wants to add a record and so donot allow him/her to close the form. If all the mandatory fields are empty, allow the user to close the form. Hope it helps. Let me know what happens.
With regards,
PGK
 
Well I don't want to check if all the records are null because they would not be accurate in my situation. A user could enter something and then delete it and it would appear null and the form would close. My problem is that one of my fields is an autonumber. If the user enters something, that record is started and the autonumber advances. If they enter something and then erase it that record is not entered in the table and that number is skipped in the autonumber field causing it to having missing numbers in its sequence. Is there any way to detect if something has been entered, if not allow the form to close, if there has been something entered do not allow the form to close??
 
Hi,
You could have a boolean variable like dataEntered which will be set to false when the form opens. If the user enters some data in the fields to add a record, set this variable to true. In the Unload event, you can check if this variable is true and can cancel the closing of the form if true. Display an error message to the user saying he has enter relevant data. Hope it helps. Let me know what happens.
With regards,
PGK
 
That seems like it will work, however I have a few questions. I am not very good at programming so I am sorry if these questions seem stupid. First, how do I set up a boolean variable and second, how do I set the variable to true once data is entered? Thanks.

laura
 
Ok this is what I did but it doesn't seem to work so I am sure I have something wrong. In the Open Event of the form I put: Dim dataEntered as Boolean
dataEntered = False

Then in the On_Dirty event of the form I put:

dataEntered = True

Then in the Upload_Event:

If dataEntered = True Then
Cancel=True

However, this is not working at all, what do I have wrong?
 
Thank you but Unfortunately the FAQ did not work for me. I could of done something wrong but it would not even let me close the form after all the data was entered.

I still am stuck, any help would be great.
 
Your variable must be global. In your last example just above you dim a variable in Form_Load. Your statement must be in the Form declarations are for it to work properly. It should look something like the following:

Option Explicit
Option ,etc

Public blnAllowExit As Boolean

FirstSubOrFunctionInYourCode...

In Form_Load or Form_Open

blnAllowExit = False

In ExitCommandButtonClick

blnAllowExit = True

In Form_Unload (so the Cancel parameter is exposed)

Cancel = Not blnAllowExit
DoCmd.Close acForm, me.name

Also, it appears from what you said above that if the user never enters any data that you never set the variable to true.

Good Luck!

 
Have you tried the method of just turning off the
close button from the form properties. I usually design a form to fit the window and then turn off ALL of the other things a user could use to do something wrong - record selectors, navigator, min/max buttons, close button, scroll bars - as appropriate.
Then, drop a CLOSE button on the form - that way you can control whatever you would like from the click event of the close button.

 
I do not have an exit command button on the form. I did but was informed to take it off. Is there any way that I can make this work when users use the 'X' to close the form? Also, my understanding was that I set the variable to false, then when data is entered it is set to true. I used the On_Dirty event to set it to true, because doesn't a form become "Dirty" once data is entered?? Please correct me if I am wrong. Thanks.

Laura
 
Something else I thought I should add, the date field on this form is already set to today's date. This is done in the properties of the form, I set the default value to Date(). I did not know if this would affect anything.
 
Good news!!! I think I have it figured out. I had a true where I should of had a false. I am sure I will be back if it isn't working correctly though. Thanks for all the help!!

Laura
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top