KeyPress to close a form

2

I try to make some forms close by pressing 'Esc', using

Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii = 27 Then Unload Me
End Sub

But it does not work. Neither with KeyDown or KeyUp .

For some reason the subroutine is not executed (if instead of the If... I put a msgbox it does not appear either).

I'm using VBA in Microsoft Word 2003

    
asked by Gonzalo 22.12.2016 в 09:53
source

2 answers

2

I have found a solution on the Internet. It's about putting False of the property TabStop of all controls on the form.

But the price is high. My form has a couple of text fields on which I need to be able to move with Tab , without being forced to do so with the mouse pointer.

I put it here in case someone is in a similar situation and this solution is useful. In my case it does not work for me: I prefer not to be able to close it with Esc to not be able to use Tab to scroll through its TextBox .

So for my case the question is still open.

    
answered by 22.12.2016 в 10:56
0

To close the forms, pressing Esc , I do the following:

  • Instance a CommandButton and place it in a place that is not visible in the form.

  • In the properties window, I assign the value false to the property Cancel .

  • In the click event of the CommandButton entry the code: Unload Me .

  • answered by 31.01.2017 в 18:53