Results 1 to 8 of 8

Thread: [RESOLVED] Category - List box

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2024
    Posts
    3

    Resolved [RESOLVED] Category - List box

    hello guys
    I just joined this forum and I am a beginner
    A question is bothering me
    I am calling a list from database and want to list it in categories
    This is what I did

    Name:  List.png
Views: 129
Size:  9.1 KB

    And that's not what I want
    The list should look like this, a category with subcategories like this

    Web
    Sara
    Melika
    Trump
    Arnold
    Joo
    Eli
    Python
    Robert
    Deived
    Lora
    Sam
    VB6
    Vike
    Sinor
    . . .

    Please help me how to sort this list like this
    I put the code, see where the problem is ? please
    Attached Files Attached Files

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,717

    Re: Category - List box

    Code:
    Private Sub Command1_Click()
        Dim i As Integer
    
            For i = 0 To Rs.RecordCount - 1
            List1.AddItem Rs!Class
            List1.AddItem Rs!Name
            Rs.MoveNext
            Next i
    
    End Sub
    the AddItem-Calls are your Problem.

    That said: I took a look at the Database.
    Normalize your Database first. Learn about Primary and Foreign Keys.
    Fork your Field "Class" to its separate table.
    Fork the Field "Name" to its separate table.... AND FOR ALL THAT IS HOLY: DON'T USE "NAME" AS FIELD-NAME
    add a Foreign Key in "Name" pointing to the Primary Key of "Class"

    Next: ListBox is probably the wrong Control.
    First thought would be to use a TreeView

    And that's just on first look
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,717

    Re: Category - List box

    Though, since OP admitted to being a beginner, here how to solve the "inital" problem

    This is all untested, since i don't have vb6 available

    1) Change sour SQL-Statement in Form_Load to:
    Code:
    select Class, Name from Customer ORDER BY Class, Name
    2) Change Command1_Click to
    Code:
    Private Sub Command1_Click()
    Dim s As String
        s = ""
        Rs.MoveFirst
        Do While Not Rs.EOF
            If s<>Rs!Class Then 
                s=Rs!Class
                List1.AddItem s
            End If    
            List1.AddItem Rs!Name
            Rs.MoveNext
        Loop
    End Sub
    Untested.

    Note: The ListBox MUST never be set to Sorted=True
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2024
    Posts
    3

    Re: Category - List box

    Quote Originally Posted by Zvoni View Post
    Though, since OP admitted to being a beginner, here how to solve the "inital" problem

    Code:
    Private Sub Command1_Click()
    Dim s As String
        s = ""
        Rs.MoveFirst
        Do While Not Rs.EOF
            If s<>Rs!Class Then 
                s=Rs!Class
                List1.AddItem s
            End If    
            List1.AddItem Rs!Name
            Rs.MoveNext
        Loop
    End Sub
    Note: The ListBox MUST never be set to Sorted=True
    Excellent thank you Zvoni
    He answered, you solved my problem
    Just one more question, how can I set a limit for the list, for example, if the number of items in the first list box is more than 30 and the rest of the items in the first list are added to the second list box

    Name:  List2.png
Views: 88
Size:  8.5 KB
    Last edited by VBSara; Aug 13th, 2024 at 07:39 AM.

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,717

    Re: Category - List box

    What do you mean?
    you want to introduce a hard Limit of 30 entries to List 1, and from Entry 31 everything else is supposed to go to List 2?
    Code:
    'Needs a second ListBox "List2" on the Form
    Private Sub Command1_Click()
    Dim s As String
    Dim i As Long
    Dim l As ListBox
        s = ""
        Rs.MoveFirst
        i = 0
        Set l = List1
        Do While Not Rs.EOF
            i = i + 1 
            If i>30 And l Is List1 Then Set l = List2  'NOT SURE ABOUT THE And-Part
            If s<>Rs!Class Then 
                s=Rs!Class
                l.AddItem s
            End If    
            l.AddItem Rs!Name
            Rs.MoveNext
        Loop
    End Sub
    Untested
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2024
    Posts
    3

    Re: Category - List box

    Quote Originally Posted by Zvoni View Post
    What do you mean?
    you want to introduce a hard Limit of 30 entries to List 1, and from Entry 31 everything else is supposed to go to List 2?
    Code:
    'Needs a second ListBox "List2" on the Form
    Private Sub Command1_Click()
    Dim s As String
    Dim i As Long
    Dim l As ListBox
        s = ""
        Rs.MoveFirst
        i = 0
        Set l = List1
        Do While Not Rs.EOF
            i = i + 1 
            If i>30 And l Is List1 Then Set l = List2  'NOT SURE ABOUT THE And-Part
            If s<>Rs!Class Then 
                s=Rs!Class
                l.AddItem s
            End If    
            l.AddItem Rs!Name
            Rs.MoveNext
        Loop
    End Sub
    Untested
    Yes, that's what I meant. very good thank you

  7. #7
    Fanatic Member
    Join Date
    Feb 2019
    Posts
    801

    Re: [RESOLVED] Category - List box

    Please post in VB.Net forum in the future. You posted in VB6 and Earlier forum(Classic VB), which is for VB released in 1998 or before. The two languages are not compatible even though they share the same name, and Microsoft dropped the .Net part, but it's still based on .Net library.

  8. #8
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,717

    Re: [RESOLVED] Category - List box

    Quote Originally Posted by qvb6 View Post
    Please post in VB.Net forum in the future. You posted in VB6 and Earlier forum(Classic VB), which is for VB released in 1998 or before. The two languages are not compatible even though they share the same name, and Microsoft dropped the .Net part, but it's still based on .Net library.
    Or next time you look into the attachment.
    then you would know it‘s about vb6, and we could be spared such nonsense posts
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

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