PDA

View Full Version : return to textbox



av8tordude
07-03-2009, 07:33 PM
I have a txtbox in a userform. Whenever I enter a incorrect value and exit the textbox, it flags it with a msgbox and moves to the next txtbox. How do I make it stay in the same txtbox to correct the value before moving to the next textbox. I have tried textbox1.Activate, but doesn't work. Any suggestions? thanks in advance.

rbrhodes
07-03-2009, 08:07 PM
Hi ava8,

On a userform?

textbox1.getfocus perhaps?

av8tordude
07-03-2009, 08:25 PM
Hi ava8,

On a userform?

textbox1.getfocus perhaps?

Sorry, that didn't work.:dunno, but I realized that I had it in an exit script so I used Cancel=True

mikerickson
07-03-2009, 10:30 PM
I have had poor results from using Exit or Enter events to control the focus. I put data validation routines in either the BeforeUpdate or AfterUpdate events and use the Cancel argument when needed.

mdmackillop
07-04-2009, 02:57 AM
I think BeforeUpdate is the appropriate event
Something like

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1.Value > 100 Then
Cancel = True
MsgBox "Error"
End If
End Sub

Bob Phillips
07-04-2009, 03:27 AM
I have had poor results from using Exit or Enter events to control the focus. I put data validation routines in either the BeforeUpdate or AfterUpdate events and use the Cancel argument when needed.

Me too. I had a situation once where it just moved on to some totally unrelated control, very odd.

lucas
07-04-2009, 09:28 AM
You can select the incorrect data too. Building on Malcolm's contribution:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1.Value > 100 Then
Cancel = True
Me.TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
MsgBox "Error"
End If
End Sub