Pressing Esc to make a form disappear

J

jayray

I have a form with the usual OK and Cancel buttons. To problems
whatsoever with those buttons. But I have a finicky user who asks if
he can just make the form disappear by pressing the Esc key (he
doesn't want to have to move his mouse to the Cancel button and click,
or use an Alt + accelerator key, or use the tab key to go there. As I
said, he's finicky.)

I'm drawing a blank on how to have Excel read an Esc key as a trigger
to hide and unload the form. Any suggestions?
 
D

Dave Peterson

You can use the .cancel property for that commandbutton. You can change it in
the properties window (for that commandbutton).

Or you can use code:

Option Explicit
Private Sub CommandButton1_Click()
MsgBox "Hi from Ok"
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
With Me.CommandButton1
.Caption = "Ok"
.Default = True
End With

With Me.CommandButton2
.Caption = "Cancel"
.Cancel = True
End With
End Sub
 
J

jayray

You can use the .cancel property for that commandbutton.  You can change it in
the properties window (for that commandbutton).

Or you can use code:

Option Explicit
Private Sub CommandButton1_Click()
    MsgBox "Hi from Ok"
End Sub
Private Sub CommandButton2_Click()
    Unload Me
End Sub
Private Sub UserForm_Initialize()
    With Me.CommandButton1
        .Caption = "Ok"
        .Default = True
    End With

    With Me.CommandButton2
        .Caption = "Cancel"
        .Cancel = True
    End With
End Sub

I meant to say "No problems whatsoever with the OK and Cancel
buttons".

Thanks for the reply. The problem I am trying to solve is to make the
form disappear WITHOUT pressing either the OK or Cancel buttons on the
form. I would like the form to disappear by pressing the Esc key.
 
D

Dave Peterson

What happened when you tried the suggestion?
I meant to say "No problems whatsoever with the OK and Cancel
buttons".

Thanks for the reply. The problem I am trying to solve is to make the
form disappear WITHOUT pressing either the OK or Cancel buttons on the
form. I would like the form to disappear by pressing the Esc key.
 
J

jayray

What happened when you tried the suggestion?





jayraywrote:




--

Dave Peterson- Hide quoted text -

- Show quoted text -

Hey...what the...WOW! My apologies. I couldn't see where the magic
was (I still don't). Thanks very much.
 
J

jayray

Hey...what the...WOW!  My apologies. I couldn't see where the magic
was (I still don't). Thanks very much.- Hide quoted text -

- Show quoted text -

I see the magic is changing the properties of the Cancel button to
True (which is what you said at the very beginning. Obviously a case
of pearls before swine).
 
D

Dave Peterson

You found the magic for the ".cancel = true" bit, but what does the ".default =
true" stuff do?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top