Tip: Easy ComboBox Dropdown

<< Click to Display Table of Contents >>

Navigation:  Access Controls >

Tip: Easy ComboBox Dropdown

Chris Weber                    

I miss the days when I could tab to a combo box, press the Alt-Down key combination, and the ComboBox would open for me. I suppose Microsoft removed the keystrokes because Alt should only be used for accessing menus. High-volume data entry operators hate taking their hands off the keyboard to reach for the mouse, and they also miss this keystroke combination. But Microsoft also added the Dropdown method to ComboBoxes, and with a few lines of VBA, your users can have the Alt-Down functionality. What’s more, users can skip using the Alt key altogether. You’ll need this module-level variable in your form’s class module and two simple snippets of code for your combo box’s KeyDown and Exit events:

Dim mfMyComboOpen As Boolean

 

Private Sub cboMyComboID_Exit(Cancel As Integer)

  mfMyComboOpen = False

End Sub

Private Sub cboMyComboID_KeyDown (KeyCode As Integer, Shift As Integer)

 

  If KeyCode = vbKeyDown Then

    If Not mfMyComboOpen Then

      cboMyComboID.Dropdown

      mfMyComboOpen = Not mfMyComboOpen

      KeyCode = 0

    End If

  End If

  

End Sub

The variable mfMyComboOpen acts as a static flag reflecting the open state of cboMyCombo. When the user is on the control, the KeyDown event traps just the first Down arrow key and uses it to execute the ComboBox’s Dropdown method. Thereafter, the Down arrow key can be used to select items in the ComboBox while it’s open. When your user tabs off the control, mfMyComboOpen is set to False, awaiting the next use of the ComboBox.