Ask the MultiValued Visual Basic Expert - #2

(as published in Spectrum magazine Jan/Feb 1997)

updated March 5, 1999
To email your questions to "Ask the MultiValued VB Expert", click here.
Copyright 1996-99 Caduceus Consulting. All rights reserved.

Using the Enter key to go from field to field

We are rewriting our main data entry screen from a character-based program on a uniVerse platform to Visual Basic. Unfortunately our users are used to using the Enter key to move from field to field, not the Windows standard Tab key. How can I give them back their Enter key and are there any problems with that? - A. Crocker, B & G Insurance

You raise an interesting issue. As a rule I counsel clients to follow the established Windows standards as closely as possible. Only by doing so do you take advantage of its universal acceptance and gain a short learning curve for new users of your application. However, despite Microsoft’s efforts, many users, especially those trained in a character-based environment, now use the Enter key instinctively. No matter how many copies of Windows they have sold, the Enter key is still the biggest key on everybody’s keyboard! Fortunately there is an easy solution, giving them a choice of either the Tab key or the Enter key to navigate from one field to the next.

Step one is to ensure that your data entry fields are in the right tab order for your users to go from one to the next. This is done by setting the TabIndex property of each control to reflect the sequential order of the fields, beginning at 0.

Visual Basic allows you to capture key strokes using the KeyPress event. To capture an Enter key (ASCII 13), you must first ensure that there are no Command Button controls on your form with a Default property of True. Otherwise, the Enter key would click the Default button instead of going to the KeyPress event.Rather than putting code on each control’s KeyPress event, we can use one event for the whole form by setting the Form control’s KeyPreview property to True. By doing so, all of the keystrokes will now be sent to the Form_KeyPress event first. Our objective is now to capture the Enter key and convert it to a Tab key. So we have a start:

Sub Form_KeyPress (KeyAscii _ As Integer)
....If KeyAscii = 13 Then
........<what goes here??>
....End If
End Sub

Note that simply changing KeyAscii from 13 to 9 will NOT work - by the time it gets to the Form_KeyPress event it is too late to treat KeyAscii as a Tab.

The trick is to send a Tab key, just as if the user had pressed Tab instead of Enter, and then ignore the Enter key that they did press, using the following code:

Sub Form_KeyPress (KeyAscii As Integer)
....If KeyAscii = 13 Then
........SendKeys "{TAB}"
........KeyAscii = 0
....End If
End Sub

Bug Notice: SendKeys "{TAB}" has been reported to occasionally change the state of the CapsLock/NumLock keys in Visual Basic version 5.0. Please use caution!

This code will work for most controls, with the exception of a Command Button. If the current control (the one with Focus) is a Command Button, then pressing the Enter key will always invoke the Click event for that button and will NOT run our Form_KeyPress code. This is probably what the user would expect anyway, however, unless the Command Button takes the user out of that form, you may want to move to a different control after the button has been clicked. To do this, simply add the following line at the end of the button’s Click event, where “NextControl” is the name of the control to go to:

Sub Command1_Click()
....' <code for Click event>
....NextControl.SetFocus
End Sub

As a final enhancement, you may want the Enter key to still give a new line when the user is in a TextBox whose MultiLine property is set to True. If so, use the enhanced code shown below (note that I put the code in its own library routine):

Sub Form_KeyPress (KeyAscii As Integer)
....EnterAsTab KeyAscii
End Sub

Sub EnterAsTab (KeyAscii As Integer)
If KeyAscii = 13 Then
....If TypeOf Screen.ActiveForm.ActiveControl Is TextBox Then
........With Screen.ActiveForm.ActiveControl
............If .MultiLine Then
................If .Text = "" Then
....................' CR in empty box will become tab
................Else
....................' Needs 2 Enter keystrokes at end
....................' of text to exit text box.
....................If .SelStart <> Len(.Text) Then
........................' Cursor not at end of text...
........................Exit Sub
....................End If
....................If Right(.Text, 1) <> Chr(10) Then
........................' Previous char. not LF...
........................Exit Sub
....................End If
................End If
............End If
........End With
....End If
....SendKeys "{TAB}"
....KeyAscii = 0
End If
End Sub

To email your questions to "Ask the MultiValued VB Expert", click here.
Copyright 1996-99 Caduceus Consulting. All rights reserved.
Revised: March 5, 1999.

Return to Caduceus Consulting Home Page

Copyright 2006 intellact
Last modified: Thursday May 25, 2006