Vatchy
670 pts. | Oct 21 2009 1:06AM GMT
Try this code:
Public Sub AutoCompleteCombo(Combo1 As ComboBox, KeyAscii As Integer)
Dim lCnt As Long ‘Generic long counter
Dim lMax As Long
Dim sComboItem As String
Dim sComboText As String ‘Text currently in combobox
Dim sText As String ‘Text after keypressed
Dim miSelStart As Integer
miSelStart = Combo1.SelStart ‘Set the entry point of the selected text
If KeyAscii = 13 Or KeyAscii = 27 Then Exit Sub ‘Charage Return or escape
With Combo1
lMax = .ListCount - 1 ‘The number of values in the list
sComboText = .Text ‘The current text in the list
‘**********************************************
‘This block either removes or adds a character
‘***********************************************
If KeyAscii = 8 Then ‘Backspace
‘If there is one character left, remove the text
If Len(Combo1.Text) = 1 Or Len(Combo1.Text) = 0 Then
Combo1.Text = “”
miSelStart = 0
Exit Sub
‘There is one character and seltext (our matched text)
ElseIf Len(Combo1.Text) - (Len(Combo1.Text) - miSelStart) = 1 Then
Combo1.Text = “”
miSelStart = 0
Exit Sub
End If
‘An ordinary backspace, decrement 1 character
Combo1.Text = Left(sComboText, miSelStart - 1)
sText = Left(sComboText, miSelStart - 1) ‘reset our sText var sText = combo1.text??
Else
‘A char other than backspace, return or escape was pressed
sText = Left(sComboText, miSelStart) & Chr(KeyAscii) ‘Increment our string with the new char
End If
KeyAscii = 0 ‘Reset key pressed
‘**********************************************
‘This block performs our lookup
‘**********************************************
For lCnt = 0 To lMax
sComboItem = .List(lCnt) ‘Current item
If UCase(sText) = UCase(Left(sComboItem, _
Len(sText))) Then ‘A match was found
‘.ListIndex = lCnt ‘Not sure why this is needed
.Text = sComboItem
.SelStart = Len(sText) ‘Start the highlighting after manually entered text
.SelLength = Len(sComboItem) - (Len(sText)) ‘Highlight to the end of the text
Exit For
Else
.Text = sText ‘Set the text value = the new text
.SelStart = Len(.Text) ‘Set selstart to the end of the string
End If
Next ‘lCnt
End With
End Sub






