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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
credoCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.