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

Enter key to behave like the Tab key

Status
Not open for further replies.

Wantok52

Technical User
Dec 23, 2002
26
AU
I have a Visual Bsic project with several text fields on a form. When I type text in a text box I Tab to get the focus to the next text box.

I am trying to do this using a barcode scanner for some of the fields, but need to have the scanner set for CrLf at the end of the text, because the scanner is also used with another device that requires CrLf.

How can I force ths Enter key to behave like a Tab key in my VB project?
 
A quick search turned up the following:

thread222-892853
thread222-1021059
thread222-707567

Swi
 
Thanks Swi,

I too did a search. (After I posted...sorry. It's gettting late here).

Found this;

Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKeyReturn
SendKeys ("{tab}")
KeyAscii = 0
End Select
End Sub

Thanks to waytech2003, I've given you a star!!

Works a treat!

Thanks
 
I would avoid sendkeys in this context, and suggest that that code could use improvement. It's much easier to respecify KeyAscii. So:
Code:
if KeyAscii = vbKeyReturn then
   KeyAscii = vbKeyTab
End If
When you set keyascii to 0, you're in fact canceling the key press. So, as you can see, the sendkeys thing is not so efficient here.

HTH

Bob
 
Hi Bob,

Is this code in the Form_KeyPress sub?

I tried the code and it won't move to the next field.

Regards,

Marcus
 
Hi Peter,

I have set KeyPreview=True in the Form Properties.

This code works:

Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKeyReturn
SendKeys ("{tab}")
KeyAscii = 0
End Select
End Sub

but if I use;

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn then
KeyAscii = vbKeyTab
End If
End Sub

it doesn't work.

I'm happy with the first solution, but BobRodes suggested that the respecify KeyAscii is more effecient.

Also, is the KeyAscii=0 line necessary? It seems to work with KeyAscii=0 and without.

One thing that comes to mind is that it may be the "event" has gone before KeyAscii is set, so the SendKeys method is sending Tab after the Return Key has been pressed.

Regards,

Marcus
 
If you don't include " KeyAscii=0" the computer will beep on the key press.

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Sorry, dumb idea. I should have tested it before I put it up. I didn't know that KeyPress event doesn't capture the tab key! While I don't like using SendKeys, that appears to be the only way to do it, unless there's some sort of SendMessage API code that would capture the tab key.

As you say, my code doesn't work. :~/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top