Public IsDelOrBack As Boolean
Public Function AutoComplete(TheText As TextBox, _ TheDB As Database, TheTable As String, _ TheField As String) As Boolean On Error Resume Next Dim OldLen As Integer Dim dsTemp As Recordset AutoComplete = False If Not TheText.Text = "" And IsDelOrBack = False Then OldLen = Len(TheText.Text) Set dsTemp = TheDB.OpenRecordset _ ("Select * from " & TheTable & " where " & TheField & _ " like '" & TheText.Text & "*'", dbOpenDynaset) If Err = 3075 Then 'here we got a bug!! End If If Not dsTemp.RecordCount = 0 Then TheText.Text = dsTemp(TheField) If TheText.SelText = "" Then TheText.SelStart = OldLen Else TheText.SelStart = InStr(TheText.Text, TheText.SelText) End If TheText.SelLength = Len(TheText.Text) AutoComplete = True End If End If End Function Public Function CheckIsDelOrBack(TheKey As Integer) As Boolean 'TheKey is the KeyAscii - all you gotta do is write 'CheckIsDelOrBack(KeyAscii) on the KeyDown event... If TheKey = vbKeyBack Or TheKey = vbKeyDelete Then IsDelOrBack = True CheckIsDelOrBack = True Else IsDelOrBack = False CheckIsDelOrBack = False End If End Function Assumes: There are two functions in the module The Function CheckIsDelOrBack checks it the key that was pressed was Delete or BackSpace (so the code will be ignored) The Other one (Autocomplete) ... autocompletes the textbox!! This module makes that autocomplete thing in textboxes, you start typing and the textbox is completed with the rest of the word, just like IE 4.0 This module does the search in a table, not from a listbox or other things. you will need to do 3 things before using the functions from this module 1st Choose the textbox to do the autocomplete thing 2nd On the OnKeyDown event of the textbox, call the CheckIsDelOrBack function 3rd On the OnChangeEvent of the textbox, call the AutoComplete function. If you have more than one autocomplete textbox on the same form,set the property keypreview of the form to true and call the function CheckIsDelOrBack on the keydown event of the form instead of calling it from the textbox keydown event. KNOWN BUGS - it has problems when there is a " ' " (apostrophe) in the table, because of the SQL statement. The function will do the search correctly until it finds the apostrophe, but after that, it gets all screwed up. I couldn't figure how to fix that out, if you find a solution for that, I would like to |