Solved

using the vbCrLf

Posted on 1998-10-16
15
467 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

929 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

18 Experts available now in Live!

Get 1:1 Help Now