Link to home
Start Free TrialLog in
Avatar of bwright
bwright

asked on

using the vbCrLf

I have a text box where people are enterig numeric data, when they are ready to go to next text box and they hit the enter key, I would like to position them in the next text box for data entry.  I believe I would use the keypress event, but unable to understand how to code.
Avatar of mkmccreary
mkmccreary

Yes, you would use the KeyPress Event.  Now, are you going to use the enter key for any other reason on the form besides moving to the next control?  This makes a big difference in the code.  

Let Me Know,
Martin
Shoot,
   I meant to post that as a comment, not an answer.  Please reject it.

Later,
Martin
Martin's right (as usual), and if you don't need to CR in the TextBox,
I think the code you're looking for is :

Create two TextBoxes, Text1 and Text2, then:

Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii = vbKeyReturn Then
                Me.Text2.SetFocus
        End If

End Sub

Let me know if it's not enough.

Dom
Avatar of bwright

ASKER

The only other places that i would use the enter key is when i am on a control
Here is another thought.  Set the Form's KeyPreview property to True.  Now, under the KeyPress event for the form use this code:

Private Sub Form_KeyPress(KeyAscii As Integer)

    If TypeOf Me.ActiveControl Is TextBox Then
        If KeyAscii = 13 Then
            SendKeys "{TAB}"
        End If
    End If
   
End Sub

With this you don't have to code for every textbox.  One problem with the code above, you can't have the Default property of any of the command buttons on the form set to true.

Good Luck,
Martin
Avatar of bwright

ASKER

When I try either suggestion, when I hit the enter key I receive a beep... Is this normal or is there a setting missing
Use this code at the end of the KeyPress Even

KeyAscii = 0
McIx is correct, that does work, but I don't know why.  Maybe McIx will tell us.

Later,
Martin
By the way, if you use my code I would set KeyAscii to 0 after the SendKeys in the if statement.  I do have an improvement on my code for added flexibility.

Private Sub Form_KeyPress(KeyAscii As Integer)

    Select Case KeyAscii
        Case 13
            ' Enter Key was pressed
            If TypeOf Me.ActiveControl Is TextBox Then
                SendKeys "{TAB}"
                KeyAscii = 0
            End If
        Case Else
            ' Do nothing, let the keystroke go through
    End Select    

End Sub

This way, if you decide to something on other keystrokes, it will be fairly easy to add.

Later,
Martin
Avatar of bwright

ASKER

Hey Martin,

Your answer worked like a champ for me... Now it works great

Please submit as an answer so that I can give you the points

Thanks to all that helped

Buster Wright
As you wish Martin...

KeyAscii = 0 resets the keyboard buffer.  You will notice with KeyPreview set to true, the Form_KeyPress event is fired, but because we reset the buffer, the TextBox_KeyPress Events are not.

Instead of hard-coding the 13 for the Carriage Return you should use the Vb Constant vbKeyReturn.  Not that the ASCII value for Carriage Return is going to change anytime soon, but I believe it makes the code more readable.

In the object browser search for KeyCodeConstants, it has all of the VB Key Constants.


ASKER CERTIFIED SOLUTION
Avatar of mkmccreary
mkmccreary

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Buster,

In your original question, you indicated that these TextBoxes will contain numeric data, are you validating for numeric data in your TextBox also?
Avatar of bwright

ASKER

Thanks very much... It worked very well, and thanks mcix for the information... Now what do you know about the "REM" statement!!

Buster Wright
REM is a great musical group...