Prevent from closing without key

IR8USLO

Registered User.
Local time
Today, 21:11
Joined
Mar 14, 2001
Messages
13
I've searched the archives and I can seem to get the key words right...I can't find what I'm looking for.

I would like to prevent my form from exiting if the keyfield linking my main form to my subform is left blank. I could either set the cursor back into the key field or give the user the option to delete the subrecord if they do not know what the key field data is at the moment. But I don't have the option. If someone tried to advance to the next record they will get my prompt to fill in the field. If they close out the form it will jest give them an error message and ask them if they want to save, Yes/No.

SO....my idea is to remove the close and minimize options from the form, have the message to prompt them to fill in the field, have a command button check for the key field and provide an additional message that tells the user that they can not close out until the field IsNotNull.......

How to I place the cursor back into the mainform key field?
If the user does not know the information that should be inthe key field....how do I give them the option to delete the record in the subform...so that they can close out of the form?
 
1. To place the cursor somewhere use : Me.Controls("YourControlName").SetFocus

2. Let the user cancel the new record:
From the field beforeupdate event, test for the value entered using something like:
If Len(Nz(Me.Controls("YourControlName").Text,""))=0 then
If MsgBox (...)=vbCancelthen
Docmd.CancelEvent
DoCmd.Undo
End If
End If

3. I do not believe complementary measures are necessary if you do the test and have the user either complete or cancel data entry from the BeforeUpdate event of your control. However, to prevent the user from closing the form you could:
From the OnClose event of your form test for the value in your control and cancel the event if the control was left blank:
If len(nz(me.Controls("YourControlName"),""))=0 then docmd.CancelEvent
 
Last edited:

Users who are viewing this thread

Back
Top Bottom