Results 1 to 14 of 14

Thread: [RESOLVED] Checking listview subitem(1) for certain text

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Resolved [RESOLVED] Checking listview subitem(1) for certain text

    i have tried everything and cannot get this to work. what i am looking to do is check all the items in the listview (subitem(1)) to see if a certain text string exists. if the string exists, then to not allow the closing of the app.

    this is what i have, any idea?

    i am getting argument not optional on highlighted.
    VB Code:
    1. Private Sub mnuFileMenuExit_Click()
    2.     If lvwUserPunchedOut.ListItems[hl].Item[/hl] = "N/A - New Employee" Then
    3.         MsgBox "You cannot close " & Me.Caption & vbNewLine & _
    4.                "because some users have never punched in or out yet.", _
    5.                vbExclamation, "New Users Present."
    6.         Exit Sub
    7.     Else
    8.         Unload Me
    9.     End If
    10. End Sub

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Checking listview subitem(1) for certain text

    For the first listitem...
    VB Code:
    1. lvwUserPunchedOut.ListItems(1).SubItems(1)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Checking listview subitem(1) for certain text

    Quote Originally Posted by RobDog888
    For the first listitem...
    VB Code:
    1. lvwUserPunchedOut.ListItems(1).SubItems(1)
    updated post #1 and got something else. hehe
    now what if i want to check all rows in the listview? i would loop through?

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Checking listview subitem(1) for certain text

    ok, this is what i have so far and its working, but is there a better way to do it?
    VB Code:
    1. Private Sub mnuFileMenuExit_Click()
    2.     m_lngN = 0
    3.     If lvwUserPunchedOut.ListItems(m_lngN + 1).SubItems(m_lngN + 1) = "N/A - New Employee" Then
    4.         MsgBox "You cannot close " & Me.Caption & vbNewLine & _
    5.                "because some users have never punched in or out yet.", _
    6.                vbExclamation, "New Users Present."
    7.         Exit Sub
    8.     Else
    9.         Unload Me
    10.     End If
    11. End Sub

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Checking listview subitem(1) for certain text

    Yes, you can use the .FindItem passing the lvwSubItem constant
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Checking listview subitem(1) for certain text

    Quote Originally Posted by RobDog888
    Yes, you can use the .FindItem passing the lvwSubItem constant
    .FindItem only allows you to pass one criteria right? would this go through all the rows to check each one or would i still have to loop through each row of the information?

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Checking listview subitem(1) for certain text

    You don't need to loop with .FindItem ...

    But if I'm not mistaken, it does not work properly on subitems on one of the match values; either whole or partial, forgot on which it fails.

    Try FindItem first... if it does not return a listitem object from a subitem match when it should then you'll have to resort to looping.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Checking listview subitem(1) for certain text

    Quote Originally Posted by leinad31
    You don't need to loop with .FindItem ...

    But if I'm not mistaken, it does not work properly on subitems on one of the match values; either whole or partial, forgot on which it fails.

    Try FindItem first... if it does not return a listitem object from a subitem match when it should then you'll have to resort to looping.
    am i looping in post #4? not sure what im doing there haha

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Checking listview subitem(1) for certain text

    VB Code:
    1. Dim cntA As Long
    2. Dim cntB As Long
    3. Dim bFound As Boolean
    4.  
    5. bFound = False
    6. For cntA = 1 To lvwUserPunchedOut.ListItems.Count
    7.    For cntB = 1 To lvwUserPunchedOut.ColumnHeaders.Count -1  'since .ListSubItems might not be initialized
    8.       If StrComp(lvwUserPunchedOut.ListItems(cntA).SubItems(cntB), "N/A - New Employee", vbTextCompare) = 0 Then
    9.          bFound = True
    10.          Exit For
    11.       End If
    12.    Next
    13.    If bFound = True Then Exit For
    14. Next

    If "N/A - New Employee" is always written to a subitem index, or a particular column, then you don't need the nested loop for subitem iteration, you'll just loop through the listitems.

  10. #10
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Checking listview subitem(1) for certain text

    Quote Originally Posted by BrailleSchool
    ok, this is what i have so far and its working, but is there a better way to do it?
    VB Code:
    1. Private Sub mnuFileMenuExit_Click()
    2.     m_lngN = 0
    3.     If lvwUserPunchedOut.ListItems(m_lngN + 1).SubItems(m_lngN + 1) = "N/A - New Employee" Then
    4.         MsgBox "You cannot close " & Me.Caption & vbNewLine & _
    5.                "because some users have never punched in or out yet.", _
    6.                vbExclamation, "New Users Present."
    7.         Exit Sub
    8.     Else
    9.         Unload Me
    10.     End If
    11. End Sub

    I don't see a loop anywhere...

    Looks like you're just checking the first sub item of the first list item.

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Checking listview subitem(1) for certain text

    Quote Originally Posted by leinad31
    You don't need to loop with .FindItem ...

    But if I'm not mistaken, it does not work properly on subitems on one of the match values; either whole or partial, forgot on which it fails.

    Try FindItem first... if it does not return a listitem object from a subitem match when it should then you'll have to resort to looping.
    I have not heard or experienced this. Passing the lvwSubitem with the lvwPartial or lvwWholeWord.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Checking listview subitem(1) for certain text

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim lstFound As ListItem
    3.    
    4.    ListView1.ListItems(1).Selected = True
    5.  
    6. '============
    7.    Set lstFound = ListView1.FindItem("Text 3", lvwSubItem, , lvwPartial)
    8.    If lstFound Is Nothing Then
    9.       Debug.Print ".FindItem(""Text 3"", lvwSubItem, , lvwPartial) Is Nothing"
    10.    Else
    11.       Debug.Print ".FindItem(""Text 3"", lvwSubItem, , lvwPartial) SUCCESS"
    12.       lstFound.Selected = True
    13.       lstFound.EnsureVisible
    14.    End If
    15. '============
    16.    Set lstFound = ListView1.FindItem("Text 3", lvwSubItem, , lvwWhole)
    17.    If lstFound Is Nothing Then
    18.       Debug.Print ".FindItem(""Text 3"", lvwSubItem, , lvwWhole) Is Nothing"
    19.    Else
    20.       Debug.Print ".FindItem(""Text 3"", lvwSubItem, , lvwWhole) SUCCESS"
    21.       lstFound.Selected = True
    22.       lstFound.EnsureVisible
    23. End If
    24. '============
    25.    Set lstFound = ListView1.FindItem("Text 3 Sub 2", lvwSubItem, , lvwPartial)
    26.    If lstFound Is Nothing Then
    27.       Debug.Print ".FindItem(""Text 3 Sub 2"", lvwSubItem, , lvwPartial) Is Nothing"
    28.    Else
    29.       Debug.Print ".FindItem(""Text 3 Sub 2"", lvwSubItem, , lvwPartial) SUCCESS"
    30.       lstFound.Selected = True
    31.       lstFound.EnsureVisible
    32.    End If
    33. '============
    34.    Set lstFound = ListView1.FindItem("Text 3 Sub 2", lvwSubItem, , lvwWhole)
    35.    If lstFound Is Nothing Then
    36.       Debug.Print ".FindItem(""Text 3 Sub 2"", lvwSubItem, , lvwWhole) Is Nothing"
    37.    Else
    38.       Debug.Print ".FindItem(""Text 3 Sub 2"", lvwSubItem, , lvwWhole) SUCCESS"
    39.       lstFound.Selected = True
    40.       lstFound.EnsureVisible
    41.    End If
    42.    
    43. End Sub
    44.  
    45. Private Sub Form_Load()
    46. Dim lstNew As ListItem
    47. Dim iA As Integer
    48. Dim iB As Integer
    49.  
    50.    With ListView1
    51.       .FullRowSelect = True
    52.       .HideColumnHeaders = False
    53.       .LabelEdit = lvwManual
    54.       .MultiSelect = False
    55.       .View = lvwReport
    56.      
    57.       .ColumnHeaders.Clear
    58.       .ColumnHeaders.Add , , "Text"
    59.       .ColumnHeaders.Add , , "SubItem(1)"
    60.       .ColumnHeaders.Add , , "SubItem(2)"
    61.       .ColumnHeaders.Add , , "SubItem(3)"
    62.       .ColumnHeaders.Add , , "SubItem(4)"
    63.       .ColumnHeaders.Add , , "SubItem(5)"
    64.       .ColumnHeaders.Add , , "SubItem(6)"
    65.      
    66.       .ListItems.Clear
    67.       For iA = 1 To 10
    68.          Set lstNew = .ListItems.Add
    69.          lstNew.Text = "Text " & iA
    70.          
    71.          For iB = 1 To .ColumnHeaders.Count - 1
    72.             .ListItems(iA).SubItems(iB) = lstNew.Text & " Sub " & iB
    73.          Next
    74.       Next
    75.    End With
    76.    
    77.    Set lstNew = Nothing
    78. End Sub

    Please verify my code and test result

    .FindItem("Text 3", lvwSubItem, , lvwPartial) Is Nothing
    .FindItem("Text 3", lvwSubItem, , lvwWhole) Is Nothing
    .FindItem("Text 3 Sub 2", lvwSubItem, , lvwPartial) SUCCESS
    .FindItem("Text 3 Sub 2", lvwSubItem, , lvwWhole) SUCCESS

    My result is that finditem on subitem, only works when the search string supplied matches the full text in the subitem regardless of lvwPartial or lvwWhole. A truly partial search does not work.

    Maybe your using an API solution? Does the API solution exhibit the same problem or works as intended?

  13. #13
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Checking listview subitem(1) for certain text

    Braille, since your seem to be searching for the full text you can use finditem

    VB Code:
    1. Private Sub mnuFileMenuExit_Click()
    2. Dim lstFound as ListItem
    3.  
    4.     Set lstFound = lvwUserPunchedOut.FindItem("N/A - New Employee", lvwSubItem, , lvwWhole)
    5.     If Not (lstFound Is Nothing) Then
    6.         MsgBox "You cannot close " & Me.Caption & vbNewLine & _
    7.                "because some users have never punched in or out yet.", _
    8.                vbExclamation, "New Users Present."
    9.         Exit Sub
    10.     Else
    11.         Unload Me
    12.     End If
    13. End Sub

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Checking listview subitem(1) for certain text

    thanks for all the help guys. everything is working so far and not finding any bugs to squash.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width