Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trap Tab key in keypress event

Status
Not open for further replies.

Webrookie

Programmer
May 18, 2000
112
US
I'm trying to validate what is typed in a text box. I'm not interested in using the Validate Event, that won't help me.

I'm trapping for keys:

'''''''''''''''''
Private Sub AtsNum_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Or KeyAscii = 9 Then
'''''''''''''''''

As you can see i'm trapping for enter and tab. After this code I do some validation determining focus of next control.

For some reason, tab does not trigger the event. Even though it says in MSDN library that it will.

Am I missing something?


Thanks in advance.
 
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyTab Or KeyCode = vbKeyReturn Then
'Do Something
End If

End Sub

This works fine. Also you should set TabStop to False for each object on the form.
 
How do you stop them from leaving via the Mouse? Anyway, here is code I used for an RTF (which by the way has changed behavior to receive Tabs without this).
Code:
Private Sub Sysprint_GotFocus()
    Dim ctlW As Control
    For Each ctlW In Me.Controls
    ' Some Ctls like Timer have no tabstop
    On Error Resume Next 
        ctlW.TabStop = False
    On Error GoTo 0
    Next
End Sub
Compare Code (Text)
Generate Sort in VB or VBScript
 
Bobloblaws,
That doesn't work. I've tried it. It actually specifies in MSDN that KeyDown event will not trap the Tab Key.

 
I tried it before I sent it to you, and it worked on my computer. What happens when you press Tab? Where does the focus go?
 
oh ye of little faith...lol..I say that a lot.

anyway, apparently I missed a command button i forgot about as far as tab stops go. so that does work. But I'm gonna have several fields on this form, so I have to set all tabstops to false, and then explicity set the focus of the next control for every field? Not too mention I'm gonna be dynamically painting textfields on the form pending data from a SQL Server database.

Ya know, I've done this before in Foxpro...and it was much easier.

Good think I haven't wandered too far into VB with this.

Thanks for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top