# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  VB - A demonstration of how to trap the Tab key

## MartinLiss

Normally the pressing of the Tab key can not trapped if there is more than one control on a form that has a TabStop set to True. The attached form demonstrates how to get around that problem.

----------


## Serge

Just to note one thing. Before assigning the TabStop property to False, first check if the control is not Label, Image or Line. These controls don't have TabStop property because they are being painted directly on the form.

----------


## MartinLiss

Thanks Serge. The StoreTabsStops sub in the form has the following statement:


VB Code:
'Store the TabStop property for each control on the form. If you run into
    'a control without a TabStop, just add an On Error Resume line
I probably should have just included the On error Resume Next line instead of the advice.

----------


## techyspecy

Here is another way ..
Put two textboxes on the form ..


VB Code:
Option Explicit
 Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Const VK_TAB = &H9
 Private Sub Text2_LostFocus()
    If GetTabState Then MsgBox "You just tabbed out of Text2"
End Sub
 Private Sub Text1_LostFocus()
    If GetTabState Then MsgBox "You just tabbed out of Text1"
End Sub
 Private Function GetTabState() As Boolean
    GetTabState = False
    If GetKeyState(VK_TAB) And -256 Then
        GetTabState = True
    End If
End Function

Simple, Effective and Efficient code ...

----------


## MartinLiss

This thread got away from what I think is appropriate for the CodeBank so rather than closing it I deleted several posts. I also edited out of techyspecy's post a quote that referred to the deleted posts. If you have concerns about my having taken this action please PM me.

----------


## Jimboat

> Here is another way ..
> 
> Simple, Effective and Efficient code ...


EXCELLENT solution!  Many thanks.

----------

