devxlogo

Validating Input Data in LostFocus

Validating Input Data in LostFocus

Question:
I have two fields on the screen that I use to enter a date range. I want to check the entry on the lost focus with IsDate, and in the event that IsDate = False, inform the user, set the Focus back on the field, and ExitSub before I continue. Since the ending date field follows in the tab sequence, the resulting behavior is that it shows a message box from each control that that date does not work. To the user it just looks as if ithe same message box comes up twice. Any suggesstions, and will VB 4.0 work better.

Answer:
There’s an inherent problem in what you are doing. LostFocus is a good place to do validation, as you’ve already seen. Unfortunately, you can’t set focus back to the box for the following reason: let’s say that the user pressed something into the box, got an error, and wants to cancel. The user will get a second error when the lostfocus event happens again when he/she goes to click the Cancel button. But since the lostfocus event will put focus back to the box, the user won’t be able to exit. You should probably set a variable if the validation fails, and check the value before committing the data.

devx-admin

Share the Post: