Text Box exit

J

Jamal

I am trying to write code so that when the value entered
in a textbox does not meet certain criteria, the text in
the textbox is selected for correction by the user. Please
see the code below.

Once the user presses the tab key the message is displayed
but the focus is lost and goes on to the next textbox or
next frame if the textbox is the last one in the current
frame. I have tried before update and change as well as
exit. What am I doing wrong? How can I get it to go back
and highlight the textbox with incorrect text/value.
Thanks for your help.


Private Sub TextBox1_Exit(ByVal Cancel As
MSForms.ReturnBoolean)

If TextBox1.Value <> 100 Then

msgbox "You must enter ....."

TextBox1.SetFocus
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)

End Sub
 
R

Ron de Bruin

You must set Cancel to True Jamal

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1.Value <> 100 Then
Cancel = True
MsgBox "You must enter ....."
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
End If
End Sub
 
B

Bob Phillips

Ron,

The frame is the problem.

The tab out fires the Frame exit event not the textbox exit event.


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
R

Ron de Bruin

Hi Bob

Now I remember it that I have this problem ones.
But I can't remember the workeround.

I try to find it
 
B

Bob Phillips

Hi Ron,

Me neither, other than to get rid of frames<VBG>

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
B

Bob Phillips

Tom,

I tried that. Problem comes when that frame has more than one textbox, you
don't get the error until the whole frame is tabbed out of, and the focus
stays on the last textbox.

It's a real nasty/

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
T

Tom Ogilvy

It's a real nasty/

MSForms Frames have kind of been like that in general. <g>
 
B

Bob Phillips

Harald,

Only after you exit the frame, and as I said, it doesn't setfocus to the
first textbox.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 

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