PDA

View Full Version : [SOLVED] How to exit a combobox on KeyPress



tommy bak
08-23-2004, 09:19 AM
Hi

Anybody know what key / keycode to use to exit a combobox on sheet (control elements).
It seems that I have to use the mouse. That can't be true or....:confused:

BR
Tommy Bak

Zack Barresse
08-23-2004, 09:45 AM
Hi Tommy,

I always use Esc if not having a predefined type of key press method.

tommy bak
08-23-2004, 09:51 AM
hi zack

that's ok as an emergecy solution, but it would be nice to exit it with enter or tab or something like that (seems more natural to the user)


BR
Tommy Bak

Zack Barresse
08-23-2004, 02:58 PM
Tommy,

Maybe you can figure this one out. I can use this code with a ListBox and it worked beautiful, but on a ComboBox, it doesn't work... (??)


Sub ComboBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 13 Then
Sheets("Sheet1").Range("B6").Activate
End If
End Sub

KeyAscii 13 is the Enter key. You won't be able to (I don't think) use TAB unless you can find a way to integrate that into a routine with SendKeys maybe.

Daniel Klann
08-23-2004, 03:17 PM
Hi Tommy,

You can capture the tab key within the KeyDown event e.g.



Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 9 Then Me.Range("B10").Select
End Sub


HTH
Dan

Zack Barresse
08-23-2004, 03:23 PM
Dan! Beautiful!

Maybe that's where I was going wrong, the help files said vbKeyTab value was 0x9, but I never put in just 9. Doh! Would you know why this doesn't work well with the KeyPress method? I error out if I change it?

Daniel Klann
08-23-2004, 04:23 PM
Dan! Beautiful!

Maybe that's where I was going wrong, the help files said vbKeyTab value was 0x9, but I never put in just 9. Doh! Would you know why this doesn't work well with the KeyPress method? I error out if I change it?From the help file:-



A KeyPress event does not occur under the following conditions:

Pressing TAB
Pressing ENTER
Pressing an arrow key.
When a keystroke causes the focus to move from one control to another.
I think the first condition is the answer to your question. :cool:

Regards,
Daniel

tommy bak
08-23-2004, 11:04 PM
Hi Dan
Thankyou very much, it works fine.
I thought I had tried that, but obviosly I hadn't
and thanks to you too, Zack, for your effort.
BR
Tommy Bak