Solved

Searching through a ListBox for a specific value

Posted on 2004-03-26
8
462 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

770 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