Hi, I'm using the code below to create some sort of autocomplete textbox.
swords is an array with more than 100.000 words. Every time the user enters a letter into the textbox (txtWord), it loops though the swords array and uses the Like operator to find matching words and puts them into a listbox (lstWordCheck).
This works fine when the word starts with 'a', but when the word starts with 'z', then with every letter entered into the textbox, it has to loop all the way to the end of the array and that's very slow.
Is there a faster way to find matching words instead of having to loop always from the beginning?
vb Code:
Option Explicit Private blnBackSpace As Boolean Private Sub txtWord_Change() Dim i As Long Dim x As Long If Len(txtWord.Text) = 0 Then Exit Sub If blnBackSpace = True Then blnBackSpace = False Exit Sub End If lstWordCheck.Clear For i = LBound(swords) To UBound(swords) If swords(i) Like txtWord.Text & "*" Then ListAddItem lstWordCheck, swords(i) x = x + 1 If x = 100 Then Exit For End If Next i End Sub Private Sub txtWord_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then If Len(txtWord.Text) <> 0 Then blnBackSpace = True End If End If End Sub




Reply With Quote