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:
  1. Option Explicit
  2.  
  3. Private blnBackSpace As Boolean
  4.  
  5. Private Sub txtWord_Change()
  6.     Dim i As Long
  7.     Dim x As Long
  8.    
  9.     If Len(txtWord.Text) = 0 Then Exit Sub
  10.    
  11.     If blnBackSpace = True Then
  12.         blnBackSpace = False
  13.         Exit Sub
  14.     End If
  15.    
  16.     lstWordCheck.Clear
  17.    
  18.     For i = LBound(swords) To UBound(swords)
  19.         If swords(i) Like txtWord.Text & "*" Then
  20.             ListAddItem lstWordCheck, swords(i)
  21.             x = x + 1
  22.             If x = 100 Then Exit For
  23.         End If
  24.     Next i
  25. End Sub
  26.  
  27. Private Sub txtWord_KeyDown(KeyCode As Integer, Shift As Integer)
  28. If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then
  29.     If Len(txtWord.Text) <> 0 Then
  30.         blnBackSpace = True
  31.     End If
  32. End If
  33. End Sub