LISTVIEW FIND

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

LISTVIEW FIND

Post by sal21 »

I need to find if in first column and second column is present a value.

I need to loop the item in listview or?

for example for first column Myvar="test"
for example for first column Myvar1="test1"

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: LISTVIEW FIND

Post by HansV »

For example:

Code: Select all

    Dim MyVar As String
    Dim MyVar1 As String
    Dim itm As MSComctlLib.ListItem
    Dim idx As Long
    MyVar = "test"
    MyVar1 = "test1"
    idx = 1
    With Me.ListView1
        Do
            Set itm = .FindItem(sz:=MyVar, Index:=idx)
            If itm Is Nothing Then Exit Do
            If itm.ListSubItems(1).Text = MyVar1 Then
                f = True
                Exit Do
            End If
            idx = itm.Index + 1
        Loop Until idx > .ListItems.Count
    End With
    If itm Is Nothing Then
        MsgBox "Not found!"
    Else
        MsgBox "Found in item #" & itm.Index
    End If
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: LISTVIEW FIND

Post by sal21 »

HansV wrote:
20 Mar 2022, 10:28
For example:

Code: Select all

    Dim MyVar As String
    Dim MyVar1 As String
    Dim itm As MSComctlLib.ListItem
    Dim idx As Long
    MyVar = "test"
    MyVar1 = "test1"
    idx = 1
    With Me.ListView1
        Do
            Set itm = .FindItem(sz:=MyVar, Index:=idx)
            If itm Is Nothing Then Exit Do
            If itm.ListSubItems(1).Text = MyVar1 Then
                f = True
                Exit Do
            End If
            idx = itm.Index + 1
        Loop Until idx > .ListItems.Count
    End With
    If itm Is Nothing Then
        MsgBox "Not found!"
    Else
        MsgBox "Found in item #" & itm.Index
    End If
:clapping:

OPS...
dim f as Boolean...

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: LISTVIEW FIND

Post by HansV »

Thanks, It should be

Code: Select all

Private Sub CommandButton2_Click()
    Dim MyVar As String
    Dim MyVar1 As String
    Dim itm As MSComctlLib.ListItem
    Dim f As Boolean
    Dim idx As Long
    MyVar = "That"
    MyVar1 = "test1"
    idx = 1
    With Me.ListView1
        Do
            Set itm = .FindItem(sz:=MyVar, Index:=idx)
            If itm Is Nothing Then Exit Do
            If itm.ListSubItems(1).Text = MyVar1 Then
                f = True
                Exit Do
            End If
            idx = itm.Index + 1
        Loop Until idx > .ListItems.Count
    End With
    If f = False Then
        MsgBox "Not found!"
    Else
        MsgBox "Found in item #" & itm.Index
    End If
End Sub
Best wishes,
Hans