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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Forcing a User to enter Data into a control before leaving it

Status
Not open for further replies.

sanan

Technical User
Apr 15, 2004
139
IR
Hi There
What is the best way to make sure that a User enters a value in a Particular form, Before Leaving it to next Control?
I know we can use the LostFocus Event of that control, But that can create problem if the user decides to close the form before Entering any value in it, as you know the MsgBox of the LostFocus events keep popping up at the time of closing.

Best Regards
Sanan
 
Have you tried setting the 'required' option in the table definition to 'YES' This will not allow you to leave that that field blank when entering data. You can set the validation rule and the data type of the field to ensure that the correct details are entered.

Open table in design view, select the field name, change the required option to yes. Repeat for all non-blank fields and save table. Your form should now not allow the user to leave the selected fields blank.

Hope that helps if I have understood your requirements correctly
 
Hi
Thanks for comment.
I prefer, that I do not use the Required Field option.
But the Validation, well it is a possibility, I used it in Excel in similar situations, But I do not know how can I use it in Access.
Could you give me a hand on it?

Best Regards
Sanan
 
I would use both the Control LostFocus Event, And the FORM UnLoad Event.

Private Sub Form_Unload(Cancel As Integer)
If IsNull(SalesID) Then Cancel = True: MsgBox "Please fill SAles ID"
End Sub

Hope this helps, good luck!
 
The best way to validate data is BeforeUpdate Event on desired control on form. It will do exactly what You want. Place Your validation code there - it won't let User to leave control before validation passes. Hope this helps.
 
I agree alfalf, I'm just under the impression that, the control needs to be filled whether someone enters the control, or not.
Otherwise, yes, beforeupdate is pretty failproof.

...So , in lieu of that, I would retract & say, use before update of the control & Unload of the form....


 
Hi
Thanks so much for your comments.
I actually tried the BeforeUpdate of a Control, But It did not work for me.
I have the following code in BeforUpdate Event of a control: (Just a test)

Private Sub InvoiceID100_BeforeUpdate(Cancel As Integer)
If IsNull(Me.InvoiceId100) Then
Me.Undo Or Cancel=true
MsgBox "try again", vbOKOnly, "info"
End If
End Sub

But after I set focus on the control I can Move away form it and leave it Empty using the Tab Key, and Nothig happens. It “sort of” works in the BeforeUpdate of the Form, But not in the control.
My objective is “ not to let a User to move away form a control with out filling in it”.

Best Regards
Sanan
 
Sanan, it is because, I believe, that the BeforeUpdate event doers not "sink", until an input has been made. Not just to enter & leave, you have enter a character.
So, you should try, OnExit or lost focus.

Private Sub Text0_Exit(Cancel As Integer)
If IsNull(Me.Text0) Then
MsgBox "try again", vbOKOnly, "info"
Cancel = True

End If
End Sub
 
Maybe on lost focus would work better, that way, if lost focus, and the control is still blank, you can restore focus and give the message.

ChaZ
 
If you want to be totally safe on all of the fields you need to put your code in the Lost Focus property of the form. You can check that all required fields have data when the user tries to move from the form.
 
put your code in the Lost Focus property of the form
This event is triggered only if the form has NO control.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi there
Thanks for all comments.
but I told you at the biginning of this thread, The LostFocus or Exit Event would work, But It creats a problem if a user decides to close the form, before entering in the control, then the Lostfocus Event's MsgBox keeps Poping up.

Best Regards
Sanan
 
Also a good idea, HiTechUser.

Sanan, I thought that was what you wanted, not allowing a person to close the form, until the text was filled?
Regardless whether they enter it or not?
 
Out of curiousity, couldn't the AfterUpdate event work better?

The value of the control is, I assume, NULL prior to getting focus. The user enters some keystrokes, the value of the control is not committed and still is NULL. Therefore, in the BeforeUpdate code, that Sanan had earlier, will almost always result in a true statement for the IsNull function.

When the user tabs out or moves off that control, the AfterUpdate event will trigger. Would that be the best time to error trap and validate the data?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top