How can a userform be unloaded while pressing Esc?
Printable View
How can a userform be unloaded while pressing Esc?
:)VB Code:
Option Explicit Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If KeyAscii = 27 Then Unload Me End If End Sub
How/Where do I introduce this code in order to make it work?
Behind the userform. ;)
I did so but it doesn't work.
What do you mean by doesnt work? No errors or nothing happens?
The userform is displayed and has focus?
The userform is properly displayed but when I press Esq nothing happens. The userform shows only half a dozen values in labels and I don't use the Setfocus property.
It doesn't work for me either.Quote:
What do you mean by doesnt work? No errors or nothing happens?
The userform is displayed and has focus?
@fonty: Here is a devilish way of achieving what you want ;)
create a commandbutton and set it's 'cancel' property to True.
Run the form...
Now when you press 'esc' it'll work...
Works perfectly fine for me in Excel 2003. Should I upload an example attachment?
Here is the example that I posted that works.
Hi Rob: That's working because there is no control on the form...
If you place a command button or a check box on the form, then it won't work...
That is correct. Good spot lol. All you have to do is like kollsid posted or place keypress code in a central handler so no matter what control is focussed it will fire.
The reason this occurs is because different from vb6 forms there is no KeyPreview property on the form which would guarentee the keypress event will always fire for the form.
I attach the xls files zipped.
RobBBBBBBBBBBBBBBBBBBBBBB!!!! it's not kollsid :D
I just disabled the frame and it works now. ;)
Sup kollsid! :D
no it still doesn't ...Quote:
I just disabled the frame and it works now.
place a checkbox or option button or a commandbutton on the form and then press escape...
No dont as Fonty didnt mention that he was going to add any other controls then what is already on the userform. If he wants to add more controls of different type then depending on the number of controls you can still code around it.