Issue with keyascii and enter key

Hi all,
  I am using a vba program, I have a form that displays products on a list box.

 When I enter text in my text box and press Enter (On keyboard), an sql statement is run which displays the results in a list box.

The issue I am having is strange though.  I use keyascii to determine id I pressed enter or not, however if I use my laptop and press enter, it enters the loop ""if keyascii = 13 then""( as shown in code below), this is what I want.  However, if I press enter on a Desktop (Any desktop) it doesnt enter ascii loop.  However, if I hold down shift and press enter it works on desktops.

Im not sure if I am making since,  have I got the wrong ascii number for enter key??

Hope someone can help,


Private Sub txt_Description_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Harisha M GEngineerCommented:
Hi cstephen100,
    You can use 10 instead of 13

Harisha M GEngineerCommented:
    10 and 13 are often confused.. One is newline and another is carriage return.

    So better use this.. which would work in both the computers...

    If KeyAscii = 13 Or KeyAscii = 10 Then
Or, just look at the KeyDown event instead of KeyPress.

Private Sub txt_Description_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
        '**** Run the SQL Query
End If
End Sub
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

cstephen100Author Commented:

Thanks for reply,
I set the Key Behaviour" property to "Enter a new line in Field". This causes the enter key to stay in the control, and not move focus.  This worked for me, somehow enter must not be recognised as key stroke unless property is set.

Thanks for help
I'm wondering if using vbCrLf will do the trick:

If KeyAscii = vbCrLf Then

You might try it in either KeyPress or KeyDown...

Another idea (that works in VB) is to put the code under a command button and setting it's "Default" property to true.  So, when you are typing along in the textbox, hitting the enter key will fire the command button.  Not sure it this will work for you, but might be worth a try.  One caveat is if another command button has the focus, then hitting the enter key will fire that one; so this idea may be best suited if you only have one command button on the form...

Harisha M GEngineerCommented:
>> One caveat is if another command button has the focus, then hitting the enter key will fire that one

No, only Default button will be fired
Hmmmm...i just tried it with a new vb project and the second button was fired....?

Harisha M GEngineerCommented:
OK.. sorry... The default will trigger when the focus is on other controls like Text box etc.

Thanks :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.