Link to home
Start Free TrialLog in
Avatar of cstephen100
cstephen100

asked on

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,

Regards
Stephen

***********
Private Sub txt_Description_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then
Me.List_Search_Result.SetFocus
Me.txt_Description.SetFocus

***********************************
Avatar of Harisha M G
Harisha M G
Flag of India image

Hi cstephen100,
   
    You can use 10 instead of 13

Bye
---
Harish
cstephen100,
    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
Avatar of codeconqueror
codeconqueror

Or, just look at the KeyDown event instead of KeyPress.

<CODE SNIPPET>
Private Sub txt_Description_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
        '**** Run the SQL Query
End If
End Sub
</CODE SNIPPET>
Avatar of cstephen100

ASKER


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
Stephen
I'm wondering if using vbCrLf will do the trick:

If KeyAscii = vbCrLf Then
Me.List_Search_Result.SetFocus
Me.txt_Description.SetFocus

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...

Preece
>> 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....?

Preece
ASKER CERTIFIED SOLUTION
Avatar of Harisha M G
Harisha M G
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial