Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Feb 2001
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Closing a userform with the Esc key (Excel 2000 SR1)

    Hello,

    This should be an easy one. I have a userform that I would like for the user to be able to close by hitting the Escape key. How do I capture the keystroke in order to close the form?

    Thanks

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Closing a userform with the Esc key (Excel 2000 SR1)

    An easy way is to put a command button on the userform.
    Set its Caption to Cancel, or Close, or something like that.
    Set its Cancel property to True.
    In its OnClick event procedure, put the instruction Unload Me (or Me.Hide if you only want to hide the userform).

  3. #3
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Closing a userform with the Esc key (Excel 2000 SR1)

    You can add code to the keypress event of the form

    <pre>Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii.Value = 27 Then Unload Me
    End Sub
    </pre>


    Steve

  4. #4
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Closing a userform with the Esc key (Excel 2000 SR1)

    Hans, I've always had a "niggling" little query about this, so now might be the time to put it to rest. Since code has to be supplied anyway, to effect closure of the form, what is the purpose of the Cancel property? I know that only one CommandButton can have its Cancel property set to true, but this doesn't really explain the purpose of this property.

    Alan

  5. #5
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Closing a userform with the Esc key (Excel 2000 SR1)

    The Cancel property specifies which button gets "pressed" when you press the ESC key.
    Legare Coleman

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Closing a userform with the Esc key (Excel 2000 SR1)

    Alan,

    Legare has already answered your question. For completeness, command buttons also have a boolean property Default. Only one command button on a form can have Default = True.
    When the user presses the Enter key anywhere in the form, the "Default" command button will be pressed (except in text boxes whose MultiLine AND EnterKeyBehavior properties are set to True; there, the Enter key will start a new line). An "OK" button is an obvious candidate for being Default.

  7. #7
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Closing a userform with the Esc key (Excel 2000 SR1)

    Thanks Hans & Legare. I was actually under the impression (for some reason) that there were more issues involved than this.

    The way I understand it now (hopefully correct) is that setting the Cancel property to True will effectively associate the Keypress event described by Steve, to run the OnClick event of the nominated "Cancel" button, which can, in turn, contain the code supplied by Hans, as further confirmed by Legare.

    Perfectly clear.

    Alan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •