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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trap the Tab Key

Status
Not open for further replies.

SteveMac32

Programmer
Jan 25, 2002
74
0
0
GB
Hi all
Does anyone out there have any idea on how to get the The KeyPress event to trap the Tab key.

I have to write a routine to block the input of certain characters due to the fact that these characters are causing errors when exporting the report into excel.

I can trap all except the tab key I know it is considered a special key by the operating system, and gets used before being passed on to the VB event handlers in order to do form navigation.

Know any way round this?

keypreview property set to true

Private Sub txtComplaint_KeyPress(KeyAscii As Integer)

Select Case KeyAscii
Case vbKeyReturn, vbKeyTab
'cancels out the keystroke
KeyAscii = 0
Beep
Case 34
KeyAscii = 0
End Select

End Sub

Thanks
 
To trap the tab key you will need to turn of tabstop on all your controls.
 
Hi Disferente
Tried that on all the controls on that form but still cant catch it strangely jumps to another control (tabIndex 21) and misses all the ones in between, its tabindex is 13.
 
Are you sure that the tabstop is false on ALL controls? Check on the control you said about (tabindex 21), it seems to me that that one have tabstop on.
 
The only way I know to capture Tab key is by using keyboard hooks.
See code in thread222-1119081 originally posted by strongm.

I am not sure if this information can be used to block the tab key input to a text box. My suggestion is, before sending the data to the report, you mask or replace all unwanted characters from the text box.
 
Add a richtextbox and some other controls for testing

Code:
Private Sub Form_Load()
    StopTabs
End Sub

Private Sub StopTabs()
    On Error Resume Next 'Resume next if control doesnt have tabstop property
    
    For Each Control In Me
        Control.TabStop = False
    Next
    
    'Control to maintain focus on tab
    RichTextBox1.TabStop = True
End Sub
 
>I have to write a routine to block the input of certain characters due to the fact that these characters are causing errors when exporting the report into excel.

Should have read before my above post
 
Here is a better solution for you

Code:
Private Sub Form_Load()
    StopTabs
    Me.KeyPreview = True
End Sub

Private Sub StopTabs()
    On Error Resume Next
    
    For Each Control In Me
        Control.TabStop = False
    Next
    
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyTab Then
        MsgBox "TAB"
    End If
End Sub
 
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
SendKeys "{TAB}"
KeyAscii = 0
End If
End Sub

 
> have to write a routine to block the input of certain characters due to the fact that these characters are causing errors when exporting the report into excel.

Why not just remove the Tabs in the text prior to exporting?
If the user knows that the Tabs will be removed when exporting, then it is up to them not to use them.
 
Erm, I'm slightly confused. A TAB char should not be inserted in your text when you press the TAB key. It doesn't "[get] used before being passed on to the VB event handlers in order to do form navigation", it goes straight to form navigation.

The only normal way that a TAB can be inserted from the keyboard is by using CTRL-TAB (e.g on a Richtextbox or on a multiline-enabled textbox). And CTRL-TAB, under those circumstances can be trapped and eliminated using the control's KeyDown event (no need to muck about with keypreview or tabstops or hooks):

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 9 Then KeyCode = 0
End Sub

Of course this won't get around people pasting tabs into the control ...
 
I Guys, thanks for all your answers and help some really interesting ones in there.

Unfortunately I made a school boy error in actually believing what my client was saying, doh!!!!!

Strongm thanks you turned the light on and was correct in saying
“Erm, I'm slightly confused. A TAB char should not be inserted in your text when you press the TAB key. It doesn't "[get] used before being passed on to the VB event handlers in order to do form navigation", it goes straight to form navigation.”

Made me re think so am going down the CTRL-TAB route.

Once again thanks all

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top