TabIndex Override

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,645
Office Version
  1. 365
Platform
  1. Windows
I have a Userform with several textboxes.

When text is added to a textbox and the user presses the TAB key, I want the focus to go to the next textbox on the form.

Prior to this happening though I need to validate the textbox entry -

E.g.
If a date value is required and the entry is a valid ate format, the TAB press will set the focus on the next textbox.

If the entry is not a valid date format, the textbox is cleared and the focus needs to stay with that textbox.

Currently, I cannot work out how to make the above work using the various options for the control and using the .SetFocus command is ignored when a controls .TabStop = TRUE
VBA Code:
'When this control has its TabStop set to False, when TAB is pressed when in the textbox prior to this one, the focus doesn't set to this control

Private Sub txtPallets_AfterUpdate()

If IsNumeric(txtPallets) Then
   txtPallets = Round(intPallets, 0)
   
   intPallets = txtPallets
   Else
   MsgBox ("Enter a numeric value only"), vbExclamation, "ERROR"
   
   txtPallets = ""
    
   txtPallets.SetFocus
End If

CheckAdd

End Sub

TIA
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try using the BeforeUpdate event instead. When you add Cancel = True, it prevents the user from progressing to the next control in the TabIndex

VBA Code:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If IsNumeric(TextBox1.text) = False Then
        MsgBox "Numbers only"
        Me.BackColor = vbRed
        Cancel = True
    Else
        Me.BackColor = vbWhite
    End If
End Sub
 
Upvote 0
Solution
Thanks Dan_W!

This has bothered me for probably 15 years!!!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top