In Access, I have a form fTest with the unbound combo box cmbOddelek that has search-as-you-type enabled, VBA code below:
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.
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.