Solved

Searching through a ListBox for a specific value

Posted on 2004-03-26
8
460 Views
Last Modified: 2012-08-14

Hi there,

I'm trying to figure out a way to search through items in a listbox for a specific word or phrase.  I've found a way to do it by using.........

Const LB_FINDSTRING = &H18F

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

.......but that only works if the user enters exactly what is on the entire line in the listbox.

My listbox contains a concatenation of about 10 different fields from the table, so a few sample lines might look something like this...

34651 - 03/25/2004 - 27 MAIN ROAD - EASTGATE SUBDIVISION - WALTER GOSSE - JANE GOSSE
14387 - 01/15/2004 - 145 SPRUCE STREET - OAK SUBDIVISION - JACK JONES - JUDY JONES
39843 - 09/18/2003 - 8 TOPSAIL ROAD - WOODLANDS SUBDIVISION - JOHN SMITH - BEV SMITH
26349 - 02/13/2004 - 88 CHURCH ROAD - GRANVILLE SUBDIVISION - JIM TATE - MARY WHITE

I want the user to be able to type in a search phrase, for example, JOHN, and have it make the line with the word JOHN in it the selected record.

It will work if JOHN is at the beginning of the line, but not in the middle somewhere.

Do you know if there is a way to do this?

Thanks!
Christy
:-)

0
Comment
Question by:Christina4485
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 7

Accepted Solution

by:
ramesh12 earned 75 total points
ID: 10690290
for counter=0 to list.lstcount -1
 if instr(list.item(counter))>0 then
    list.listindex=counter
    exit for
 end if
next

Some thing like this if you are asking for vb code
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10690298
'In this example the result is placed in a textbox, and its Multiline property set to True. To modify it, replace Text1.Text.

Private Sub Command1_Click()
Call FindInList("John")
End Sub

Private Sub FindInList(ByVal fnWhat As String)
Dim c As Integer
Text1.Text = ""
For c = 0 To List1.ListCount - 1
    If InStr(1, LCase(List1.List(c)), LCase(fnWhat)) > 0 Then
        Text1.Text = Text1.Text & List1.List(c) & vbCrLf
    End If
Next c
End Sub

S
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10690328
ps. The way the function works is, that in case there are more than one "John" in the list, it will bring all "John" to the result box.

S
0
 
LVL 13

Expert Comment

by:imarshad
ID: 10690372
I have not tried this but I think it should work.....

Option Explicit

Private Declare Function SendMessagebyString Lib _
"user32" Alias "SendMessageA" (ByVal hWND As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As String) As Long

Private Const LB_FINDSTRINGEXACT = &H1A2
Private Const CB_FINDSTRINGEXACT = &H158


Public Function FindStringinListControl(ListControl As Object, _
   ByVal SearchText As String) As Long

'Input:
'ListControl: List or ComboBox Object
'SearchText: String to Search For

'Returns: ListIndex of Item if found
'or -1 if not found
   
Dim lHwnd As Long
Dim lMsg As Long

On Error Resume Next
lHwnd = ListControl.hWND

If TypeOf ListControl Is ListBox Then
    lMsg = LB_FINDSTRINGEXACT
ElseIf TypeOf ListControl Is ComboBox Then
    lMsg = CB_FINDSTRINGEXACT
Else
    FindStringinListControl = -1
    Exit Function
End If

FindStringinListControl = SendMessagebyString _
  (lHwnd, lMsg, -1, SearchText)

End Function
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 13

Expert Comment

by:imarshad
ID: 10690427
Another good property (though not related to your question) of ListBoxes from Codeguru

By changing the SendMessage Function's "ByVal wParam as Long" to "ByVal wParam as String", we change the search ability from first letter only, to "change-as-we-type" searching.


Here's some example code. Start a new Standard EXE project and add a ListBox (List1) and a TextBox (Text1), then paste in the following code :


option Explicit

'Start a new Standard-EXE project.
'Add a textbox and a listbox control to form 1
'Add the following code to form1:

private Declare Function SendMessage Lib "User32" _
        Alias "SendMessageA" (byval _
        hWnd as Long, _
        byval wMsg as Integer, _
        byval wParam as string, _
        lParam as Any) as Long

Const LB_FINDSTRING = &H18F

private Sub Form_Load()

    With List1
        .Clear
        .AddItem "RAM"
        .AddItem "rams"
        .AddItem "RAMBO"
        .AddItem "ROM"
        .AddItem "Roma"
        .AddItem "Rome"
        .AddItem "Rommel"
        .AddItem "Cache"
        .AddItem "Cash"
    End With

End Sub

private Sub Text1_Change()
    List1.ListIndex = SendMessage(List1.hWnd, LB_FINDSTRING, _
                      Text1, byval Text1.Text)
End Sub

0
 
LVL 5

Assisted Solution

by:Pi7
Pi7 earned 50 total points
ID: 10702530
Another way is to do it this way

Set  the MultiSelect property of the list box to 1( do this only if more than 1 item can be selected)
Paste the code below in your project

Private Sub Highlite(StrItem as String)

Dim Icount as Integer

For Icount = 0 To List1.ListCount - 1
List1.Selected(Icount)= Instr(List1.List(Icount),LCase(StrItem))  'tests for case insensitive strings
Next Icount

Private Command1_Click()

Highlite Text1.Text 'here I'm assuming the string to be searched for was typed in textbox 1
End Sub

Hopefully this should guide you

Regards,
Pi7
0
 

Author Comment

by:Christina4485
ID: 10706373

Pi7 - that works pretty good, thanks!  

Only problem is if the user clicks the "find" button again, I need it to go to the next instance of that string in the listbox.

Maybe I'll try changing the code a bit myself.

Any ideas?

Thanks!
Christy
0
 

Author Comment

by:Christina4485
ID: 10706809

Ok, I ended up using ramesh12's answer, and modified it a bit to be able to find consecutive matches.

Pi7 - yours would have worked well to - I think both were equally correct!

Thanks everyone for your help!

Christy
:-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now