Keypress event doesn't work on userform

Ricky Morris

Active Member
Joined
Mar 31, 2002
Messages
363
The following code appears if it should work with a userform but doesn't.

Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
MsgBox "hi"
End Sub

I'm trying to unload the form when the user presses ESC but I can't even get the simple message box to code to execute. Help!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Instead of using a KeyPress event, just set one of your buttons Cancel property to True, and then pressing Esc will cause the form to close.

However, if you want to use the keypress event, then here it is...

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_KeyDown(<SPAN style="color:#00007F">ByVal</SPAN> KeyCode <SPAN style="color:#00007F">As</SPAN> MSForms.ReturnInteger, <SPAN style="color:#00007F">ByVal</SPAN> Shift <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>)
    <SPAN style="color:#00007F">If</SPAN> KeyCode = 27 <SPAN style="color:#00007F">Then</SPAN> Unload Me
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Upvote 0
When there is any control on the userform that can have the focus, the keypress event won't work....
Work around :
- set the "TabStop" property of every control to false
- use a "Select Case KeyAscii" to control your userform
- currently i'am working on a userform to open 260 different files with only 2 keys...
 
Upvote 0
Thanks Bruno...still an effective workaround.
When there is any control on the userform that can have the focus, the keypress event won't work....
Work around :
- set the "TabStop" property of every control to false
- use a "Select Case KeyAscii" to control your userform
- currently i'am working on a userform to open 260 different files with only 2 keys...
 
Upvote 0
When there is any control on the userform that can have the focus, the keypress event won't work....
Work around :
- set the "TabStop" property of every control to false
- use a "Select Case KeyAscii" to control your userform
- currently i'am working on a userform to open 260 different files with only 2 keys...


I was able to workaround this a different way. My Listbox was robbing focus from the UserForm_Keypress event, but I found a different event to capture, which I passed through to me original attempt. Code:

Code:
Private Sub ListBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    'keypress captured in listbox - pass it through to my intended keypress handler
    Call UserForm_KeyPress(KeyAscii)
End Sub


Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case 13 'enter key
            CommandButton_Go_Click
        Case Else 'who cares
    End Select
End Sub
 
Upvote 0
I was able to workaround this a different way. My Listbox was robbing focus from the UserForm_Keypress event, but I found a different event to capture, which I passed through to me original attempt. Code:

Code:
Private Sub ListBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    'keypress captured in listbox - pass it through to my intended keypress handler
    Call UserForm_KeyPress(KeyAscii)
End Sub


Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case 13 'enter key
            CommandButton_Go_Click
        Case Else 'who cares
    End Select
End Sub

Thanks, used something similar for a text box, however I had to set the MultLine property to 'True' for the 'Enter Key' to trigger the event.
 
Upvote 0
I was able to workaround this a different way. My Listbox was robbing focus from the UserForm_Keypress event, but I found a different event to capture, which I passed through to me original attempt. Code:

Code:
Private Sub ListBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    'keypress captured in listbox - pass it through to my intended keypress handler
    Call UserForm_KeyPress(KeyAscii)
End Sub


Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case 13 'enter key
            CommandButton_Go_Click
        Case Else 'who cares
    End Select
End Sub

Hi. I just want to add in case some need this as well. This post has been helpful to me as I was experiencing the same thing. Just needed to alter the code a bit by using TextBox1.SetFocus in Userform.Initialize and the Esc key automatically closes the userform.

Hope that helps!
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,930
Members
449,094
Latest member
teemeren

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