Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KeyPress(KeyAscii...

Status
Not open for further replies.

rickyoswaldiow

Programmer
Jul 16, 2007
127
GB
I am trying to capture a key press inside a text box, if the user hits the enter key after typing somthing in the block of code will execute
Code:
Private Sub txtOldPass_KeyPress(KeyAscii As Integer)
    If (KeyAscii = 13) Then
        'DO STUFF
    End If
    
End Sub

I have had similar code running in the past but I'm not sure what is going wrong here :|
 
It must be something in the DO STUFF that isn't working because the keyascii bit is fine.

I would recommend including a keyascii = 0 to stop the 'bell'

If (KeyAscii = 13) Then
Keyascii = 0
'DO STUFF
End If

[gray]Experience is something you don't get until just after you need it.[/gray]
 
A further thought. You haven't Locked the text box by any chance?

[gray]Experience is something you don't get until just after you need it.[/gray]
 
The code in 'DO STUFF works fine, it is an exact copy of the code from the command button next to the text box. What do you mean by 'locked'? The box is enabled if that's what you mean.
By the way, I am working in Access 2002 VBA, should I re-post this on that forum?
 
Basically, when I hit enter it is jumping to the next tab-order item and not executing any code.
 
>should I re-post this on that forum?

Youi should really ...

However:

1) Enter key becomes a special key when there is more than 1 control that can take the focus on the form. You can change this by changing the Enter Key property from 'Default' to 'Creates New Line'

2) Use the KeyDown event instead of KeyPress
 
Youi should really ...

Sorry about that, I didn't find that forum until after my post :)

1) This now is doing just that, instead of attempting to read the value it is just creating a new line.

2) There is no KeyAscii function for KeyDown, and KeyCode does not seem to be fixing the problem :|


I'll move the post over to the VBA forum, but thanks for the help!
 
instead of attempting to read the value it is just creating a new line
You should set the KeyAscii to zero as Error7 has said. This cancels out the keystroke.

 
As you suggest, I have done so but what is the reason? This does not make the function... function.
 
At the risk of sounding incredibly thick, where do you change the ENTER key properties?

HyperEngineer
If it ain't broke, it probably needs improvement.
 
>At the risk of sounding incredibly thick

Not at all - I think you've missed the fact that we are talking about Access 2002 (i.e Access XP) ...
 
Thanks strongm, that explains it. I feel a whole lot better.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
As you suggest, I have done so but what is the reason?
As I said, it cancels out the keystroke - I'm assuming you don't actually want a carriage return at the end of the line.
Have you confirmed that the code is going into the "DO STUFF"? I.e. have you put a breakpoint in the KeyPress code and watched if it evaluates Keyascii = 13 and goes into the "DO STUFF" code?


 
2) There is no KeyAscii function for KeyDown, and KeyCode does not seem to be fixing the problem :|

What condition did you check for KeyCode?

You should check If KeyCode=vbKeyReturn . This captures the pressing of the enter key.

-V
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top