Date validation routine problems 
Author Message
 Date validation routine problems

As part of a Visual Basic 4.0 Pro project, I created a form with several textboxes and lables.  One of the textboxes
(txtBirthDate) will be used to enter a date.  I have written the following code to check the validity of the date upon
the control loss of focus.

Private Sub txtBirthDate_LostFocus()
    Dim Response As Byte

    'Check for validity of entered date and convert to appropriate format
    If txtBirthDate.Text = "" Or IsDate(txtBirthDate.Text) = True Then
        txtBirthDate.Text = Format(txtBirthDate.Text, "m/d/yy")
    Else
        Response = MsgBox("You have entered an invalid date.", vbOKOnly + vbExclamation, "MicroServices Fitness Tracker")
        txtBirthDate.SetFocus
    End If
End Sub

If the date is valid it will be converted to the specified format.  If it is invalid, a message box will appear.  After
clicking the OK button in the message box, control will return to the date control, thus forcing the entry of a valid
date before proceeding.

Unfortunately I have run into several strange problems with this routine.  If an invalid date is entered, the message box
does appear.  After clicking the OK button, control does return to txtBirthDate, however the arrow keys are not functional.
If a second invalid date is entered, the message box does not appear and the cursor vanishes.

I have had similar problems with a integer checking routine for the same application.  Interestingly the problem goes away
if I delete the message box line (Response = MsgBox("You...).

Am I doing something wrong?  If so how can I correct it?  If not, how can I work around the bug?  Thanks for your help.

Matt Ricciardi



Wed, 18 Aug 1999 03:00:00 GMT  
 Date validation routine problems

I have done a lot of work with validity checking from the LostFocus event,
including dates and have found it to be one of the trickiest things to code
correctly. Without knowing more than you have said (ie: Do you have code in
the GotFocus events of other controls?) I can only offer a few suggestions.

(1) Never put the code that actually checks validity in a LostFocus event,
use a subroutine.

(2) Never try to reset the focus to the control that fired a LostFocus
event in that control's LostFocus event or from any subroutine called by
that LostFocus event unless you are absolutely 110% sure that there will
never be a pending GotFocus or Click event attached to some other control.

(3) When there is any possibility that a GotFocus or Click event is trying
to execute in another control after the LostFocus event detects an error
causing a redirection of the focus back to the erred control, do the
following:

(a) Create a form-scoped flag, will be used to signal state of the
LostFocus validity check.

(b) LostFocus event will set or clear the flag, depending on state of the
validity check.

(c) GotFocus and/or Click event(s) will test flag & branch to an error
recovery subroutine when flag is true.

(b) Error recovery routine will then reset the focus to the correct control
after a DoEvents and display the error message.

(4) Try to use a single TextBox array for all input, it makes coding for
LostFocus validity checks a lot easier.

Regards, Mark E Alsop



Wed, 18 Aug 1999 03:00:00 GMT  
 Date validation routine problems


Quote:
>As part of a Visual Basic 4.0 Pro project, I created a form with several textboxes and lables.  One of the textboxes
>(txtBirthDate) will be used to enter a date.  I have written the following code to check the validity of the date upon
>the control loss of focus.
>Private Sub txtBirthDate_LostFocus()
>    Dim Response As Byte

>    'Check for validity of entered date and convert to appropriate format
>    If txtBirthDate.Text = "" Or IsDate(txtBirthDate.Text) = True Then
>        txtBirthDate.Text = Format(txtBirthDate.Text, "m/d/yy")
>    Else
>        Response = MsgBox("You have entered an invalid date.", vbOKOnly + vbExclamation, "MicroServices Fitness Tracker")
>        txtBirthDate.SetFocus
>    End If
>End Sub
>If the date is valid it will be converted to the specified format.  If it is invalid, a message box will appear.  After
>clicking the OK button in the message box, control will return to the date control, thus forcing the entry of a valid
>date before proceeding.
>Unfortunately I have run into several strange problems with this routine.  If an invalid date is entered, the message box
>does appear.  After clicking the OK button, control does return to txtBirthDate, however the arrow keys are not functional.
>If a second invalid date is entered, the message box does not appear and the cursor vanishes.
>I have had similar problems with a integer checking routine for the same application.  Interestingly the problem goes away
>if I delete the message box line (Response = MsgBox("You...).
>Am I doing something wrong?  If so how can I correct it?  If not, how can I work around the bug?  Thanks for your help.
>Matt Ricciardi


I have always set my return value to Integer and not byte.
Dim Response as integer
I have never had the problem you outlined.

John Reedich



Fri, 20 Aug 1999 03:00:00 GMT  
 Date validation routine problems

Quote:

> As part of a Visual Basic 4.0 Pro project, I created a form with several textboxes and lables.  One of the textboxes
> (txtBirthDate) will be used to enter a date.  I have written the following code to check the validity of the date upon
> the control loss of focus.

> Private Sub txtBirthDate_LostFocus()
>     Dim Response As Byte

>     'Check for validity of entered date and convert to appropriate format
>     If txtBirthDate.Text = "" Or IsDate(txtBirthDate.Text) = True Then
>         txtBirthDate.Text = Format(txtBirthDate.Text, "m/d/yy")
>     Else
>         Response = MsgBox("You have entered an invalid date.", vbOKOnly + vbExclamation, "MicroServices Fitness Tracker")
>         txtBirthDate.SetFocus
>     End If
> End Sub

> If the date is valid it will be converted to the specified format.  If it is invalid, a message box will appear.  After
> clicking the OK button in the message box, control will return to the date control, thus forcing the entry of a valid
> date before proceeding.

> Unfortunately I have run into several strange problems with this routine.  If an invalid date is entered, the message box
> does appear.  After clicking the OK button, control does return to txtBirthDate, however the arrow keys are not functional.
> If a second invalid date is entered, the message box does not appear and the cursor vanishes.

> I have had similar problems with a integer checking routine for the same application.  Interestingly the problem goes away
> if I delete the message box line (Response = MsgBox("You...).

> Am I doing something wrong?  If so how can I correct it?  If not, how can I work around the bug?  Thanks for your help.

> Matt Ricciardi


Hello Matt;

You're doing the validation inside the lost focus event.  Where is the
focus shifted to?  When the message box appears the present focus
control will do a "lost focus".  Then you come back from the message box
and set focus to the control you just checked.  And perhaps that set
focus occurs before the message box clears, giving you a 2nd lost focus.

This stuff will make you nuts.  Instead of a message box, you could
update a label on the form in queation.  Or, do your validity check in
the got focus of the next control.

You cannot single step with the debuger and see these problems.  Put
some debug.print "routine name"  ' statements at the head of the various
got/lost focus subs.  You will be amazed how many events fire, and the
path that takes you through the "msgbox" will change the flow.  Once you
understand these event flows you will be able to figure out how to avoid
the problems.

Also, as you cut/paste solution attempts, be sure to save your code
before every execute.  It is very easy to get into a stack overflow,
gpf, crash as you inatvertantly create an endless event loop.

--
Regards
/Don Ames
GUU Software Inc.

Please visit our web site at...
http://www.access.digex.net/~guu_sftw



Mon, 23 Aug 1999 03:00:00 GMT  
 Date validation routine problems

I have not found MessageBox to be very well behaved in regards to working
well with other controls' events (this in VB3).  Specifically, I recall
that after a MessageBox call, correct events were not firing on my TrueGrid
control; I actually wrote my own MessageBox equivalent to get around this
problem (it was very intractable).

Have you thought about waiting until the user completes the form, then just
beeping and setting focus to the offending text box (maybe with a red
status bar text on the bottom of the form)?  I know this seems like
cheating, but, in actuality, I think most users would rather not have their
typing flow interrupted until they complete an input form.  What's
important for quick data entry is uninterrupted flow and a predictable
sequence of events (this is why it's usually not a good idea to have fields
that automatically advance to the next field when they are full, not
requiring a tab).  



Quote:
> As part of a Visual Basic 4.0 Pro project, I created a form with several

textboxes and lables.  One of the textboxes
Quote:
> (txtBirthDate) will be used to enter a date.  I have written the

following code to check the validity of the date upon
Quote:
> the control loss of focus.

> Private Sub txtBirthDate_LostFocus()
>     Dim Response As Byte

>     'Check for validity of entered date and convert to appropriate format
>     If txtBirthDate.Text = "" Or IsDate(txtBirthDate.Text) = True Then
>         txtBirthDate.Text = Format(txtBirthDate.Text, "m/d/yy")
>     Else
>         Response = MsgBox("You have entered an invalid date.", vbOKOnly +

vbExclamation, "MicroServices Fitness Tracker")
Quote:
>         txtBirthDate.SetFocus
>     End If
> End Sub

> If the date is valid it will be converted to the specified format.  If it

is invalid, a message box will appear.  After
Quote:
> clicking the OK button in the message box, control will return to the

date control, thus forcing the entry of a valid
Quote:
> date before proceeding.

> Unfortunately I have run into several strange problems with this routine.

 If an invalid date is entered, the message box
Quote:
> does appear.  After clicking the OK button, control does return to

txtBirthDate, however the arrow keys are not functional.
Quote:
> If a second invalid date is entered, the message box does not appear and

the cursor vanishes.
Quote:

> I have had similar problems with a integer checking routine for the same

application.  Interestingly the problem goes away
Quote:
> if I delete the message box line (Response = MsgBox("You...).

> Am I doing something wrong?  If so how can I correct it?  If not, how can

I work around the bug?  Thanks for your help.
Quote:

> Matt Ricciardi




Tue, 24 Aug 1999 03:00:00 GMT  
 Date validation routine problems

Doing validation in lost focus will always present a problem.  The
msgbox WILL mess with the event stack.

You CAN find a way to work around both of these, I would suggest however
that a messagebox firing as a cursor leaves a field is not very pleasent
user interface anyway.  Do a form validation after the user has
indicated that everything is done.  Then you can show your messagebox
with all problems in it and set the focus accordingly.  This will also
allow you to centralize your validation, some fields may otherwise avoid
validation if the user moused over them.

Good Luck!

David



Thu, 26 Aug 1999 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Date validation routine problems

2. Date validation routine problems

3. Need Date validation routine

4. Problems with getting server validation routine from client script

5. Date Validation Problem

6. Date validation problem...please help

7. New problem with Date validation

8. problem: vbscript date validation

9. searching a routine to convert date number in normal format date

10. DBGrid data validation routine

11. Time Validation Routines Needed

12. Validation routines in VB/VBScript

 

 
Powered by phpBB® Forum Software