Solved Combobox search as you type issue (1 Viewer)

frankt68

Registered User.
Local time
Today, 11:00
Joined
Mar 14, 2012
Messages
90
In Access, I have a form fTest with the unbound combo box cmbOddelek that has search-as-you-type enabled, VBA code below:
Code:
Private Sub cmbOddelek_KeyPress(KeyAscii As Integer)
    Dim strSearch As String
    Dim strSQL As String

    ' Combine the current text in the combobox with the typed character (ChrW(KeyAscii))
    strSearch = Me.cmbOddelek.Text & ChrW(KeyAscii)

    ' Uncomment the following line to debug and print the current search string
    ' Debug.Print "strSearch " & strSearch

    If Len(strSearch) > 0 Then
        ' Build the SQL query to search for matches in the "Oddelek:" column
        strSQL = "SELECT [ID_šifraSTRM], [Oddelek:] FROM Oddelki WHERE [Oddelek:] LIKE '*" & strSearch & "*' ORDER BY [Oddelek:];"

        ' Uncomment the following line to debug and print the SQL query
        ' Debug.Print "strSQL > 0 " & strSQL
    Else
        ' If no search string is provided, show all records
        strSQL = "SELECT [ID_šifraSTRM], [Oddelek:] FROM Oddelki ORDER BY [Oddelek:];"

        ' Uncomment the following line to debug and print the SQL query
        ' Debug.Print "strSQL = 0 " & strSQL
    End If

    ' Set the RowSource property of the combobox to the generated SQL query
    Me.cmbOddelek.RowSource = strSQL

    ' Display the dropdown list with filtered results
    Me.cmbOddelek.Dropdown

End Sub



The code generally works, but the issue is that it doesn't work for every typed letter. When I type the first letter, it nicely displays the dropdown list with filtered results. When I type the second letter, the list disappears (becomes empty). When I type the third letter, the list is visible again, and so on. Essentially, the list is visible only when an odd number of characters are entered into the combo box. I'm interested in what the problem might be. Attached is a sample file and an animated gif.
 

Attachments

  • Combobox search test.accdb
    832 KB · Views: 98
  • combobox search.gif
    combobox search.gif
    324.4 KB · Views: 70

theDBguy

I’m here to help
Staff member
Local time
Today, 02:00
Joined
Oct 29, 2018
Messages
21,473
For something like this, I prefer to use the Change event of the Combobox. You might give it a try to see if you'll get better results.
 

moke123

AWF VIP
Local time
Today, 05:00
Joined
Jan 11, 2013
Messages
3,920
I'm not quite sure what your trying to accomplish .

Below is code that will filter a form as you type. Perhaps you can adapt it to do what your trying to do.

Code:
Private blnSpace As Boolean

Private Sub Text3_Change()

    Dim strFilter As String

    If blnSpace = False Then
    
        strFilter = "FirstName Like ""*" & Me.Text3.Text & "*"" or LastName like ""*" & Me.Text3.Text & "*"""
        Me.Filter = strFilter
        Me.Requery
        Me.Refresh
        Me.Text3.SetFocus
        Me.Text3.SelStart = Len(Me.Text3.Text)
        
    End If
    
    Me.FilterOn = True
        
End Sub
 
Private Sub Text3_KeyPress(KeyAscii As Integer)

    If KeyAscii = 32 Then
        blnSpace = True
    Else
        blnSpace = False
    End If
    
End Sub
 

frankt68

Registered User.
Local time
Today, 11:00
Joined
Mar 14, 2012
Messages
90
For something like this, I prefer to use the Change event of the Combobox. You might give it a try to see if you'll get better results.
I've tried and doesn't work, the dropdown list turns blank as soon as I type the first letter
 

Josef P.

Well-known member
Local time
Today, 11:00
Joined
Feb 2, 2023
Messages
826
Another approach: I let the user enter several characters and start the data filtering with Return. This costs less data accesses.

Example: Open test form from attached file and insert *int*. Commit with return key. The filtering process can be repeated nested.
Note: Esc key deletes the filter.
 

Attachments

  • Combobox search test_CbxFilterControl.zip
    40.5 KB · Views: 84
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 02:00
Joined
Oct 29, 2018
Messages
21,473
I've tried and doesn't work, the dropdown list turns blank as soon as I type the first letter
I just gave it a try, and it seems to work for me. Open up Form1.
 

Attachments

  • Combobox search test.accdb
    452 KB · Views: 92

moke123

AWF VIP
Local time
Today, 05:00
Joined
Jan 11, 2013
Messages
3,920
I just gave it a try, and it seems to work for me. Open up Form1

I think it still has a glitch.

If I type in Cov it only returns one record until you hit backspace and then 3 records appear.
 

moke123

AWF VIP
Local time
Today, 05:00
Joined
Jan 11, 2013
Messages
3,920
Maybe try MajP's Fayt class

 

Gasman

Enthusiastic Amateur
Local time
Today, 10:00
Joined
Sep 21, 2011
Messages
14,301
I use the class from @MajP as mentioned above.
 

MarkK

bit cruncher
Local time
Today, 02:00
Joined
Mar 17, 2004
Messages
8,181
Here's code that works using the change event and a parameterized QueryDef...
Code:
Private Const SQL_SELECT As String = _
    "SELECT ID, Oddelek " & _
    "FROM Oddelki " & _
    "WHERE Oddelek LIKE p0 " & _
    "ORDER BY Oddelek;"
   
Private qdf_ As DAO.QueryDef

Property Get qdfSearch() As DAO.QueryDef
    If qdf_ Is Nothing Then Set qdf_ = CurrentDb.CreateQueryDef("", SQL_SELECT)
    Set qdfSearch = qdf_
End Property

Private Sub cmbOddelek_Change()
    With qdfSearch
        .Parameters(0) = "*" & Me.cmbOddelek.Text & "*"
        Set Me.cmbOddelek.Recordset = .OpenRecordset
    End With
    Me.cmbOddelek.Dropdown
End Sub

Private Sub Form_Load()
    Me.cmbOddelek.AutoExpand = False
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:00
Joined
Oct 29, 2018
Messages
21,473
I think it still has a glitch.

If I type in Cov it only returns one record until you hit backspace and then 3 records appear.
Thanks. Please try it now...
 

Attachments

  • Combobox search test.accdb
    452 KB · Views: 111

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:00
Joined
May 21, 2018
Messages
8,529
Works fine for me with one line of code.
Code:
Private FAYT As New FindAsYouTypeCombo
Private Sub Form_Load()
  FAYT.InitalizeFilterCombo Me.cmbOddelek, "[oddelek:]", anywhereinstring, True
End Sub
 

frankt68

Registered User.
Local time
Today, 11:00
Joined
Mar 14, 2012
Messages
90
I'm not quite sure what your trying to accomplish .
I want to reduce the number of displayed records in the dropdown list of the combo box containing the search term (= text entered in combo box) in any of the columns displayed in the combo box.

Below is code that will filter a form as you type. Perhaps you can adapt it to do what your trying to do.

Thank you for your contribution, but unfortunately, I couldn't figure out how to adapt it to make it work.
 

frankt68

Registered User.
Local time
Today, 11:00
Joined
Mar 14, 2012
Messages
90
Another approach: I let the user enter several characters and start the data filtering with Return. This costs less data accesses.

Example: Open test form from attached file and insert *int*. Commit with return key. The filtering process can be repeated nested.
Note: Esc key deletes the filter.
Thank you for your contribution. It works, but it's not exactly what I wanted because it doesn't display a filtered dropdown list; all the records are still in the list. When you enter the search text, it positions to the first matching record.
 

frankt68

Registered User.
Local time
Today, 11:00
Joined
Mar 14, 2012
Messages
90
Maybe try MajP's Fayt class

I did and it works fine.
 

frankt68

Registered User.
Local time
Today, 11:00
Joined
Mar 14, 2012
Messages
90
Here's code that works using the change event and a parameterized QueryDef...
Code:
Private Const SQL_SELECT As String = _
    "SELECT ID, Oddelek " & _
    "FROM Oddelki " & _
    "WHERE Oddelek LIKE p0 " & _
    "ORDER BY Oddelek;"
  
Private qdf_ As DAO.QueryDef

Property Get qdfSearch() As DAO.QueryDef
    If qdf_ Is Nothing Then Set qdf_ = CurrentDb.CreateQueryDef("", SQL_SELECT)
    Set qdfSearch = qdf_
End Property

Private Sub cmbOddelek_Change()
    With qdfSearch
        .Parameters(0) = "*" & Me.cmbOddelek.Text & "*"
        Set Me.cmbOddelek.Recordset = .OpenRecordset
    End With
    Me.cmbOddelek.Dropdown
End Sub

Private Sub Form_Load()
    Me.cmbOddelek.AutoExpand = False
End Sub
Thank you for your suggestion. I've tested the code, but when I run it, it returns a runtime error 3061: Too few parameters, marking this line of code: Set Me.cmbOddelek.Recordset = .OpenRecordset
 

frankt68

Registered User.
Local time
Today, 11:00
Joined
Mar 14, 2012
Messages
90
Thank you all for the good solution suggestions. I believe that FAYT is the most suitable solution. However, I still don't understand why the code I provided at the beginning is not working as expected.
 

Josef P.

Well-known member
Local time
Today, 11:00
Joined
Feb 2, 2023
Messages
826
...it doesn't display a filtered dropdown list; all the records are still in the list. When you enter the search text, it positions to the first matching record.
I use NotInList event to start filtering.
ComboboxFilterControl.gif
 

Users who are viewing this thread

Top Bottom