Discussion Locked

Using Enter to navigate tab sequence

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am creating a VB 6 front end to an Access 2000 database and want to simulate Access' capability of moving through the tab sequence of my controls by pressing Enter as well as Tab. I do this with the following code for each control (varies slightly for Option Buttons, the DateTimePicker, etc.):

Private Sub txtCheckNum_KeyPress(KeyAscii As Integer)
' Use the Enter key to advance to the next field.
If KeyAscii = Asc(vbCrLf) Then
SendKeys "{TAB}"
End If
End Sub

The problem is, when I press Enter to move from a text box, the text disappears. The data is still captured and sent to the DB when I append the record, but I don't want my users to have to put up with this disconcerting behavior; plus, they may go back and add the data again, resulting in an error message that the text is too long for the field (because it is actually duplicated).

Thanks in advance for any help rendered.

5 replies

by marshwiggle

P.S. I forgot to add, this behavior does not occur with Combo Boxes, Masked Edit Boxes, or any of the other controls on the form.

by tony hopkinson

That's because you are doing Enter and then sending a tab, what you want is a tab only, so you need to trap enter before it gets processed and change it to a tab as in Keyascii = 8. Keydown might be a better choice.

P.S. the behavior of the keys changes if the component is in dropdown mode, so you might want to test for that as well.

by marshwiggle

Actually, I have to use Keydown w/ the DateTimePicker, which I also have on the form, so I tried that and it doesn't do the job. But you are correct in that characters (two little boxes) representing vbCrLf are being added to the text, and I have to strip them off before setting the Recordset Field value w/ the Text Box value or they are sent to the database, where they are not visible, but cause problems later when I try to retrieve them.

by marshwiggle

Upon further investigation, I discovered that because the text boxes in question were multiline-enabled, but not high enough to fully accommodate two lines, the text was being scrolled up (and out of sight) because VB was processing the vbCrLf passed by pressing Enter before sending the Tab key to advance to the next control.

I increased the height of the text boxes, and the problem went away.

This discussion is closed to new replies.