Setfocus won't work on a userform's textbox control

G

Guest

Hi everyone
I have check other similar messages and tried different things but cannot get the focus on the textbox control when an invalid entry is made
The code follows
' ******************************************************************
Private Sub txtChangeDate_Exit(ByVal Cancel As MSForms.ReturnBoolean
' Jac Tremblay - 2004-05-2
With ufoFeedXl.txtChangeDat

' Check if date is valid
If IsDate(.Text) The

' Check if date is present
If Len(.Text) > 0 The

' Check that length is 10 and format is OK
If Len(.Text) <> 10 Or
Mid(.Text, 5, 1) <> "-" Or
Mid(.Text, 8, 1) <> "-" The
MsgBox "Date format must be ""yyyy-mm-dd"".
.Activat
' .SetFocu
.SelStart =
.SelLength = Len(.Text
End I
End I
Els
MsgBox "Date is invalid.
.Activat
' .SetFocu
.SelStart =
.SelLength = Len(.Text
End I
End Wit
End Su
' ******************************************************************
Some suggest to use .Activate, but this method does not exist for that control. The Setfocus never works. What can I do
Any help will be appreciated
Thanks
 
T

Tom Ogilvy

You don't need to setfocus in the exit event. You need to set Cancel to
True and the exit will be halted.

Private Sub txtChangeDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Jac Tremblay - 2004-05-24
With ufoFeedXl.txtChangeDate

' Check if date is valid.
If IsDate(.Text) Then

' Check if date is present.
If Len(.Text) > 0 Then

' Check that length is 10 and format is OK.
If Len(.Text) <> 10 Or _
Mid(.Text, 5, 1) <> "-" Or _
Mid(.Text, 8, 1) <> "-" Then
MsgBox "Date format must be ""yyyy-mm-dd""."
Cancel = True
End If
End If
Else
MsgBox "Date is invalid."
Cancel = True
End If
End With
End Sub

--
Regards,
Tom Ogilvy

Jac Tremblay said:
Hi everyone,
I have check other similar messages and tried different things but cannot
get the focus on the textbox control when an invalid entry is made.
The code follows:
' *******************************************************************
Private Sub txtChangeDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Jac Tremblay - 2004-05-24
With ufoFeedXl.txtChangeDate

' Check if date is valid.
If IsDate(.Text) Then

' Check if date is present.
If Len(.Text) > 0 Then

' Check that length is 10 and format is OK.
If Len(.Text) <> 10 Or _
Mid(.Text, 5, 1) <> "-" Or _
Mid(.Text, 8, 1) <> "-" Then
MsgBox "Date format must be ""yyyy-mm-dd""."
.Activate
' .SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End If
End If
Else
MsgBox "Date is invalid."
.Activate
' .SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End If
End With
End Sub
' *******************************************************************
Some suggest to use .Activate, but this method does not exist for that
control. The Setfocus never works. What can I do?
 
G

Guest

Thank you Tom. It effectively works fine that way. I changed the logic a bit too
Here is the new code
' ******************************************************************
Private Sub txtChangeDate_Exit(ByVal Cancel As MSForms.ReturnBoolean
' Jac Tremblay - 2004-05-2
With ufoFeedXl.txtChangeDat

' Accept an empty date field
If Len(.Text) = 0 Then Exit Su

' Check if date is valid
If IsDate(.Text) The

' Check that length is 10 and format is OK
If Len(.Text) <> 10 Or
Mid(.Text, 5, 1) <> "-" Or
Mid(.Text, 8, 1) <> "-" The
MsgBox "Date format must be ""yyyy-mm-dd"".
.SelStart =
.SelLength = Len(.Text
Cancel = Tru
End I
Els
MsgBox "Date is invalid.
.SelStart =
.SelLength = Len(.Text
Cancel = Tru
End I
End Wit
End Su
' ******************************************************************
I appreciate your help.
 
D

Dana DeLouis

If Len(.Text) said:
Mid(.Text, 5, 1) <> "-" Or _
Mid(.Text, 8, 1) <> "-" Then
MsgBox "Date format must be ""yyyy-mm-dd""."

Would this idea work for you?

Dim s
s = "2004-5-3"
If Not s Like "####-##-##" Then MsgBox "Error" '<- Error

s = "2004-05-03"
If Not s Like "####-##-##" Then MsgBox "Error" '<- Ok

--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


Jac Tremblay said:
Hi everyone,
I have check other similar messages and tried different things but cannot
get the focus on the textbox control when an invalid entry is made.
The code follows:
' *******************************************************************
Private Sub txtChangeDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Jac Tremblay - 2004-05-24
With ufoFeedXl.txtChangeDate

' Check if date is valid.
If IsDate(.Text) Then

' Check if date is present.
If Len(.Text) > 0 Then

' Check that length is 10 and format is OK.
If Len(.Text) <> 10 Or _
Mid(.Text, 5, 1) <> "-" Or _
Mid(.Text, 8, 1) <> "-" Then
MsgBox "Date format must be ""yyyy-mm-dd""."
.Activate
' .SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End If
End If
Else
MsgBox "Date is invalid."
.Activate
' .SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End If
End With
End Sub
' *******************************************************************
Some suggest to use .Activate, but this method does not exist for that
control. The Setfocus never works. What can I do?
 
G

Guest

Hi Dana
That is a good idea, I must admit. I will use it because it is better than mine
Here is the new code version
' ******************************************************************
Private Sub txtChangeDate_Exit(ByVal Cancel As MSForms.ReturnBoolean
' Jac Tremblay - 2004-05-2
' With the precious help of Tom Ogilvy and Dana DeLoui
With ufoFeedXl.txtChangeDat

' Accept an empty date field
If Len(.Text) = 0 Then Exit Su

' Check if date is valid
If IsDate(.Text) The

' Check that format is OK
If Not .Text Like "####-##-##" The
MsgBox "Date format must be ""yyyy-mm-dd"".
.SelStart =
.SelLength = Len(.Text
Cancel = Tru
End I
Els
MsgBox "Date is invalid.
.SelStart =
.SelLength = Len(.Text
Cancel = Tru
End I
End Wit
End Su
' ******************************************************************
Thanks a lot. Your help is very appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top