Solved

using the vbCrLf

Posted on 1998-10-16
15
459 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
Comment Utility
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
Comment Utility
Shoot,
   I meant to post that as a comment, not an answer.  Please reject it.

Later,
Martin
0
 
LVL 1

Expert Comment

by:dtavassoli
Comment Utility
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
 

Author Comment

by:bwright
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Use this code at the end of the KeyPress Even

KeyAscii = 0
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Expert Comment

by:mkmccreary
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
REM is a great musical group...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now