KeyDown-KeyUp, how do I make it work on a form?

KeyDown-KeyUp, how do I make it work on a form?

Anonymous
Not applicable
1,290 Views
7 Replies
Message 1 of 8

KeyDown-KeyUp, how do I make it work on a form?

Anonymous
Not applicable
How in the world do I get a VBA UserForm to acceppt KeyDown and KeyUp, I
can get the following to work in text boxes but not on the form:

NO WORKY:
Private Sub UserForm_KeyDown(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
msgbox KeyCode
End Sub

YES WORKY:
Private Sub txtBoxIamHere_KeyDown(ByVal KeyCode As_
MSForms.ReturnInteger, ByVal Shift As Integer)
msgbox KeyCode
End Sub

any ideas?


I know this is VBA and in VB I think I might ise keypreview or similar to
sniff out keys. any ideas?

thanks joseguia
0 Likes
1,291 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
Jose Guia had this to say:

> any ideas?

If this is related to your form hide idea, my suggestion will stop
working as soon as the form is hidden from view (nothing on the form
has the focus). The rest of the time, you can simply forward the
KeyAscii parameter to the form's KeyPress procedure from any of the
controls on that form:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Call UserForm_KeyPress(KeyAscii)

End Sub

--
http://www.acadx.com
"If you want to be somebody else change your mind"
0 Likes
Message 3 of 8

Anonymous
Not applicable
Because a VBA UserForm does not support a KeyPreview property like VB the key events will occur only on the control that has focus. A form can have the focus only if it has no controls on it or all its visible controls are disabled. Hence, if you have controls such as a TextBox on your form then key events will occur only for the controls and not the form unless you disable all the controls first.

Joe
--
0 Likes
Message 4 of 8

Anonymous
Not applicable
but this solution will prevent you from entering text in your control...
0 Likes
Message 5 of 8

Anonymous
Not applicable
That is what I wanted to know.

 

thanks

 

so I would be better off putting the
keydown

 

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Because
a VBA UserForm does not support a KeyPreview property like VB the key events
will occur only on the control that has focus. A form can have the focus only
if it has no controls on it or all its visible controls are disabled. Hence,
if you have controls such as a TextBox on your form then key events will occur
only for the controls and not the form unless you disable all the controls
first.

Joe
--

0 Likes
Message 6 of 8

Anonymous
Not applicable
Ezeckiel had this to say:

> but this solution will prevent you from entering text in your
> control...

No, it will not.

--
http://www.acadx.com
"If you want to be somebody else change your mind"
0 Likes
Message 7 of 8

Anonymous
Not applicable
It's just a bad practice for one event to be calling another event.

Joe
--

"Frank Oquendo" wrote in message
news:18C343794D1B2B447A911C7BF4C0A994@in.WebX.maYIadrTaRb...
> Ezeckiel had this to say:
>
> > but this solution will prevent you from entering text in your
> > control...
>
> No, it will not.
>
> --
> http://www.acadx.com
> "If you want to be somebody else change your mind"
>
>
0 Likes
Message 8 of 8

Anonymous
Not applicable
Joe Sutphin had this to say:

> It's just a bad practice for one event to be calling another event.
>
> Joe

Lots of programmers would disagree with you, including me. There's
nothing "bad" about using message chains. Hooks rely on the passing of
parameters from one procedure to another. So do derived classes, or
have you never seen a call MyBase.New()?

--
http://www.acadx.com
"If you want to be somebody else change your mind"
0 Likes