[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 543
  • Last Modified:

Search listview Data with wildcard

Hi guys,

 Pls provide some code to search all the column in listview with wildcard.

0
aarontham
Asked:
aarontham
  • 5
  • 4
1 Solution
 
JR2003Commented:

Option Explicit

Private rtText As RichTextBox


Private Sub CancelButton_Click()

    Me.Visible = False

End Sub


Private Sub cbFind_Change()

    cmdFind.Enabled = (Len(cbFind.Text) > 0)

End Sub


Private Sub cmdFind_Click()

    Dim iFoundStart As Long
    Dim iFoundLength As Long
    Dim iCompareType As Integer
    Dim i As Long
    Dim bPrevSearched As Boolean
    Dim iStartPos As Long
    Dim sText As String
    If chkMatchCase.Value = vbChecked Then
        iCompareType = vbBinaryCompare
    Else
        iCompareType = vbTextCompare
    End If
   
    With rtText
        iFoundLength = Len(cbFind.Text)
        If .SelStart = 0 Then .SelStart = 1
        sText = Replace(.Text, vbNewLine, vbCr)
        If optDown.Value Then
            '.Find
            iStartPos = .SelStart + .SelLength
            iFoundStart = InStr(iStartPos, sText, cbFind.Text, iCompareType)
        Else
            iFoundStart = InStrRev(sText, cbFind.Text, .SelStart, iCompareType)
        End If
       
        If iFoundStart > 0 Then
            .SelStart = iFoundStart - 1
            .SelLength = iFoundLength
        Else
            'Beep
            MsgBox "Cannot find """ & cbFind.Text & """", vbInformation
        End If
    End With
   
    With cbFind
        For i = 0 To .ListCount - 1
            If .List(i) = .Text Then
                bPrevSearched = True
                Exit For
            End If
        Next i
        If Not bPrevSearched Then
            .AddItem .Text
        End If
    End With
   
End Sub


Private Sub Form_Activate()

    cbFind.SetFocus
   
End Sub


Private Sub Form_Load()
   
    SetFormIcon Me
   
End Sub


Public Sub setText(RichText As RichTextBox)

    Set rtText = RichText
   
End Sub

0
 
aaronthamAuthor Commented:
Hi JR2003,

 which sub is for Listview?? or what variable should i use for listview. Pls advice.
0
 
JR2003Commented:
Private Sub mnuEditFind_Click()

    Dim i As Long
    Dim j As Long
    Dim iStoreLine As Long
    Static sSearchString As String
   
    sSearchString = InputBox("Search for Text:", "Find Text", sSearchString)
    If sSearchString = "" Then GoTo SkipTrap
    Screen.MousePointer = vbHourglass
    With lvQueryResults
        With .ListItems
            For i = .Count To 1 Step -1
                With .Item(i)
                    If InStr(1, .Text, sSearchString, vbTextCompare) > 0 Then
                        iStoreLine = i
                        .ForeColor = RGB(200, 0, 200)
                        '.Bold = True
                    ElseIf .ForeColor <> vbWindowText Then
                        '.Bold = False
                        .ForeColor = vbWindowText
                    End If
                    For j = .ListSubItems.Count To 1 Step -1
                        With .ListSubItems(j)
                            If InStr(1, .Text, sSearchString, vbTextCompare) > 0 Then
                                iStoreLine = i
                                .ForeColor = RGB(0, 0, 200)
                                '.Bold = True
                            ElseIf .ForeColor <> vbWindowText Then
                                .ForeColor = vbWindowText
                                '.Bold = False
                            End If
                        End With
                    Next j
                End With
            Next i
        End With
       
        If iStoreLine > 0 Then
            .ListItems(iStoreLine).EnsureVisible
            .ListItems(iStoreLine).Selected = True
            .Refresh
        End If
       
    End With
   

End Sub
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
aaronthamAuthor Commented:
Hi JR2003,

 i quite new in vb. pls help. let say i have a listview1 and already pupolate with data look like below

Brand         Model
Acer           Aspire 5500
Acer           Aspire 4500
Acer           Aspire 4400

when i key in 500in the search text box then it show like below

Brand         Model
Acer           Aspire 5500
Acer           Aspire 4500

Thank You


0
 
JR2003Commented:
The only way you can prevent items (lines) in a listview from being show is to actually remove them from the listview.
The problem you might have is that you want to go back to the full view with all the lines.

Option Explicit


Private Sub Form_Load()
    Dim i As Long
    With lvQueryResults
        .View = lvwReport
        With .ColumnHeaders
            .Add , , "Make"
            .Add , , "Model"
        End With
   
        Dim itmX As ListItem
        For i = 1 To 10
            Set itmX = .ListItems.Add(, , "Acer")
            itmX.ListSubItems.Add , , "Aspire 550" & i
        Next i
    End With
End Sub

Private Sub mnuEditFind_Click()

    Dim i As Long
    Dim j As Long
    Dim bStringFound As Boolean
    Static sSearchString As String
   
    sSearchString = InputBox("Search for Text:", "Find Text", sSearchString)
    If sSearchString = "" Then GoTo SkipTrap
    Screen.MousePointer = vbHourglass
    With lvQueryResults
        With .ListItems
            For i = .Count To 1 Step -1
                bStringFound = False 'Initialise
                With .Item(i)
                    If InStr(1, .Text, sSearchString, vbTextCompare) > 0 Then
                        bStringFound = True
                    End If
                    For j = .ListSubItems.Count To 1 Step -1
                        With .ListSubItems(j)
                            If InStr(1, .Text, sSearchString, vbTextCompare) > 0 Then
                                bStringFound = True
                            End If
                        End With
                    Next j
                End With
                If Not bStringFound Then
                    .Remove (i)
                End If
            Next i
        End With
       
    End With
SkipTrap:
    Screen.MousePointer = vbDefault


End Sub



0
 
aaronthamAuthor Commented:
Hi,

The listview should be in what name??
0
 
JR2003Commented:
lvQueryResults
0
 
aaronthamAuthor Commented:
Hi JR2003,

your code work but it not what i want.

i want the code can work in a text change event like below.

When i key in 4500 in the search box it show below.

Brand         Model
Acer           Aspire 4500

But when i Backspace three time in the search box twince to 4 then it show like below

Brand         Model
Acer           Aspire 4500
Acer           Aspire 4400

If i backspace again it nothing in the search textbox then it show back the original data in the listview like below .
Brand         Model
Acer           Aspire 5500
Acer           Aspire 4500
Acer           Aspire 4400

Pls advice.

Thank You
0
 
JR2003Commented:
The only way you can hide rows in a listview is row remove them.
If you paste your code in this thread I can probably work something out for you.
0
 
RomModCommented:
Question closed - 300 points refunded.

Best regards,
RomMod
Experts Exchange
Community Support Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now