Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

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.
0
bwright
Asked:
bwright
  • 6
  • 4
  • 4
  • +1
1 Solution
 
mkmccrearyCommented:
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
0
 
mkmccrearyCommented:
Shoot,
   I meant to post that as a comment, not an answer.  Please reject it.

Later,
Martin
0
 
dtavassoliCommented:
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bwrightAuthor Commented:
The only other places that i would use the enter key is when i am on a control
0
 
mkmccrearyCommented:
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
0
 
bwrightAuthor Commented:
When I try either suggestion, when I hit the enter key I receive a beep... Is this normal or is there a setting missing
0
 
mcixCommented:
Use this code at the end of the KeyPress Even

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

Later,
Martin
0
 
mkmccrearyCommented:
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
0
 
bwrightAuthor Commented:
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
0
 
mcixCommented:
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.


0
 
mkmccrearyCommented:
Again,  McIx is correct about using the constant, but I'll take the points.  He's kicking my tail, especially since I had to start over.  Starting over was my fault, though, I deserved it.

Later,
Martin
0
 
mcixCommented:
Buster,

In your original question, you indicated that these TextBoxes will contain numeric data, are you validating for numeric data in your TextBox also?
0
 
bwrightAuthor Commented:
Thanks very much... It worked very well, and thanks mcix for the information... Now what do you know about the "REM" statement!!

Buster Wright
0
 
mcixCommented:
REM is a great musical group...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now