Tab Stop Broken

G

Garry Jones

I have a problem with Tab Stop.

I have 5 textboxes which must be used in order.

UserForm1 starts with Textbox1, Textbox2 Enabled=True and Textbox3 thru
Textbox5 Enabled=False

After a tab from Textbox1 to Textbox2 the code sets Enabled=False for
Textbox1 and Enabled=True for Textbox3 and the cursor should now be in
Textbox2.

This does not work, the cursor skips the entire list of textboxes one by
one as if Tabstop has been turned off.

However, if I choose not to set Enabled=False for the current textbox it
works.

For other reasons not explained here I need to use Textbox_Exit for each
Textbox. I stopped an earlier problem by disabling events when running
the Enabled=False.

Bottom Line, what is wrong with the code? Why isn't it tab stopping?
_______________________________

Dim bDisableEvents As Boolean 'needed to trap enabled = true
_______________________________

Private Sub UserForm_Initialize()
'user must use textboxes in order
'3 textboxes swicthed off with this
TextBox3.Enabled = False 'switch off textbox3
TextBox4.Enabled = False 'switch off textbox3
TextBox5.Enabled = False 'switch off textbox3
End Sub
_______________________________

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If bDisableEvents Then Exit Sub
ntl (1)
End Sub
_______________________________

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If bDisableEvents Then Exit Sub
ntl (2)
End Sub
_______________________________

Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If bDisableEvents Then Exit Sub
ntl (3)
End Sub
_______________________________

Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If bDisableEvents Then Exit Sub
ntl (4)
End Sub
_______________________________

Private Sub TextBox5_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If bDisableEvents Then Exit Sub
ntl (5)
End Sub
_______________________________

Private Sub ntl(bnum As Integer)

'This disables the current textbox

With UserForm1("textbox" & bnum)
bDisableEvents = True
.Enabled = False 'offending code
bDisableEvents = False
End With

'This enables the text box two after current

Dim bbbnum As Integer
bbbnum = bnum + 2

If bbbnum < 6 Then
With UserForm1("textbox" & bbbnum)
.Enabled = True
End With
End If

End Sub
_______________________________

Note:
If I comment out the offending code it works except for the fact that
the Textboxes that should be Enabled=False are still Enabled = True.

If anyone can help me I will be grateful. If you want to see it in
action create a userform with 5 textboxes. Change the offending code to
"comment" and it will tab, remove comment and it will skip textboxes.

Why?

Garry Jones
Sweden
 
D

Dave Peterson

Why should textbox2 be enabled when you start? (I'm guessing that the next
textbox should be enabled when you exit the previous textbox.)

I just look for the tab character when I'm in a textbox and react to it:


Option Explicit
Dim bDisableEvents As Boolean
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Beep
'msgbox "doesn't work"
Cancel = True
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 9: KeyAscii = 0: ntl 1
End Select
End Sub
Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 9: KeyAscii = 0: ntl 2
End Select
End Sub
Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 9: KeyAscii = 0: ntl 3
End Select
End Sub
Private Sub TextBox4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 9: KeyAscii = 0: ntl 4
End Select
End Sub
Private Sub TextBox5_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 9: KeyAscii = 0: ntl 5
End Select
End Sub
Private Sub UserForm_Initialize()

Dim i As Long

Me.CommandButton1.TabStop = True

With Me.TextBox1
.SetFocus
.Enabled = True
.TabStop = True
.TabKeyBehavior = True
End With

For i = 2 To 5
With Me.Controls("textbox" & i)
.Enabled = False
.TabStop = False
.BackColor = &H80000018
.TabKeyBehavior = True
End With
Next i
Me.TextBox1.SetFocus

End Sub
Private Sub ntl(bnum As Integer)

bDisableEvents = True
With Me.Controls("textbox" & bnum)
If Trim(.Value) = "" Then
.SetFocus
Beep
Else
.Enabled = False
.BackColor = &H80000018
If bnum < 5 Then
With Me.Controls("textbox" & bnum + 1)
.Enabled = True
.BackColor = &H80000005
.SetFocus
End With
Else
Me.CommandButton1.SetFocus
End If
End If
End With
bDisableEvents = False

End Sub






I added some colors--it made testing easier for me. And I checked to see if
they left the textbox empty.
 

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