Change pages of Multipage with Keyboard with focus on textbox control

sujittalukde

Well-known Member
Joined
Jun 2, 2007
Messages
520
I have a Multipage control on a userform with 5 pages.
In Run time, if I press Ctrl+Tab, focus moves from Active page say page 1 to page 2 and so on.
Now the problem arises when I have a Textbox control on page1.
If on page1, there is a text box and the focus is on textbox, then pressing Ctrl+Tab moves the cursor with in the Textbox and does not change the pages og Multipage control.
Why? How to overcome this?

Regards
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
This should work for every page in every MultiPage in the UserForm regardless of how many multipages, pages or textboxes you have.


1- Add a new Class module to your Project and give it the name of CTextBox

Place this code in the Class module :
Code:
Option Explicit

Public WithEvents TxtBx As MSForms.TextBox

Private sMltPg As String

Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer


Public Property Let MltPg(ByVal sMltPgName As String)
    sMltPg = sMltPgName
End Property

Private Sub TxtBx_KeyDown _
(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    Dim oMltPg As MSForms.MultiPage
    
    Set oMltPg = CallByName(TxtBx.Parent.Parent.Parent, sMltPg, VbGet)
    If KeyCode = 9 And GetAsyncKeyState(VBA.vbKeyControl) <> 0 Then
        On Error Resume Next
        oMltPg.Value = oMltPg.Value + 1
        If Err.Number <> 0 Then oMltPg.Value = 0
    End If

End Sub
2- Place this code in the UserForm module :

Code:
Option Explicit

Private oCol As Collection

Private Sub UserForm_Initialize()

    Dim oTextBx As CTextBox
    Dim oTxtBx As MSForms.Control
    Dim oPg As MSForms.Control
    Dim oMltPg As MSForms.Control
    
    Set oCol = New Collection

    For Each oMltPg In Me.Controls
        If TypeOf oMltPg Is MSForms.MultiPage Then
            For Each oPg In oMltPg.Pages
                For Each oTxtBx In oPg.Controls
                    If TypeOf oTxtBx Is MSForms.TextBox Then
                        Set oTextBx = New CTextBox
                        oTextBx.MltPg = oTxtBx.Parent.Parent.Name
                        Set oTextBx.TxtBx = oTxtBx
                        oCol.Add oTextBx
                    End If
                Next
            Next
        End If
    Next
    
End Sub

Private Sub UserForm_Terminate()
    Set oCol = Nothing
End Sub
 
Upvote 0
Thank you for the reply. But this is a very complex procedure.

Is there any simple method of doing the same? or this is a limitation?

Regards
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

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