Keypress/Key down VS UserForm : does not work!

B

bad_clone

Hello everybody,

I am currently developing a custom software using Excel's VBA macros.

My problem is : when I use the code :

Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer)
MsgBox "Test"
End Sub

nothing happens even if the userform has the focus. Trying with an
empty
form worked fine, but once I've added a textbox, nothing happened.

Any ideas?

Thanks!
 
J

Jay Freedman

Hello everybody,

I am currently developing a custom software using Excel's VBA macros.

My problem is : when I use the code :

Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer)
MsgBox "Test"
End Sub

nothing happens even if the userform has the focus. Trying with an
empty
form worked fine, but once I've added a textbox, nothing happened.

Any ideas?

Thanks!

The UserForm_KeyDown event occurs only if the userform itself (the background
that you stick controls on) has the focus when the key is pressed. As soon as
you add any controls, they get the focus instead.

If you want a procedure that tracks keypresses in a text box control, you need
to use the procedure

TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

(or whatever name you gave to the text box).

You might also want to look at the TextBox1_Change() procedure.
 
F

francois.chartrand

The UserForm_KeyDown event occurs only if the userform itself (the background
that you stick controls on) has the focus when the key is pressed. As soon as
you add any controls, they get the focus instead.

If you want a procedure that tracks keypresses in a text box control, you need
to use the procedure

TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

(or whatever name you gave to the text box).

You might also want to look at the TextBox1_Change() procedure.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.

So how can I do if I want to create shortcut keys on my UserForm (ex.:
CTRL+M)?

I don't want to use XYZ_KeyDown function for each control that could
get the focus...

Thanks
 
J

Jay Freedman

So how can I do if I want to create shortcut keys on my UserForm (ex.:
CTRL+M)?

I don't want to use XYZ_KeyDown function for each control that could
get the focus...

Thanks

It depends on what you want to use the shortcut for. If it's just to move the
focus to a particular control, then each button, label, checkbox, and option
button control has an Accelerator property that you can assign a value to.

Text boxes, list boxes, and combo boxes don't have an Accelerator property of
their own, but you should put a label control next to each of those (and
immediately before it in the tab order); since the label can't take the focus,
pressing the label's accelerator sends the focus to the next control which is
the associated box.

If you had some other purpose in mind, you have no alternative to writing all
those Keydown procedures. However, each can be a one-line call to the one
procedure that does the actual work.
 

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