home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD242012201999.psc / Autocomplete.bas next >
Encoding:
BASIC Source File  |  1999-12-20  |  3.2 KB  |  68 lines

  1. Attribute VB_Name = "Module1"
  2. '********************************************************************************************
  3.  
  4. 'This module makes that autocomplete thing in textboxes, you start typing
  5. 'and the textbox is completed with the rest of the word, just like IE 4.0
  6. 'This module does the search in a table, not from a listbox or other things.
  7. 'you will need to do 3 things before using the functions from this module
  8. '1st Choose the textbox to do the autocomplete thing
  9. '2nd On the OnKeyDown event of the textbox, call the CheckIsDelOrBack function
  10. '3rd On the OnChangeEvent of the textbox, call the AutoComplete function
  11. 'If you have more than one autocomplete textbox on the same form, set the property keypreview
  12. 'of the form to true and call the function CheckIsDelOrBack on the keydown event
  13. 'of the form instead of calling it from the textbox keydown event.
  14. 'KNOWN BUGS - it has problems when there is a " ' " (apostrophe) in the table, because of the SQL statement.
  15. 'The function will do the search correctly until it finds the apostrophe, but after that, it gets all screwed up.
  16. 'I couldn't figure how to fix that out, if you find a solution for that, I would like to
  17. 'be informed, so I can fix that in my projects too!!
  18. '                     *####*      mautheman@yahoo.com       *####*
  19.  
  20. '********************************************************************************************
  21. Public IsDelOrBack As Boolean
  22. Public Function AutoComplete(TheText As TextBox, TheDB As Database, TheTable As String, TheField As String) As Boolean
  23. On Error Resume Next
  24. '****************************************************************************************
  25. 'TheText is the textbox that will do the autocomplete thing
  26. 'TheField is the field from the table that has the information that will fill the textbox
  27. 'TheTable is the table where you will search for the information to fill the textbox
  28. 'TheDB is the database with the TheTable
  29. '****************************************************************************************
  30.  
  31. Dim OldLen As Integer
  32. Dim dsTemp As Recordset
  33.  
  34. AutoComplete = False
  35. If Not TheText.Text = "" And IsDelOrBack = False Then
  36.  
  37. OldLen = Len(TheText.Text)
  38.     Set dsTemp = TheDB.OpenRecordset("Select * from " & TheTable & " where " & TheField & " like '" & TheText.Text & "*'", dbOpenDynaset)
  39.       If Err = 3075 Then
  40.         'here we got a bug!!
  41.       End If
  42.          If Not dsTemp.RecordCount = 0 Then
  43.             TheText.Text = dsTemp(TheField)
  44.                 If TheText.SelText = "" Then
  45.                     TheText.SelStart = OldLen
  46.                 Else
  47.                     TheText.SelStart = InStr(TheText.Text, TheText.SelText)
  48.                 End If
  49.                     TheText.SelLength = Len(TheText.Text)
  50.                     AutoComplete = True
  51.                     
  52.         End If
  53.         
  54. End If
  55.  
  56. End Function
  57.  
  58. Public Function CheckIsDelOrBack(TheKey As Integer) As Boolean
  59. 'TheKey is the KeyCode - all you gotta do is write CheckIsDelOrBack(KeyCode) on the KeyDown event...
  60.     If TheKey = vbKeyBack Or TheKey = vbKeyDelete Then
  61.         IsDelOrBack = True
  62.         CheckIsDelOrBack = True
  63.     Else
  64.         IsDelOrBack = False
  65.         CheckIsDelOrBack = False
  66.     End If
  67. End Function
  68.