Wednesday, February 27, 2019

Type to select on a Combo Box


Private Sub cboGroup_GotFocus()
   Dim cboTTS As ComboBox
   Set cboTTS = cboGroup
   cboTTS.Tag = ""
   'Initialize the string of characters typed while on this control
   cboTTS.ToolTipText = ""
End Sub

Private Sub cboGroup_KeyPress(KeyAscii As Integer)
   Dim txtTTS As TextBox
   Set txtTTS = Me.ActiveControl
   
   'The code in the GotFocus() and KeyUp() events are needed to make this code work
   'The following line is required in the form's General Declarations:
   'Public dicPublicList As Object
   'Before this code is invoked, the dictionary must be populated like:
   'Set dicPublicList = CreateObject("Scripting.Dictionary")
   'dicPublicList.RemoveAll
   'dicPublicList.Add "Key1", "ValueOne"
   'dicPublicList.Add "Key2", "ValueTwo"
   'dicPublicList.Add "Key3", "ValueThree"
   
   If dicPublicList.Count < 1 Then
      Exit Sub
   End If
   txtTTS.Locked = True
   'Lock the text box during this subroutine or it won't work right
   Dim lI As Long
   If KeyAscii = 91 Then
      'User typed a [ character, which is a problem for the LIKE command used later, replace it with a ? character
      KeyAscii = 63
   End If
   If KeyAscii = 8 And Len(CStr(txtTTS.Tag)) > 0 Then
      'User typed a backspace
      txtTTS.Tag = Left(CStr(txtTTS.Tag), Len(CStr(txtTTS.Tag)) - 1)
      'Remove the latest character from the string of characters typed while on this control
      txtTTS.SelStart = Len(CStr(txtTTS.Tag))
      'Show how many characters have been typed
      txtTTS.SelLength = Len(CStr(txtTTS.Text))
      'Highlight the rest of the selected item
      txtTTS.ToolTipText = txtTTS.Tag
   End If
   If KeyAscii > 31 Then
      'The user did not type a control character
      txtTTS.Tag = CStr(txtTTS.Tag) + Chr(KeyAscii)
      'Add the character to the string of characters typed while on this control
      txtTTS.ToolTipText = txtTTS.Tag
      For lI = 1 To dicPublicList.Count
         'Look at each item in the dictionary
         If CStr(dicPublicList.Item(lI)) Like CStr(txtTTS.Tag) & "*" Then
            'The item starts with the same characters as those typed
            'Option Compare Text must be done on this form's general declarations
            txtTTS.Text = dicPublicList(lI)
            'Show the text of the item
            txtTTS.SelStart = Len(CStr(txtTTS.Tag))
            'Show how many characters have been typed
            txtTTS.SelLength = Len(CStr(txtTTS.Text))
            'Highlight the rest of the selected item
            Exit Sub
            'Stop looking at items
         End If
      Next lI
      txtTTS.Tag = Left(CStr(txtTTS.Tag), Len(CStr(txtTTS.Tag)) - 1)
      'Nothing matches, so remove this character from the string of characters typed
      txtTTS.ToolTipText = txtTTS.Tag
   End If
End Sub

Private Sub cboGroup_KeyUp(KeyCode As Integer, Shift As Integer)
   Dim cboTTS As ComboBox
   Set cboTTS = cboGroup
   cboTTS.Locked = False
   'Unlock the combo box so items can be selected with the mouse
End Sub

No comments:

Post a Comment