-
Aug 13th, 2024, 05:28 AM
#1
Thread Starter
New Member
-
Aug 13th, 2024, 06:08 AM
#2
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
-
Aug 13th, 2024, 06:35 AM
#3
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
-
Aug 13th, 2024, 07:35 AM
#4
Thread Starter
New Member
Re: Category - List box
Originally Posted by Zvoni
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
Last edited by VBSara; Aug 13th, 2024 at 07:39 AM.
-
Aug 13th, 2024, 07:43 AM
#5
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
-
Aug 13th, 2024, 07:50 AM
#6
Thread Starter
New Member
Re: Category - List box
Originally Posted by Zvoni
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
-
Aug 13th, 2024, 09:30 AM
#7
Fanatic Member
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.
-
Aug 13th, 2024, 09:54 AM
#8
Re: [RESOLVED] Category - List box
Originally Posted by qvb6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|