key press event not working for "ENTER"

alcorjr

Active Member
Joined
Dec 29, 2002
Messages
416
I guys I'm trying to have this little script run from ENTER, and it's not doing it. Any ideas? :) :)
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  TextBox1.EnterKeyBehavior = True

If KeyAscii = 13 Then
pwd = Me.TextBox1.Text
If Me.TextBox1.Text <> "" Then
If pwd = "matri" Or pwd = "leogr" Or pwd = "kgbkgb27" Then
Me.TextBox1.Text = Empty

Exit Sub
Else:
If Me.TextBox1.Text <> "" Then
    Dim msg9 As String
msg9 = "El password no es correcto"
CreateObject("WScript.Shell").Popup msg9, vbSystemnomode
Me.TextBox1.Text = Empty
pwd = Empty
End If
End If
End If
End If
End Sub

Thanks
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Fron Excel Help

A KeyPress event does not occur under the following conditions:

Pressing TAB.


Pressing ENTER.


Pressing an arrow key.


When a keystroke causes the focus to move from one control to another.
 
Upvote 0
Try using VBKeyReturn


Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then
MsgBox "It's your thang..." & vbCrLf & "Do whatcha wanna do...", 64, "Enter was clicked."
KeyCode = 0
End If
End Sub
 
Upvote 0
Thanks for your interest. I tried the vbKeyReturn approach, and "nada".
I tested the key by placing
Code:
If KeyCode = vbKeyReturn Then
End If

and putting a breakpoint there, and guess what....keyCode appears Empty.
So....what gives???? :unsure: :unsure:

I can't believe Xcel won't allow such a commom Key as ENTER to be assigned!!!
:confused: :confused:
 
Upvote 0
This works as tested in an ActiveX / userform text box, if you are attempting to trap the act of the Enter key being pressed. If that is what you want to do, then post the entire code that failed, with how you applied VBKeyReturn, so we can see what's what. This should work; it does for me.
 
Upvote 0
Thanks
There it is:
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 

If KeyCode = vbKeyReturn Then

pwd = Me.TextBox1.Text
If Me.TextBox1.Text <> "" Then
If pwd = "matri" Or pwd = "leogr" Or pwd = "kgbkgb27" Then
Me.TextBox1.Text = Empty

Exit Sub
Else:
If Me.TextBox1.Text <> "" Then
    Dim msg9 As String
msg9 = "El password no es correcto"
CreateObject("WScript.Shell").Popup msg9, vbSystemnomode
Me.TextBox1.Text = Empty
pwd = Empty
End If
End If
End If
End If
End Sub
 
Upvote 0
Try replacing this Sub line you keep using
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

with this Sub line for the VBKeyReturn arguments I posted:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)


Any better?
 
Upvote 0
I stand in awe!!!

:wink:

Thanks a bunch.
So, Am I correct in assuming that the same event "header" could be used to run some code from the arrow keys ?
 
Upvote 0
I would not give an across-the-board Yes answer to that because some keys are quirky, like the Enter key.

What happened here was, you did not use the Sub line that contains the arguments for the Enter key that I posted; you tried to force your existing Sub line which works for some keys but not others, such as Enter. In some cases you might be able to get away with that but there will be a time like this when you cannot. For myself, I find that trial & error experimentation is the best route to figure it out once, and then I make a note of the solution for the next time I run across the problem. Always something new to learn with this stuff.
 
Upvote 0
Thanks for the advice. I already tried threKeydown event, in this case on a combobox (with the arrow keys), establishing the condition of the KeyCode to 37, 40 etc. It works like a charm :p :p
 
Upvote 0

Forum statistics

Threads
1,213,535
Messages
6,114,198
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top