by Said Sowiny (2 Submissions)
Category: Coding Standards
Compatability: Visual Basic 5.0
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (6 Votes)
This is a high class code article
i never be stingy with vb lovers .
I try and success to move the power of access to the power of vb .
The main features are :
1) the sub can be used with combo box and list box controls.
2) High performance.
3) faster with huge lists.
4) fully support of Delete and Backspace keys.
5)Case Sensitive.
6) of course few lines of code point to the purpose.
Try it and tell me.
If you found this really advanced please be faithful and vote.
Thanks.
'==================================
Public Sub AutoCompleteList(ByVal cboCtl As ComboBox, ByVal KeyCode As Integer)
'No comments it is clear .
Dim Counter As Integer
Dim Length As Integer
If cboCtl.Text <> "" Then
If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then
KeyCode = 0
Exit Sub
End If
For Counter = 0 To cboCtl.ListCount - 1
Length = Len(cboCtl.Text)
If Mid(cboCtl.List(Counter), 1, Len(cboCtl)) = cboCtl.Text Then
cboCtl.Text = cboCtl.List(Counter)
cboCtl.SelStart = Length
cboCtl.SelLength = Len(cboCtl.Text)
cboCtl.ListIndex = Counter
Exit For
End If
Next
End If
End Sub
'===========================================
Typical usage :
Private Sub cboCategory_Change()
Call AutoCompleteList(cboCategory, mintKeyCode)
End Sub
Where mintKeyCode is a form variable given by a Form_KeyDown event like :
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
mintKeyCode = KeyCode
End Sub
and the cboCategory is the combo box filled by your own items.
Taking into consideration the header of the form should look like this one :
Option Explicit
Private mintKeyCode As Integer
and your form KeyPreview Property is set to TRUE.
Enjoy ,