CodeItBetter Programming Another VB Programming Blog

AutoComplete Combo box

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'Controls - AutoComplete Combo box
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
Filed under: Controls Leave a comment
Comments (2) Trackbacks (0)
  1. keyascii is filled with ????

  2. Wow, this sure did the trick… Although it missed to point out that it should be CALLed in the KeyPress procedure of the ComboBox, passing the KeyAscii integer to the function.

    This is better than an earlier suggestion to install an add-on. Nicely done. I was trying to do the same and wasted 2 hours using SelStart, SelText, SelLenght properties in almost any possible sequence I can think off. What a bummer I am.

    Thank you for easing the pain.


Leave a comment


 

No trackbacks yet.