Tab key

Hi,

I am a beginner in VB and am working on a VB key trapping using the KeyPress procedure. I'd like to make a condition statement for pressing the Tabelator key (I tried both vbKeyTab and its ASCII code 9) in the text box control during runtime. But this condition is ignored and cursor moves to the next control in the order of TabIndex. Is there a way how to make the program respond to pressing the Tab key (without setting the TabStop property to False)?

Thank you in advance,
Tomas Jirsak
jirsak@bnl.gov

P.S1. I tried without success also KeyDown and KeyUp procedures.
P.S2. A very simple example is below.


--------------------------------

Private Sub txtPosition4_KeyPress(KeyAscii As Integer)
Select Case True
    Case KeyAscii = vbKeyTab
        MsgBox "That was a Tab key"
    Case Else
        MsgBox "That WAS NOT a Tab key"
End Select
End Sub

---------------------------------
Tomas_JirsakAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
credoConnect With a Mentor Commented:
Tomas,

Your code should be as follows:
Private Sub txtPosition4_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
    Case vbKeyTab
        MsgBox "That was a Tab key"
    Case Else
        MsgBox "That WAS NOT a Tab key"
End Select
End Sub

An easier way to do this would be to use the following code:

Private Sub txtPosition4_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyTab Then
        MsgBox "That was a tab key"
    Else
        MsgBox "That was Not a tab key"
    End If
End Sub

*Remember that if you want to stop the key press from working then set the KeyAscii value to 0.

Here is an example:
Private Sub txtPosition4_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyTab Then
       KeyAscii = 0 'Cancel Tab key press
    End If
End Sub

Kind regards,
Credo
0
 
JuiletteCommented:
You can set the Tabstop property to false for all the controls:

Private Sub Form_Load()
Dim i As Integer
On Error Resume Next
For i = 0 To Controls.Count - 1   ' Use the Controls collection
    Controls(i).TabStop = False
Next
End Sub
0
 
JuiletteCommented:
Oooops...sorry...didn't clue in on the not set to false.
Wayne
0
 
Tomas_JirsakAuthor Commented:
Juliette's answer is exactly what I don't want,  i.e. setting properties of all the controls to False (this is explicitly mentioned in my question).
0
 
beachbum714Commented:
Neither the KeyPress nor the KeyCode events respond to the tab key as stated in the original question.  I have noticed this in VB6 AND VB3.  Does anyone have a real solution?
0
All Courses

From novice to tech pro — start learning today.