PDA

View Full Version : Solved: Userform, Selecting / Higlighting the content of a Text box



BoatwrenchV8
10-25-2012, 11:45 AM
On a Userform, how do you select the contents of a text box so it appears in highlight? I have a text box on a userform that is supposed to have a date pasted or typed into it. To verify the data entered is a date, I have this code (which was partially borrowed), which fires on the text box _exit event,

Private Sub tb_StartDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With tb_StartDate
If IsDate(.Text) Then
'/ format it
.Text = Format$(.Text, "mm/dd/yyyy")
Else
'/ error
MsgBox "The value you entered is not a date."
.SelStart = 0
.SelLength = Len(.Text)
Cancel = True
End If
End With
'End Sub


Everything I tried has not worked so far. Also, what is Cancel = True doing? I have not found any difference between setting this to false. Insight, explanation, etc needed :)

gmaxey
10-25-2012, 12:39 PM
Your code seems to work here. Are you sure it is named properly and firing on your end?

Cancel keeps the focus in the textbox.

Private Sub tb_StartDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With tb_StartDate
If IsDate(.Text) Then
.Text = Format$(.Text, "mm/dd/yyyy")
Else
Cancel = True
MsgBox "The value you entered is not a date."
.SelStart = 0
.SelLength = Len(.Text)
End If
End With
End Sub

BoatwrenchV8
10-25-2012, 01:55 PM
It is working properly to determine if the data entered is a date or not, that part is fine. The part that isn't working is if the data entered is not a date, the data is not highlighted so it stands out. I want it to look highlighted like it has been selected to be copied, signalling to the user the data is bad.

gmaxey
10-25-2012, 02:01 PM
It does here. Post your document and I'll look at it.

BoatwrenchV8
10-25-2012, 02:13 PM
I will have to create an example as I cannot send the original.

BoatwrenchV8
10-25-2012, 02:21 PM
I just created a sample and it DOES do what I want it to. Now I have to figure out why it is not working in the actual userform. Got to be a userform setting somewhere. Hmmm....

Frosty
10-25-2012, 02:22 PM
remember that code execution pauses when you display the message box. So if you want the text of the text box to be selected when you display the messagebox, you need to display the messagebox *after* you manipulate the selection via .SelLength and .SelStart...

Frosty
10-25-2012, 02:24 PM
And in terms of properties to play around with-- look at the EnterFieldBehavior property of the control. That should only be triggered if you're using .SetFocus via code, but I'm guessing either you need to reorder your code, or you will need to examine some other events also associated with the control

BoatwrenchV8
10-25-2012, 02:42 PM
The userform and text box properties are identical except for size and for the userform ShowModal = False. The more I think about it, I think Frosty is correct. I am going to move this to a different location in the code and move some things around.

BoatwrenchV8
10-25-2012, 03:07 PM
Ok, I just commented out each procedure and ran my userform. It still does not work correctly like the sample I threw together. I have 7 pages of code commented out with only Private Sub tb_StartDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With tb_StartDate
If IsDate(.Text) Then
'/ format it
.Text = Format$(.Text, "mm/dd/yyyy")
Else
'/ error

MsgBox "The value you entered is not a date."
tb_StartDate.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
Cancel = True
' tb_StartDate.SelText = .Text
End If
End With
End Sub
left in and it still does not highlight the bad data that was entered. Got to be a textbox setting I am overlooking.

Frosty
10-25-2012, 04:40 PM
It's not selecting the text, even after the msgbox is dismissed?

BoatwrenchV8
10-25-2012, 04:56 PM
Correct, nothing is selected after the message box is dismissed. Still scratching my head on this.

BoatwrenchV8
10-25-2012, 05:59 PM
Well, I found it as I was working thru my code entering comments. Noticed I had a line in there I didn't have in the other, test form. Chalking this up to experience and knowing when to go take a break. lol. Thank you for all your help on this Frosty and Gmaxey.

gmaxey
10-25-2012, 06:46 PM
BWV8,

I'm glad you solved it. I've spent the last 5 minutes trying to reply to your PM and could never get it to work :-(. Shouldv'e looked here first.

If it is not a state secret, what was the line of code?

BoatwrenchV8
10-26-2012, 07:08 AM
I had left in the line: Tb_FirstDate.SelText = .Text
Was looking right at it and didn't see it.:doh: After commenting it out, worked like it was supposed to.
Private Sub Tb_FirstDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Tb_FirstDate
If IsDate(.Text) Then
'/ format it
.Text = Format$(.Text, "mm/dd/yyyy")
Else
'/ error
MsgBox "The value you entered is not a date."
.SelStart = 0
.SelLength = Len(.Text)
Cancel = True
Tb_FirstDate.SelText = .Text
End If
End With
End Sub

Frosty
10-26-2012, 07:26 AM
I love those solutions!

It is hard to respond to PMs, isn't it? I always hit forward and then type in the recipient's name again.

Glad it worked out-- would have responded to the PM, Boatwrench, but saw you sorted it out here first.

BoatwrenchV8
10-26-2012, 11:52 AM
Thanks again for your help.

BoatwrenchV8
10-28-2012, 09:40 AM
Well, I got back into this. Yes, the text is selected if it is not a recognized date, but only if the message box notification is commented out. I tried it with the message box in various places and it will not allow the text box to be selected. Comment out the message box and it works perfectly. Scratching my head again.

Frosty
10-29-2012, 12:12 PM
Hmm, I thought I'd discussed this potential in this thread, but it must have been another thread.

You should check the .ShowModal property of the userform. Msgbox (which is *always* modal) and modeless UserForms don't play well together.

I'm posting that here, because that's the ultimate *answer* to the original question.

BoatwrenchV8
10-29-2012, 04:09 PM
Hmmm... I will have to get rid of the message box then and warn the user in a different way. And yes!, this is another Doh:doh: moment. Add that to the experience sheet. Thank you for your help.