-
Autocomplete
All,
I'm very new to VB and and learning very rapidly due to the knowledge on this site. I have a question/problem that I hope someone can help me out with.
I have a text box and a list view on a form. I would like to give the user the option of typing text into the text box and use an autocomplete functionality that would search the list view and find the first item in it with the string entered. As the user types more text it would keep searching the list view based upon the new string entered.
I have seen a few postings on this site through searching but have not found enough to get started.
All help would be greatly appreciated.
Thanks again from a beginner.
-
Here is a function that I found and modified for auto type ahead on a combo box.
It works great.
This may help you.
Code:
Option Explicit
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const CB_FINDSTRING = &H14C
Public Function AutoFind(ByRef cboCtl As ComboBox, ByRef KeyAscii As Integer, Optional ByRef LimitToList As Boolean = False)
'<RR 05/08/03 - VB/OUTLOOK GURU>
On Error GoTo No_Bugs
Dim lCB As Long
Dim sFindString As String
If KeyAscii = 8 Then
If cboCtl.SelStart <= 1 Then
cboCtl = ""
AutoFind = 0
Exit Function
End If
If cboCtl.SelLength = 0 Then
sFindString = UCase(Left(cboCtl, Len(cboCtl) - 1))
Else
sFindString = Left$(cboCtl.Text, cboCtl.SelStart - 1)
End If
ElseIf KeyAscii < 32 Or KeyAscii > 127 Then
Exit Function
Else
If cboCtl.SelLength = 0 Then
sFindString = UCase(cboCtl.Text & Chr$(KeyAscii))
Else
sFindString = Left$(cboCtl.Text, cboCtl.SelStart) & Chr$(KeyAscii)
End If
End If
lCB = SendMessageStr(cboCtl.hwnd, CB_FINDSTRING, -1, ByVal sFindString)
If lCB <> (-1) Then
cboCtl.ListIndex = lCB
cboCtl.SelStart = Len(sFindString)
cboCtl.SelLength = Len(cboCtl.Text) - cboCtl.SelStart
AutoFind = 0
Else
If LimitToList = True Then
AutoFind = 0
Else
AutoFind = KeyAscii
End If
End If
Exit Function
No_Bugs:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation, App.ProductName
End Function