How to stop a form from closing if some essential fields are not populated

blogmad

Registered User.
Local time
Today, 07:52
Joined
Feb 24, 2006
Messages
16
I tried putting the following code in the form unload procedure, but although it does generate the correct message, it doesnt stop the form from closing.

Private Sub Form_Unload(Cancel As Integer)
If [Starttime] > 0 And [Admin_time] = 0 Then
response = MsgBox("Please click on the stop button to stop the clock")
Exit Sub
End If
end sub

If the above condition holds, I want the user to click on the stop button before they close down the form. I'm guessing that the form is already commited to closing before the unload event? :o
 
Use the BeforeUpdate event and Cancel=True if the condition isn't met
 
Thanks for that Rich,
I get a message saying that there is an error ...if you close object now, the data will be lost. I presume this is the generic message you get if the cancel state is true. Is it possible to replace the message with a more user friendly one, do you think?
 
why not just make the fields "required" in your tables? that would prompt you to fill the field before it closes.
 
thanks Mikk, but that doesnt really suit my purposes. The field in question can be a 0 value in certain circumstances, so what I want to do is set a condition that executes before the user closes the form. Here is the code, if that makes it clearer to you.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If [Starttime] > 0 And [Admin_time] = 0 Then

Cancel = True
End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom