Home > How-To Library > Controls

AutoComplete Combo box

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
Public Function AutoCompleteCombo(Combo1 As ComboBox, KeyAscii As Integer) 'Code to Auto complete combo box 'Call this function as: 'Call AutoCompleteCombo(ComboBoxName,Keyascii) Dim lCnt As Long Dim lMax As Long Dim sComboItem As String Dim sComboText As String Dim sText As String Dim miSelStart As Integer miSelStart = Combo1.SelStart If KeyAscii = 13 Or KeyAscii = 27 Then Exit Function With Combo1 lMax = .ListCount - 1 sComboText = .Text If KeyAscii = 8 Then If Len(Combo1.Text) = 1 Or Len(Combo1.Text) = 0 Then Combo1.Text = "" miSelStart = 0 Exit Function ElseIf Len(Combo1.Text) - (Len(Combo1.Text) - miSelStart) = 1 Then Combo1.Text = "" miSelStart = 0 Exit Function End If Combo1.Text = Left(sComboText, miSelStart - 1) sText = Left(sComboText, miSelStart - 1) Else sText = Left(sComboText, miSelStart) & Chr(KeyAscii) End If KeyAscii = 0 For lCnt = 0 To lMax sComboItem = .List(lCnt) If UCase(sText) = UCase(Left(sComboItem, Len(sText))) Then .Text = sComboItem .SelStart = Len(sText) .SelLength = Len(sComboItem) - (Len(sText)) Exit For Else .Text = sText .SelStart = Len(.Text) End If Next End With End Function

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.