Solved

settin focus using tabindex

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

895 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

15 Experts available now in Live!

Get 1:1 Help Now