Solved

settin focus using tabindex

Posted on 2004-08-05
3
1,575 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 49

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now