Solved

using the vbCrLf

Posted on 1998-10-16
15
483 Views
Last Modified: 2013-12-26
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
Comment
Question by:bwright
  • 6
  • 4
  • 4
  • +1
15 Comments
 
LVL 2

Expert Comment

by:mkmccreary
ID: 1487700
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
 
LVL 2

Expert Comment

by:mkmccreary
ID: 1487701
Shoot,
   I meant to post that as a comment, not an answer.  Please reject it.

Later,
Martin
0
 
LVL 1

Expert Comment

by:dtavassoli
ID: 1487702
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!

 

Author Comment

by:bwright
ID: 1487703
The only other places that i would use the enter key is when i am on a control
0
 
LVL 2

Expert Comment

by:mkmccreary
ID: 1487704
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
 

Author Comment

by:bwright
ID: 1487705
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
 
LVL 4

Expert Comment

by:mcix
ID: 1487706
Use this code at the end of the KeyPress Even

KeyAscii = 0
0
 
LVL 2

Expert Comment

by:mkmccreary
ID: 1487707
McIx is correct, that does work, but I don't know why.  Maybe McIx will tell us.

Later,
Martin
0
 
LVL 2

Expert Comment

by:mkmccreary
ID: 1487708
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
 

Author Comment

by:bwright
ID: 1487709
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
 
LVL 4

Expert Comment

by:mcix
ID: 1487710
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
 
LVL 2

Accepted Solution

by:
mkmccreary earned 100 total points
ID: 1487711
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
 
LVL 4

Expert Comment

by:mcix
ID: 1487712
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
 

Author Comment

by:bwright
ID: 1487713
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
 
LVL 4

Expert Comment

by:mcix
ID: 1487714
REM is a great musical group...
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question