excel 2013 vba: Custom "tab" button not making continuous cycle through textboxes

jedwardo

Board Regular
Joined
Aug 21, 2012
Messages
122
Hello Everyone,

I've created this button on a userform on-screen keyboard to cycle through all the textboxes on another userform. After the 21st textbox (21st by TabIndex) it returns to the first textbox and starts over. What's happening though is that after making it through 22 boxes it stops cycling and does nothing at all. If I then call up the on-screen keyboard again (without resetting the userform it's being used with) it will cycle through 21 textboxes, then 20, then 19 and so on until I reset UserFormContacts and then it starts over at 22 again.

I'm pretty sure it's got something to do with the Case 22 since it cycles through 22 textboxes the first go around but I don't know what to change.

Here's the code:

Code:
Public iTab As Long

Code:
Private Sub cbnTab_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Dim ctl As Control
        For Each ctl In UserFormContacts.Controls
            If ctl.TabIndex = iTab + 1 Then
                Select Case ctl.TabIndex
                    Case 22
                        ' Wrap focus to the first textbox
                        UserFormContacts.TextBoxFirstName.SetFocus
                        Exit Sub
                    Case Else
                        ctl.SetFocus
                        Exit Sub
                End Select
            End If
        Next
End Sub


Everytime the tab button is clicked it sets focus on the next TextBox Enter event which sets iTab to the ActiveControl.TabIndex. All the Textboxes look like this one below:

Code:
Private Sub TextBoxAddress_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    iTab = ActiveControl.TabIndex
        If Me.TextBoxAddress.Name = Me.ActiveControl.Name Then
            FullKeyboard
        Else
            Exit Sub
        End If
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
In the Enter event, have you tried using the control itself rather than ActiveControl to set iTab

Code:
Private Sub TextBoxAddress_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    iTab = TextBoxAddress.TabIndex: Rem <<<<<<<<<<<<<<<<<<<<<<<
        If Me.TextBoxAddress.Name = Me.ActiveControl.Name Then
            FullKeyboard
        Else
            Exit Sub
        End If
End Sub
 
Upvote 0
If it stops working at a particular control, then at some point, no control meets the condition .TabIndex = iTab + 1

Try changing the condition to

Code:
        For Each ctl In UserFormContacts.Controls
    
            If ctl.TabIndex =(iTab Mod UserFormContacts.Controls.Count) + 1 Then
                If ctl.TabStop Then
                    Select Case ctl.TabIndex
                        Case 22
                            ' Wrap focus to the first textbox
                            UserFormContacts.TextBoxFirstName.SetFocus
                            Exit Sub
                        Case Else
                            ctl.SetFocus
                            Exit Sub
                    End Select
                Else
                    iTab = (iTab Mod UserFormContacts.Controls.Count) + 1
                End If
            End If
        Next
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,731
Messages
6,132,390
Members
449,725
Latest member
Enero1

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