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

x
?
Solved

settin focus using tabindex

Posted on 2004-08-05
3
Medium Priority
?
1,588 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 54

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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 …
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

865 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