Results 1 to 16 of 16

Thread: search list1 column2

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2016
    Posts
    210

    search list1 column2

    I need code, there are several articles that are in column 2,
    search for list1 column 2 and select the item from list1


    code


    Code:
    
    form load
    
    list1.clear
    
    list1.additem 1,a
    list1.additem 2,b
    list1.additem 3,c
    list1.additem 4,d
    
    end sub

    Thank you

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: search list1 column2

    Cited from "Guide to Getting Help" for these forums:

    We will help with specific questions, but do not post something expecting people to just cough up code. At least make an attempt on your own.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2016
    Posts
    210

    Re: search list1 column2

    thank you
    in principle is a small programming code I do not ask for much, if you do not throw code I will not be able to ask any more questions thanks

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: search list1 column2

    Check the help for the AddItem method.
    The first parameter is the text, the second parameter is for the position.
    There is no second column with data..
    Unless you are referring to the ListView control, which has different methods and properties

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2016
    Posts
    210

    Re: search list1 column2

    hello
    I need to search for column 2 and make it selected



    Code:
    Option Explicit
     
    Private Declare Function SendMessage Lib "user32" _
       Alias "SendMessageA" _
       (ByVal hwnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        lParam As Any) As Long
     
    Private Const LB_SETTABSTOPS = &H192
     
     
    Private Sub Command1_Click()
        List1.AddItem "Column1" & vbTab & "Column2" & vbTab & "Column3"
        List1.AddItem "Column1" & vbTab & "Column2" & vbTab & "Column3"
        List1.AddItem "Column1" & vbTab & "Column2" & vbTab & "Column3"
        List1.AddItem "Column1" & vbTab & "Column2" & vbTab & "Column3"
    End Sub
     
    Private Sub Form_Load()
     
      'set up the tabstops in the list boxes
       ReDim TabStop(0 To 2) As Long
      
      'assign some values to the tabs for the second third and fourth
      'column (the first is flush against the listbox edge)
       TabStop(0) = 60
       TabStop(1) = 120
       TabStop(2) = 180
      
      'clear then set the tabs
       Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
       Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 3, TabStop(0))
       List1.Refresh
     
    End Sub

    Thank you

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,144

    Re: search list1 column2

    For what you are attempting, you are using the 'wrong' control. As possibly suggested above, check out the ListView in MSDN. Will be so much easier than messing around with vbTab searches.
    Sam I am (as well as Confused at times).

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: search list1 column2

    Actually, ontro, I do use the ListView frequently, but I also frequently use the ListBox in a way quite similar to what you're suggesting.

    But, to answer your question, you just need to read your ListBox items, then use Mid$ to strip them to the vbTab you're interested in, and then see if it's what you want.

    It's not difficult, but I still don't see an attempt by you to do it. Yes, you have successfully loaded a ListBox for us, but now it's time to create a loop (For i = 0 To List1.ListCount - 1: some Mid$ work: Next) to do your search.

    Good Luck
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2016
    Posts
    210

    Re: search list1 column2

    hello
    I need to search for column 2 and make it selected



    Code:
    Option Explicit
     
    Private Declare Function SendMessage Lib "user32" _
       Alias "SendMessageA" _
       (ByVal hwnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        lParam As Any) As Long
     
    Private Const LB_SETTABSTOPS = &H192
    
    Private Sub Command1_Click()
        List1.AddItem "Column1" & vbTab & "Column1" & vbTab & "Column1"
        List1.AddItem "Column2" & vbTab & "Column2" & vbTab & "Column2"
        List1.AddItem "Column3" & vbTab & "Column3" & vbTab & "Column3"
        List1.AddItem "Column4" & vbTab & "Column4" & vbTab & "Column4"
    End Sub
    
    Private Sub Command2_Click()
    'search
    Dim i As Integer
    For i = 0 To List1.ListCount - 1
    List1.Selected(i) = True
    List1.List(i) = Text1.Text
    Next
    End Sub
    
    Private Sub Form_Load()
    'set up the tabstops in the list boxes
       ReDim TabStop(0 To 2) As Long
      
      'assign some values to the tabs for the second third and fourth
      'column (the first is flush against the listbox edge)
       TabStop(0) = 60
       TabStop(1) = 120
       TabStop(2) = 180
      
      'clear then set the tabs
       Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
       Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 3, TabStop(0))
       List1.Refresh
    End Sub
    Thank you

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,144

    Re: search list1 column2

    We KNOW that is what you want to do...Elroy told you HOW. DO IT!
    Sam I am (as well as Confused at times).

  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,144

    Re: search list1 column2

    Put a listbox and a textbox on a form...

    put this code in your form

    Code:
    Option Explicit
    
    
    Private Sub Form_Load()
        List1.AddItem ("HELLO" & vbTab & "WORLD" & vbTab & "2019")
        List1.AddItem ("GOODBYE" & vbTab & "CRUEL WORLD" & vbTab & "2021")
    End Sub
    
    
    Private Sub List1_Click()
        Dim i As Integer
        Dim j As Integer
        i = InStr(1, List1.Text, vbTab)
        j = InStrRev(List1.Text, vbTab)
        Text1.Text = Mid(List1.Text, i + 1, j - i - 1)
    End Sub
    You should be able to figure it out from that EXAMPLE
    Sam I am (as well as Confused at times).

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2016
    Posts
    210

    Re: search list1 column2

    from list1 to text1 fine, but it is from text1 to list1 and select to list1
    commandbutton2 search

    Code:
    Private Declare Function SendMessage Lib "user32" _
       Alias "SendMessageA" _
       (ByVal hwnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        lParam As Any) As Long
     
    Private Const LB_SETTABSTOPS = &H192
    
    Private Sub Command1_Click()
        List1.AddItem "1" & vbTab & "Column1" & vbTab & "Column1"
        List1.AddItem "2" & vbTab & "Column2" & vbTab & "Column2"
        List1.AddItem "3" & vbTab & "Column3" & vbTab & "Column3"
        List1.AddItem "4" & vbTab & "Column4" & vbTab & "Column4"
    End Sub
    
    Private Sub Command2_Click()
    'search
    Dim i As Integer
    For i = 0 To List1.ListCount - 1
    List1.Selected(i) = True
    List1.List(i) = Text1.Text
    Next
    End Sub
    
    Private Sub Form_Load()
    'set up the tabstops in the list boxes
       ReDim TabStop(0 To 2) As Long
      
      'assign some values to the tabs for the second third and fourth
      'column (the first is flush against the listbox edge)
       TabStop(0) = 60
       TabStop(1) = 120
       TabStop(2) = 180
      
      'clear then set the tabs
       Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
       Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 3, TabStop(0))
       List1.Refresh
    End Sub
    
    Private Sub List1_Click()
        Dim i As Integer
        Dim j As Integer
        i = InStr(1, List1.Text, vbTab)
        j = InStrRev(List1.Text, vbTab)
        Text1.Text = Mid(List1.Text, i + 1, j - i - 1)
    End Sub



    Thank you

  12. #12
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,144

    Re: search list1 column2

    SO confusing!

    Are you trying to replace an item in a listbox with something someone typed into a textbox?
    Sam I am (as well as Confused at times).

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Aug 2016
    Posts
    210

    Re: search list1 column2

    I try to search for text1 to selected list1 in column2

    Thank you

  14. #14
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: search list1 column2

    Well I think the first thign I would do is a simple Instr() test on the list items to see if the text exists at all in the item. Then if it does exist parse out the position you want to test and see if that part is the match if yes you found it and if not then keep looking at the rest of the items. A simple loop, an Instr() and possibly a Split() should do it.

    Or you could just Split() the item and check for a match without first testing the whole. Depending on the number of items the speed difference may or may not be noticeable.

    you keep talking about columns but that is just confusing the issue. You have no columns. You have a single string of text that is trying to appear as multiple columns but in reality, there is only one column in your list
    Last edited by DataMiser; Dec 23rd, 2021 at 08:40 AM.

  15. #15
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,144

    Re: search list1 column2

    Is this what you are looking for?

    1 Form, 1 Listbox, 1 Textbox, 1 Command Button

    Code:
    Option Explicit
    
    
    Private Sub Command1_Click()
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim midColItem As String
        For k = 0 To List1.ListCount - 1
            List1.ListIndex = k
            i = InStr(1, List1.Text, vbTab)
            j = InStrRev(List1.Text, vbTab)
            midColItem = Mid(List1.Text, i + 1, j - i - 1)
            If midColItem = Text1.Text Then
                List1.Selected(k) = True
                Exit Sub
            Else
                List1.ListIndex = -1
            End If
        Next k
    End Sub
    
    
    Private Sub Form_Load()
        List1.AddItem ("FORD" & vbTab & "TRUCK" & vbTab & "2019")
        List1.AddItem ("FORD" & vbTab & "CAR" & vbTab & "2019")
        List1.AddItem ("FORD" & vbTab & "SUV" & vbTab & "2019")
        List1.AddItem ("FORD" & vbTab & "HYBRID" & vbTab & "2019")
        Text1.Text = "SUV"
    End Sub
    Sam I am (as well as Confused at times).

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Aug 2016
    Posts
    210

    Re: search list1 column2

    Resolved

    ok thank you very much was this

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