Solved

settin focus using tabindex

Posted on 2004-08-05
3
1,580 Views
Last Modified: 2013-12-25
i'm tryin to set focus in the next text box
by pressing vbKeyDown in the text box

there are three n boxes txt_1 , txt_2 and txt_3 ... txt_n

when i press down key in txt_1 focus should be set to txt_2
and from txt_2 to txt_3
and upto txt_n

i will call a function in each textboxes keydown event
which will shift the focus to the next text box

function :
private function shiftFocus(byval index as integer)
    dim i as integer
    i=index+1
    me.controls(i).setfocus ' this line is setting wrong focus
end function

call :

call (txt_1.tabIndex)

any solution to my problem is worth 500 points.
Thanks

0
Comment
Question by:amit_mi2
3 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 11724066
Try use sendkeys function, like:

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

in the case that the TabIndex(s) are in order

cheers
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 11730801
ryancys' comment is the pragmatic answer.

However,  to understand why your attempt did not work, you should understand that TabIndex is not the same as the index in the controls collection. Therefore you cannot address a control by its TabIndex. You would have to find it programatically:

Option Explicit

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
KeyCode = 9
    NextBox Text1
End Sub

Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
    NextBox Text2
End Sub

Private Sub Text3_KeyDown(KeyCode As Integer, Shift As Integer)
    NextBox Text3
End Sub

Sub NextBox(TextX As Control)
    Dim ctl As Object
    Dim i As Integer
    Dim m As Integer
    Dim Minctl As Control
   
    m = TextX.TabIndex
    Set Minctl = Me.Text1
    For Each ctl In Me.Controls
        If TypeOf ctl Is TextBox Then
            If ctl.TabIndex = TextX.TabIndex + 1 Then
                ctl.SetFocus
                Exit Sub
            End If
            If ctl.TabIndex < m Then
                m = ctl.TabIndex
                Set Minctl = ctl
            End If
        End If
    Next ctl
    Minctl.SetFocus 'last box reached, back to start
End Sub
0
 

Author Comment

by:amit_mi2
ID: 11741926
nice answer Graham
same thing i worked out
after posting
0

Featured Post

Independent Software Vendors: 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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

733 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