-
May 13th, 2015, 11:17 AM
#1
Thread Starter
Junior Member
[RESOLVED] Sorting multiple text boxes by ascending/Descending order (VB6)
I need help with sorting multiple text boxes by ascending/Descending order.
I have a total of three text boxes for input.
1. points (txtvalue.text)
2. Student ID (txtStudentID.text)
3. Student Name (txtStudentName.text)
I need to sort all by points. I'm able to sort only by points but I can't figure out how to add the rest to the sort. Here is what I have so far:
----------------------------------------------------------
Private Sub CmdAscending_Click()
List1.Clear
Dim a(9)
a(0) = CInt(Txtvalue1.Text)
a(1) = CInt(Txtvalue2.Text)
a(2) = CInt(Txtvalue3.Text)
a(3) = CInt(Txtvalue4.Text)
a(4) = CInt(Txtvalue5.Text)
a(5) = CInt(Txtvalue6.Text)
a(6) = CInt(Txtvalue7.Text)
a(7) = CInt(Txtvalue8.Text)
a(8) = CInt(Txtvalue9.Text)
a(9) = CInt(Txtvalue10.Text)
Sort a, True
For i = 0 To 9
List1.AddItem a(i)
Next i
End Sub
----------------------------------------------------------
Sub Sort(ByRef Arr() As Variant, Optional ByVal bAsc As Boolean = True)
Dim Done As Boolean
Done = False
Do While Done = False
Done = True
For i = 0 To UBound(Arr) - 1
If (Arr(i) > Arr(i + 1) And bAsc) Or (Arr(i) < Arr(i + 1) And Not bAsc) Then Swap Arr(i), Arr(i + 1): Done = False
Next i
Loop
End Sub
----------------------------------------------------------
Sub Swap(ByRef a As Variant, ByRef b As Variant)
Dim tmp As Variant
tmp = a
a = b
b = tmp
End Sub
----------------------------------------------------------
Thanks in advance
-
May 13th, 2015, 11:37 AM
#2
Re: Sorting multiple text boxes by ascending/Descending order (VB6)
I'm assuming you want to sort all of the data based upon the points, so you'd end up with something like this:
ID Name Points
23 John 89
51 Jane 76
27 Fred 59
Correct?
If so, use a multi-dimensional array versus your 1D array. Many links out there to show procedure.
-
May 13th, 2015, 11:47 AM
#3
Re: Sorting multiple text boxes by ascending/Descending order (VB6)
A 1D array can still be used, but the array would not contain the points, rather it would contain a reference to the record. In other words, a lookup table.
-
May 13th, 2015, 12:23 PM
#4
Re: Sorting multiple text boxes by ascending/Descending order (VB6)
True....and probably easier to implement.
-
May 13th, 2015, 12:26 PM
#5
Re: Sorting multiple text boxes by ascending/Descending order (VB6)
If you must use textboxes then instead of this:
Code:
Private Sub CmdAscending_Click()
List1.Clear
Dim a(9)
a(0) = CInt(Txtvalue1.Text)
a(1) = CInt(Txtvalue2.Text)
a(2) = CInt(Txtvalue3.Text)
a(3) = CInt(Txtvalue4.Text)
a(4) = CInt(Txtvalue5.Text)
a(5) = CInt(Txtvalue6.Text)
a(6) = CInt(Txtvalue7.Text)
a(7) = CInt(Txtvalue8.Text)
a(8) = CInt(Txtvalue9.Text)
a(9) = CInt(Txtvalue10.Text)
Sort a, True
For i = 0 To 9
List1.AddItem a(i)
Next i
End Sub
Do this....
Instead of having 10 textboxes each with a different name:
Txtvalue1
Txtvalue2
Txtvalue3
'
'
'
Txtvalue10
Make your textboxes a control array so you have this:
Txtvalue(0)
Txtvalue(1)
Txtvalue(2)
'
'
'
Txtvalue(9)
Then change your code to this:
Code:
Private Sub CmdAscending_Click()
List1.Clear
Dim a(9)
For i = 0 to 9
a(i) = CInt(Txtvalue(i).Text)
Next i
Sort a, True
For i = 0 To 9
List1.AddItem a(i)
Next i
End Sub
Last edited by jmsrickland; May 13th, 2015 at 12:30 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 13th, 2015, 12:28 PM
#6
Thread Starter
Junior Member
Re: Sorting multiple text boxes by ascending/Descending order (VB6)
I will google for more info. thanks.
-
May 13th, 2015, 12:30 PM
#7
Thread Starter
Junior Member
Re: Sorting multiple text boxes by ascending/Descending order (VB6)
thanks. that makes it much cleaner. I will do!
-
May 13th, 2015, 02:50 PM
#8
Re: [RESOLVED] Sorting multiple text boxes by ascending/Descending order (VB6)
If your going to use CInt() you better check to make sure the text in the textbox is numeric and that it has a number in it or else I would suggest using Val() if textbox is empty it will return 0 instead of error.
-
May 13th, 2015, 03:15 PM
#9
Re: [RESOLVED] Sorting multiple text boxes by ascending/Descending order (VB6)
Did you actually get it solved, and able to display all of the students' information in the proper sequence sorting by points?
If so, why not post your solution so that others who visit and may have similar questions can see how you did it.
If not...why did you mark it as resolved?
-
May 13th, 2015, 03:27 PM
#10
Thread Starter
Junior Member
Re: [RESOLVED] Sorting multiple text boxes by ascending/Descending order (VB6)
Not solved it yet but I will be starting from scratch to create a multi-dimensional array that will do the job. I will post the final piece once I'm done. Thanks all for your tips on getting me going. Cheers!
-
May 14th, 2015, 06:14 AM
#11
Re: [RESOLVED] Sorting multiple text boxes by ascending/Descending order (VB6)
Attached is a SAMPLE of ONE WAY of possibly doing what you want...not the BEST WAY probably, but it shows how using a UDT and a one-dimensional array. It is also not probably the optimal way of using a UDT, as I don't use them. I just put this together for practice.
Attachment 126629
The code is below so others can suggest improvements...better ways of doing it.
(Uses two command buttons, three text boxes and a list box.)
Code:
Option Explicit
Dim recnumber As Integer
Private Type EmployeeRecord
strName As String
iId As Integer
iPoints As Integer
End Type
Dim aRecords(100) As EmployeeRecord
Dim a(100) As Integer
Private Sub Command1_Click()
recnumber = recnumber + 1
aRecords(recnumber).strName = Text1(0).Text
aRecords(recnumber).iId = CInt(Text1(1).Text)
aRecords(recnumber).iPoints = CInt(Text1(2).Text)
Dim x As Integer
For x = 0 To 2
Text1(x).Text = ""
Next x
' MsgBox aRecords(1).iId
a(recnumber) = aRecords(recnumber).iPoints
End Sub
Private Sub Command2_Click()
Sort a(), True
Dim x As Integer
For x = 1 To 100
If a(x) > 0 Then
Dim y As Integer
For y = 0 To 100
If a(x) = aRecords(y).iPoints Then
List1.AddItem (aRecords(y).iId & " " & aRecords(y).strName & " " & aRecords(y).iPoints)
End If
Next y
End If
Next x
End Sub
Sub Sort(ByRef Arr() As Integer, Optional ByVal bAsc As Boolean = True)
Dim Done As Boolean
Done = False
Do While Done = False
Done = True
Dim i As Integer
For i = 0 To UBound(Arr) - 1
If (Arr(i) > Arr(i + 1) And bAsc) Or (Arr(i) < Arr(i + 1) And Not bAsc) Then Swap Arr(i), Arr(i + 1): Done = False
Next i
Loop
End Sub
Sub Swap(ByRef a As Variant, ByRef b As Variant)
Dim tmp As Variant
tmp = a
a = b
b = tmp
End Sub
-
May 14th, 2015, 01:44 PM
#12
Re: [RESOLVED] Sorting multiple text boxes by ascending/Descending order (VB6)
Since you want to sort on the points then you could try this approach which eliminates the Sub Sort.
Make an array of textboxes, txtPoints(0) thru txtPoints(9). Put the points in the textbox and use the Tag property to hold the StudentName and Student ID separated by a comma so you wind up with something like this:
txtPoints(0).Text = "23"; txtPoints(0).Tag = "John, 89"
txtPoints(1).Text = "51"; txtPoints(1).Tag = Jane, 76"
txtPoints(2).Text = "27"; txtPoints(2).Tag = "Fred, 59"
txtPoints(3).Text = "15"; txtPoints(3).Tag = "Jack, 39"
txtPoints(4).Text = "43"; txtPoints(3).Tag = "Mary, 33"
Private Sub CmdAscending_Click()
List1.Sorted = True
List1.Clear
For i = 0 To 9
List1.AddItem Right("000" & txtPoints(i).Text, 3) & "," & txtPoints(i).Tag
Next i
End Sub
After running the above List1 will now look like this:
"015,Jack,39"
"023,John,89"
"027,Fred,59"
"043,Mary,33"
"051,Jane,76"
Now you have the points sorted and the name and ID all on a single record
Later when you need to process them just take each List1 element and split on the comma to extract out the points, name and ID
For ascending order get the List1 items from 0 to List1.ListCount -1 and for descending order get the items from List1.ListCount-1 to 0
Last edited by jmsrickland; May 14th, 2015 at 01:47 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
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
|