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

I want the tab key to emulate the enter key 1

Status
Not open for further replies.

tyedyejenn

Programmer
Jun 14, 2000
35
US
I have looked through other postings and see lots of help for having the enter key work like the tab key, but what I need is just the opposite:

I have a button on my form with the default property set to true so when the user presses the enter key the code in the cmdButton_click event executes, that part is ok

However most of the users I am dealing with are more likely to press the Tab key, so if the user presses the tab key from within a textbox I want the code in cmdButton_click to execute

I have tried trapping for the vbKeytab in the keypress, keyup, and keydown for my textbox and also in the form keyup, keydown and keypress events and so far none of that works

Any suggestions on how to trick my program into treating the tab key like the enter key would be greatly appreciated

Thanks
Jenn
 
Have you tried calling the button_click event from the textbox key_down? Check for the tab key and then call the button_click event.

 
You may also try setting cmdButton.Value = True in the textbox's LostFocus routine if there's another control that will recieve the focus when Tab is pressed. Difference between a madman and a genius:
A madman uses his genius destructively,
A genius uses his madness constructively.
 
There is a better way to do this than with send keys (it has some draw backs)

Public Const WM_KEYDOWN = &H100
Public Declare Function PostMessage Lib "User32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Sub TabOnEnter(KeyAscii As Integer)

On Error Resume Next
If KeyAscii = vbKeyReturn Then
If TypeOf Screen.ActiveControl Is TextBox Then
If Screen.ActiveControl.MultiLine Then Exit Sub
End If
KeyAscii = 0
PostMessage Screen.ActiveControl.hwnd, WM_KEYDOWN, vbKeyTab, 0
End If
End Sub

In The keyPress of a control type :

TabOnEnter KeyAscii

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top