New to Visual Basic

Hi Experts,

I am brand new to Visual Basic.

I have this function here whereby it will execute anytime user hit on "F2".

I want the focus to set on the next "Tabindex". I do not know the properties well, can someone assist?

I know how to achieve by indicating for every single control but I sounds stupid. It should have a way to identify where is the tabindex on focus now and is just the matter of adding 1 to navigate to the next one.

Thanks advance for your assistance.
LVL 10
adriankohwsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Something like...

Option Explicit

Private Sub Form_Load()
    Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyF2 Then
        SendKeys "{Tab}"
    End If
End Sub

adriankohwsAuthor Commented:
Thank you. Actually, if I want to use this, of course it will work. But my real concern is not to just get it work.

My main purpose is to know how to access the properties of the controls within a form without having to name the controls like "To find out which control is currently focused or activve".
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
To get the control that is currently focused:

    Dim c As Control
    Set c = Me.ActiveControl
    If Not (c Is Nothing) Then
        ' do something with "c" in here...
    End If

To iterate thru all the controls on a Form:

    Dim c As Control
    For Each c In Me.Controls
        ' do something with "c" in here...  
    Next c

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adriankohwsAuthor Commented:
Well, something very close...

Just wondering if I can get what is the tabindex of the activecontrol and add one to it to navigate to the next control?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
I don't know why you would want to do it this way...

Option Explicit

Private Sub Form_Load()
    Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim ctl As Control
    Dim curCtl As Control
    Dim curTabIndex As Integer
    Dim firstCtl As Control
   
    If KeyCode = vbKeyF2 Then
        Set curCtl = Me.ActiveControl
        If Not (curCtl Is Nothing) Then
            curTabIndex = curCtl.TabIndex
            For Each ctl In Me.Controls
                If Not (ctl Is curCtl) Then
                    If ctl.TabIndex = (curTabIndex + 1) Then
                        ctl.SetFocus
                        Exit Sub
                    ElseIf ctl.TabIndex = 0 Then
                        Set firstCtl = ctl
                    End If
                End If
            Next
            If Not (firstCtl Is Nothing) Then
                firstCtl.SetFocus
            End If
        End If
    End If
End Sub
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.