[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

New to Visual Basic

Posted on 2006-04-01
5
Medium Priority
?
240 Views
Last Modified: 2013-11-25
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.
0
Comment
Question by:adriankohws
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16351583
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

0
 
LVL 10

Author Comment

by:adriankohws
ID: 16352943
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".
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 150 total points
ID: 16353276
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
0
 
LVL 10

Author Comment

by:adriankohws
ID: 16358537
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?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16360524
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question