Results 1 to 16 of 16

Thread: Autocomplete textbox question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Autocomplete textbox question

    Hello all,

    I the following code (with the help of others) which looksup info in my listbox.

    vb Code:
    1. Private Const LB_FINDSTRING = &H18F
    2. Private Const LB_FINDSTRINGEXACT = &H1A2
    3.  
    4. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    5. (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    6.  
    7. Private Sub txtLookup_Change()
    8.     Dim strText$
    9.    
    10.     strText = Trim$(txtLookUp.Text)
    11.     If strText <> "" Then
    12.          lstRadioSerials.ListIndex = FindItem(lstRadioSerials, strText)
    13.     Else
    14.         MsgBox "No matching record found; Please check your criteria!", vbInformation + vbOKOnly, "No Record Match"
    15.     End If
    16. End Sub
    17.  
    18. Private Function FindItem(lst As ListBox, strText As String) As Integer
    19.    
    20.     Dim iIndex As Integer
    21.     iIndex = SendMessage(lst.hwnd, LB_FINDSTRINGEXACT, -1, ByVal strText)
    22.     FindItem = iIndex
    23. End Function

    This works for looking up the certain serials in my listbox, but what I would like to do is refine it a little more.

    What I would like to add is this: when a user starts to type in the serial number and he types a wrong number (ie. he is looking for 1234 and he types 1224), then I would like an error message to state that there is no record with that serial number.

    Right now when the user types the wrong number, the only thing that happens is that the highlighted part will not move anymore in the listbox.

    I hope that I am not too confusing?

  2. #2
    Member
    Join Date
    Nov 2007
    Posts
    51

    Re: Autocomplete textbox question

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Dim o2 As Object, o1 As Object,NotOnList as Boolean
    Set o2 = Text1
    Set o1 = WhateverListBoxName
    If KeyAscii = 8 Then 'Backspace key
    CheckComboInput o1, o2, KeyAscii
    Exit Sub
    End If


    CheckComboInput o1, o2, KeyAscii,NotOnList:KeyAscii = 0
    If NotOnLIst=True then …….give the MESSAGE or Whatever
    End Sub

    Public Sub CheckComboInput(c1 As Object, c2 As Object, N As_ Integer,NotOnList)
    'c1 as object the object is the ListBox that holds the list eg SerialNumbers
    'c2 as object is the Text control
    ' N as Integer is the Ascii val of the last character entered by the user
    'This procedure is accessed by Textbox keypress proceedures to check if what
    'is being entered is on the ListBox list
    Dim L As Integer, S As Integer, T As String, N2 As Integer
    N2 = N ' N2 holds keyascii so keyascii is returned unchanged as is on next line
    If N = 8 Then N = 0
    S = c2.SelStart: L = c2.SelLength: T = c2.Text
    T = Left(T, S) & Chr(N) & Right(T, Len(T) - S)
    S = S + 1: I = 0
    Do While (I < c1.ListCount - 1) And StrComp(Left(T, S), Left(c1.List(I), S), vbTextCompare) <> 0
    I = I + 1
    Loop
    If UCase(Left(T, S)) = UCase(Left(c1.List(I), S)) Then
    T = c1.List(I): L = Len(T) - S
    c1.Selected(I) = True
    Else
    For I = 0 To c1.ListCount - 1
    If c1.Selected(I) = True Then
    c1.Selected(I) = False: NotOnList=true
    else
    NotOnList=true
    Next
    T = Left(T, S): L = 0
    End If
    c2.Text = T: c2.SelStart = S: c2.SelLength = 1
    N = N2
    End Sub
    Last edited by dasjoe; Apr 19th, 2008 at 09:48 PM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Autocomplete textbox question

    dasjoe,

    Thanks for the reply; I'll give your code a try.


    Also, is there anyway that I can just add to the code that I am already using?

  4. #4
    Member
    Join Date
    Nov 2007
    Posts
    51

    Re: Autocomplete textbox question

    The CheckComboInput o1, o2, KeyAscii,NotOnList routine can be placed on a
    module, just make sure it is public. You can then call it from any list box
    keypress. It is basically an auto complete routine, it will complete the entry
    if it is listed on the list box. The NotOnList returns true if as soon as a key is pressed that does not make up what ever is listed in the list box. Only use the
    NotOnList if its needed otherwise use it as an autocomplete.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Autocomplete textbox question

    dasjoe,

    I get an error in the following block of code:

    vb Code:
    1. S = c2.SelStart: L = c2.SelLength: T = c2.Text

    Error: "Object doesnt support this property or method"

    Do I need to reference this? Not sure what to do.

    Thanks

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Autocomplete textbox question

    Quote Originally Posted by cfd33
    This works for looking up the certain serials in my listbox, but what I would like to do is refine it a little more.

    What I would like to add is this: when a user starts to type in the serial number and he types a wrong number (ie. he is looking for 1234 and he types 1224), then I would like an error message to state that there is no record with that serial number.

    Right now when the user types the wrong number, the only thing that happens is that the highlighted part will not move anymore in the listbox.

    I hope that I am not too confusing?
    A very simple solution for you is to throw away the listbox and the textbox, replace both of them with a single combobox.
    A combobox has 2 parts, a listbox and a textbox. It allows user to select an item in the list, it also allow user to type in.
    You can set MatchRequire to True, it will do the job for you and it also do autocomplete.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Autocomplete textbox question

    anhn,

    Thanks for the reply, but I would like to keep my setup the way it is now (with the listbox and textbox).

  8. #8
    Member
    Join Date
    Nov 2007
    Posts
    51

    Re: Autocomplete textbox question

    Sorry, Missed KeyAscii = 0
    also set objects reversed
    Try now, I have amended
    Also added Space Key provisions

    The List Box can be invisible if you like or add code so double clicking the listbox item will populate the test box
    Last edited by dasjoe; Apr 19th, 2008 at 09:43 PM.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Autocomplete textbox question

    Thanks,

    I will try it when I get back to work on Monday.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Autocomplete textbox question

    dasjoe,

    I get an error message at this code:
    vb Code:
    1. Private Sub txtLookup_KeyPress(KeyAscii As Integer)
    2.     Dim o2 As Object, o1 As Object, NotOnList As Boolean
    3.     Set o1 = lstRadioSerials
    4.     Set o2 = txtLookup
    5.    
    6.     If KeyAscii = 8 Then 'Backspace key
    7.         [COLOR="SeaGreen"]ChkListBox [/COLOR]o1, o2, KeyAscii <---Eror Here on ChkListBox!!
    8.         Exit Sub
    9.     End If
    10.  
    11.    
    12.     ChkListBox o1, o2, KeyAscii, NotOnList: KeyAscii = 0
    13.  
    14.    
    15.     If NotOnList = True Then
    16.         Call MsgBox("No matching record found; Please check your criteria!", vbInformation + vbOKOnly, "No Record Match")
    17.     End If
    18. End Sub

    "Argument not optional"

    Also, if a person types a serial that is not in the db, then where would I put a msgbox telling the users that there is no serial number like that in the db?




    dasjoe, I removed this part of the IF statement and now it allows backspace:
    vb Code:
    1. If KeyAscii = 8 Then 'Backspace key
    2.         'ChkListBox o1, o2, KeyAscii
    3.         Exit Sub
    4.     End If

    Is this ok to do?



    Is this part the part of code that tells the user that there is no record match if they should type a serial that is not in the database?
    vb Code:
    1. If NotOnList = True Then
    2.         Call MsgBox("No matching record found; Please check your criteria!", vbInformation + vbOKOnly, "No Record Match")
    3.     End If
    if it is, it is not working. (ie. 1234 and I type 1233, then I want a message box to appear stating that there is no serial like that)

    ____________________________________________________________________________________________________ ________________________________

    Ahhh, never mind on the messagebox! I placed the code for the messagebox in the module instead of the Keypress event and now it works just fine.
    Last edited by cfd33; Apr 21st, 2008 at 02:43 PM.

  11. #11
    Member
    Join Date
    Nov 2007
    Posts
    51

    Re: Autocomplete textbox question

    The messagebox is better left where it is, as this provides flexibility.
    The CheckListBox routine can be called from any text box/List Box combination. Good to have a flexible adaptable working routine that can be added to any project, as the message from message box may not always be the same let alone the same style.

    However after checking the original code I provided the was an error

    Missing end if onCheckComboBox

    Also I Noted the Ascii =8 'Space Bar
    incorrect, code is correct but Ascii 8 is backspace key. This line of code is required because it it isn't a backspace will invoke a not on list message.
    so when a backspace is entered, also you can not deleted using backspace because after call the CheckCombo routine Ascii is set to 0 meaning no
    backspace as backspace=8

    I have re-supplied ammended code, Sorry for not checking and running the code earlier, but did not have time.

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Dim o2 As Object, o1 As Object, NotOnList As Boolean
    Set o2 = Text1
    Set o1 = List1
    If KeyAscii = 8 Then 'Backspace key
    CheckComboInput o1, o2, KeyAscii, NotOnList
    Exit Sub
    End If

    CheckComboInput o1, o2, KeyAscii, NotOnList: KeyAscii = 0
    If NotOnList = True Then
    Dim msg As String, Style As String, Title As String
    msg = " Item not listed on Data Base."
    Style = vbInformation + vbOKOnly
    Title = "Listing not found"
    MsgBox msg, Style, Title
    End If
    End Sub

    Public Sub CheckComboInput(c1 As Object, c2 As Object, N As Integer, NotOnList)
    'c1 as object the object is the ListBox that holds the list eg SerialNumbers
    'c2 as object is the Text control
    ' N as Integer is the Ascii val of the last character entered by the user
    'This procedure is accessed by Textbox keypress proceedures to check if what
    'is being entered is on the ListBox list
    Dim L As Integer, S As Integer, T As String, N2 As Integer
    N2 = N ' N2 holds keyascii so keyascii is returned unchanged as is on next line
    If N = 8 Then N = 0
    S = c2.SelStart: L = c2.SelLength: T = c2.Text
    T = Left(T, S) & Chr(N) & Right(T, Len(T) - S)
    S = S + 1: I = 0
    Do While (I < c1.ListCount - 1) And StrComp(Left(T, S), Left(c1.List(I), S), vbTextCompare) <> 0
    I = I + 1
    Loop

    If UCase(Left(T, S)) = UCase(Left(c1.List(I), S)) Then
    T = c1.List(I): L = Len(T) - S
    c1.Selected(I) = True
    Else
    For I = 0 To c1.ListCount - 1
    If c1.Selected(I) = True Then
    c1.Selected(I) = False: NotOnList = True
    Else
    NotOnList = True
    End If
    Next
    T = Left(T, S): L = 0
    End If

    c2.Text = T: c2.SelStart = S: c2.SelLength = 1
    N = N2
    End Sub
    Last edited by dasjoe; Apr 21st, 2008 at 08:04 PM.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Autocomplete textbox question

    dasjoe,

    I must not understand about the msgbox; when I try the code like you have it below:
    vb Code:
    1. If NotOnList = True Then
    2.         Dim msg As String, Style As String, Title As String
    3.         msg = " Item not listed on Data Base."
    4.         Style = vbInformation + vbOKOnly
    5.         Title = "Listing not found"
    6.         MsgBox msg, Style, Title
    7.     End If
    it does not fire, but when I place the msgbox in the module, then it works great.

    What am I doing wrong?

    This is the whole code for the Keypress:
    vb Code:
    1. Private Sub txtLookup_KeyPress(KeyAscii As Integer)
    2.     Dim o2 As Object, o1 As Object, NotOnList As Boolean
    3.     Set o1 = lstRadioSerials
    4.     Set o2 = txtLookUp
    5.    
    6.     If KeyAscii = 8 Then 'Backspace key
    7.         ChkListBox o1, o2, KeyAscii, NotOnList
    8.         Exit Sub
    9.     End If
    10.  
    11.    
    12.     ChkListBox o1, o2, KeyAscii, NotOnList: KeyAscii = 0
    13.    
    14.    
    15.     If NotOnList = True Then
    16.         Dim msg As String, Style As String, Title As String
    17.         msg = " Item not listed on Data Base."
    18.         Style = vbInformation + vbOKOnly
    19.         Title = "Listing not found"
    20.         MsgBox msg, Style, Title
    21.     End If
    22.  
    23. End Sub
    Last edited by cfd33; Apr 21st, 2008 at 07:57 PM.

  13. #13
    Member
    Join Date
    Nov 2007
    Posts
    51

    Re: Autocomplete textbox question

    I have just did a copy and past straight from this site onto a new VB6
    project, added a textbox named Text1 and a ListBox name List1
    Left code as is and works fine. So ??? suggest you do the same on a new
    project. Do you have VB6 ??? By doing this you can prove at least this part
    of the coding.

    Cheers

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Autocomplete textbox question

    dasjoe,

    I will give what you said a try. Yes, I have vb6.

    I really appreciate all of your help!!

    I am still a newbie at this vb6, so please excuse me.

  15. #15
    Member
    Join Date
    Nov 2007
    Posts
    51

    Re: Autocomplete textbox question

    No problem, we all start somewhere
    Last edited by dasjoe; Apr 22nd, 2008 at 12:15 AM.

  16. #16
    Addicted Member
    Join Date
    Mar 2008
    Posts
    143

    Re: Autocomplete textbox question

    Quote Originally Posted by anhn
    You can set MatchRequire to True, it will do the job for you and it also do autocomplete.
    MatchRequire is not available in VB6.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width