I need dropdown list with text field where i can type and dropdown will filter on basis of text.
Like I have data of all countries name and I type "AME" in my textbox so only country containing word "AME" it will show list of countries containing character "AME" only and I can select from list (Like America).
I tried using combo box
Private Sub Combo1_GotFocus()
Combo1.AddItem "America"
Combo1.AddItem "Europe"
Combo1.AddItem "China"
Combo1.AddItem "INDIA"
Combo1.AddItem "London"
End Sub
There are 2 issue I am facing
Generally, I like using third-party controls for features that are not included with standard controls. However, a combination of API calls and the KeyPress event may get you close enough.
For easier coding this solution depends upon a Recordset containing country names.
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Const CB_SHOWDROPDOWN = &H14F
Private m_Countries As ADODB.Recordset
Private Sub Form_Load()
Set m_Countries = New ADODB.Recordset
m_Countries.Fields.Append "Country", adVarChar, 50
m_Countries.Open
m_Countries.AddNew "Country", "America"
m_Countries.AddNew "Country", "Europe"
m_Countries.AddNew "Country", "China"
m_Countries.AddNew "Country", "Chicago"
m_Countries.AddNew "Country", "INDIA"
m_Countries.AddNew "Country", "London"
m_Countries.MoveFirst
Combo1.Text = ""
FillCombo
End Sub
Private Sub Combo1_GotFocus()
SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, True, ByVal 0
End Sub
Private Sub Combo1_LostFocus()
SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, False, ByVal 0
End Sub
Private Sub Combo1_KeyPress(KeyAscii As Integer)
Dim Filter As String
If KeyAscii > 31 Then
Filter = Combo1.Text & Chr(KeyAscii) 'typed char
ElseIf KeyAscii = 8 And Combo1.Text <> "" Then
Filter = Left(Combo1.Text, Len(Combo1.Text) - 1) 'backspace
End If
If Filter <> "" Then
m_Countries.Filter = "Country LIKE '" & Filter & "%'"
Else
m_Countries.Filter = ""
End If
FillCombo
End Sub
Private Sub FillCombo()
Dim i As Integer
'to avoid interferring with the textbox of the combo don't use the Clear method
For i = Combo1.ListCount - 1 To 0 Step -1
Combo1.RemoveItem i
Next
Do While Not (m_Countries.BOF Or m_Countries.EOF)
Combo1.AddItem m_Countries.Fields("Country").Value
m_Countries.MoveNext
Loop
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.