Consulting

Results 1 to 8 of 8

Thread: How to exit a combobox on KeyPress

  1. #1
    VBAX Regular
    Joined
    Jun 2004
    Location
    Denmark
    Posts
    58
    Location

    How to exit a combobox on KeyPress

    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....

    BR
    Tommy Bak

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi Tommy,

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

  3. #3
    VBAX Regular
    Joined
    Jun 2004
    Location
    Denmark
    Posts
    58
    Location
    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

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    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.

  5. #5
    VBAX Regular
    Joined
    May 2004
    Location
    Sydney, Australia
    Posts
    36
    Location
    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

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    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?

  7. #7
    VBAX Regular
    Joined
    May 2004
    Location
    Sydney, Australia
    Posts
    36
    Location
    Quote Originally Posted by firefytr
    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.

    Regards,
    Daniel

  8. #8
    VBAX Regular
    Joined
    Jun 2004
    Location
    Denmark
    Posts
    58
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •