Iluvpresident
Iluvpresident

Reputation: 175

Activate UserForm with only one 'Enter'

I want to start up the UserForm with ONLY pressing on a number and on Enter (so no clicking involved). The problem I have is with pressing Enter twice. What happens if I press Enter the first time is it focuses on Doorgaan and after the second Enter it actually 'clicks' Doorgaan.

How it looks

Does anyone know how I can change this to one enter?

Upvotes: 2

Views: 1618

Answers (2)

paul bica
paul bica

Reputation: 10715

Ralph's solution is the easiest way to implement it if you have a simple form like yours

If you have multiple buttons, or use labels instead of buttons you can do something like this

Private Sub TxtBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    If KeyCode = vbKeyReturn Then Me.Hide    'or CommandButton1_Click

End Sub

Upvotes: 3

Ralph
Ralph

Reputation: 9434

Just set Doorgaan to Default within the properties in the VBE. There is only one element per form that can be default. The default element will be activated upon pressing Enter. So, if you set that button to Default = True then the code for that button will run when you press enter.

You can also change the element which is Default during runtime like so:

UserForm1.btnDoorgaan.Default = True

By the way, there is also the option to set an element to Cancel = True. That element gets activated when you hit Escape on the key board. So, if you add a Cancel button to your form and assign it to Cancel = True then you can easily close and exit that form by just pressing Escape.

Upvotes: 4

Related Questions