Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to use textbox and datalist to search?

Status
Not open for further replies.

longmatch

Programmer
Nov 1, 2001
406
0
0
Dear Friends:
I am trying to design a textbox for user input. When uses put letters in this box, I would like to show corresponding records. For example, when users put A, the name with starting with A will show. I also want user to click the record in datalist for record editing. It there any event to use for this kind operation? I do not know how to do it. Please give me a hand.

Thanks

Haijun
 
Const LB_FINDSTRING = &H18F
Const LB_FINDSTRINGEXACT = &H1A2

Private Declare Function SendMessage Lib "user32" _ Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As _ Long, ByVal wParam As Long, lParam As Any) As Long

dim nIndx as Long

nIndx = SendMessage(lstAuto.hwnd, LB_FINDSTRING, -1, "A")

If nIndx <> -1 then
lstAuto.ListIndex = nIndx
Else
MsgBox &quot;A is not found&quot;
End If

Haijun,

I may have misunderstood you post but if I didn't this should help you. The sendmessage function will return the list index of the matching record if there is one. It will return a -1 or whatever you specify it to return in the &quot;wParam&quot; when calling the function. Then simply check to see if nIndx is = -1 and if it is handle it as you see fit. If nIndx <> -1 then just set the listindex of the listbox equal to the nIndex variable. If this helps let me know.

Derek


P.S. You can use either the LB_FINDSTRING or the LB_FINDSTRINGEXACT constant. Experiment and see which one best suites your needs.
 
This is the code that I have always used to do this. I tried Derek's code and couldn't get it to work. If you can get that to work then that is probably better. Put this code in the change even of your text box:

Code:
    Dim LCV As Long
    
    For LCV = 0 To List1.ListCount - 1
        If LCase(Left(List1.List(LCV), Len(Text1.Text))) = LCase(Text1) Then
            List1.Selected(LCV) = True
            Exit Sub
        End If
    Next
    
    'if here then no match found
    MsgBox &quot;No match found&quot;
 
Sorry.....Try Taking the underscores out of the Declare statement. Then try it...It should work?
 
Also...If the listcount never exceeds 1000 (or maybe even more) I would go ahead and use bjd4jc's code. The only reason I use the API call is because I'm dealing with listboxes with sometimes over 9000 records...Where a loop like that is very time consuming. Especially if the record you're looking for is at the end of the list. Hope this helps.
 
Thank you for your help. I have a few questions for bjd4jc.
Can this code be used in datalist control? I can not find the listcount property. If I use list for display the item, how to populate the list items using code? I have a adodc control in my form, I get run-time error '91' when I use adodc to manipulate the data.


Thanks

Haijun
 
Is there some reason why you need to use the DataList control? I have never used it, always just used the regular listbox.

What line of code are you getting the runtime error on?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top