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

Focus back to Text after Text box Validation

Status
Not open for further replies.

mcgibbong

Programmer
Jan 17, 2002
38
GB
I have a couple of functions that do very simple validation on text boxes.
See below the code for the date check.

When I call this function, and Non Valid Date value is displayed, I want the cursor to go back to the text box that called it, hence the setfocus, But it doesnt work.
By the way I am calling the function on exit from this field.

Also in the case of the date routine, it allows dates entered as mm/dd/yyy to be entered, although they are re-displayed as dd/mm/yyyy.


Any Ideas?


Thanks

Gary


Public Sub ChkDate(ctrl As TextBox)
Dim dteDate As Variant
If Trim(ctrl.Text) <> &quot;&quot; Then
If IsDate(ctrl.Text) = True Then
dteDate = DateValue(ctrl.Text)
ctrl.Text = Format(dteDate, &quot;dd/mm/yyyy&quot;)
Else
MsgBox &quot;Non Valid Date value &quot;
ctrl.Text = &quot;&quot;
ctrl.SetFocus
End If
End If
End Sub

 
Make your ChkDate routine a boolean function, true if a valid date. In your _exit routine, use:

cancel = not ChkDate(...)

Rob
[flowerface]
 
Thanks Rob that works!

But, as always there is a but, when it goes back to the text box, the cursor has disappered and text cannot be entered. I know that it is 'in' the text box as the textbox exit event is trigerred when I tab out of it.

Am I still doing anything wrong?

 
Did you ever get an answer to the issue of the cursor being in the field, but nothing happens. When I tab out of the field it triggers the txtdate_exit event. But I can't get it to highlight the error.

Tim
 
No Tim,

I didnt get an answer to this, but fortunately the project I was working on is 'on hold' so it isnt a priority. If you do get an answer please let me know.


Gary
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top