BTW I just realized that you wanted the output to be in the sorted lists so here is the change to do that. If you want separate sorts you should be able to modify this code.
Code:
Private Sub cmdSort_Click()
Dim lngIndex As Long
Dim bSwapped As Boolean
Dim strName As String
Dim intMark As Integer
'Transfer the unsorted data to the sorted lists
For lngIndex = 0 To lstNames.ListCount - 1
lstSortedNames.AddItem lstNames.List(lngIndex)
lstSortedMarks.AddItem lstMarks.List(lngIndex)
Next
' Since we are going to loop until bSwapped = False we need to set it to
' True at the start
bSwapped = True
Do Until Not bSwapped
' Set it to false so if there are no values to be swapped, the
' process will end
bSwapped = False
' Loop through the data. we start at 1 rather than zero because we
' have to look at the previous value
For lngIndex = 1 To lstSortedNames.ListCount - 1
' If the current value is greater than the previous one then we
' need to swap the values
If lstSortedNames.List(lngIndex) > lstSortedNames.List(lngIndex - 1) Then
' Store the high values
strName = lstSortedNames.List(lngIndex)
intMark = lstSortedMarks.List(lngIndex)
' Replace the high values with the low values
lstSortedNames.List(lngIndex) = lstSortedNames.List(lngIndex - 1)
lstSortedMarks.List(lngIndex) = lstSortedMarks.List(lngIndex - 1)
' Replace the low values with the stored high values
lstSortedNames.List(lngIndex - 1) = strName
lstSortedMarks.List(lngIndex - 1) = intMark
' Indicate that we have swapped some data
bSwapped = True
' Get out of the For/Next loop
Exit For
End If
Next
Loop
End Sub
Here is a hint if you want to sort the two lists separately. When you are sorting the marks you will have to make sure you are sorting numbers rather than the raw list values which are strings.
yeh i sorted them into the list boxes meself i know a bit well a tiny amount compared to you kind sir
Done!!! and I feel like I learned a heck of alot because of initially seeing the code...martin did the first bit and then I did the latter part only because I had something to work on
is there a way of cleaning up the sorting code so that only one sort occurs i.e. marks by descending order whilst keeping the name?
currently the names and numbers are stored in an array but can you show me how the code would change if you stored the names and marks in a record instead of an array (without the sorting and finding)?
Thank you so much martin for your guidance, teaching and support...
as you see currently the names and marks are stored in an array called "student"...
but i want to improve the program and store the names and marks in 1 record per student instead and not use the array "student" at all....this has nothing to do with the finding or sorting of the names...
so to recap currently the student array, consists of "x" records, which have 2 fields i.e. name and marks...
but i want to rewrite the program so when i press the "add button", the name and mark is added to one single record for student 1, then another for student 2 etc, and not use the "student" array at all
1) Why do you have lstMarks and lstNames disabled?
2) Why are you adding the entered data to lstSortedMarks and lstSorted Names at data entry time?
3) You say "i want to improve the program and store the names and marks in 1 record per student". Are you talking about saving the data in a database so that you can use the same data next time or are you talking about just keeping the data together in your program? I ask because your Students array is getting filled with one record for each name which contains a "record" for each name and its associated mark but you aren't doing anything with it except to fill it.
4) You asked "is there a way of cleaning up the sorting code so that only one sort occurs i.e. marks by descending order whilst keeping the name?" and the answer is yes but to be sure I understand you, if the unsorted data looks like this
Andy - 44
Sandra - 55
Joe - 66
do you want the output to look like this?
Joe - 66
Sandra - 55
Andy - 44
1) Why do you have lstMarks and lstNames disabled?
so users cannot click on them and type anything inside them, just to lock them really...is that not the correct way?
2) Why are you adding the entered data to lstSortedMarks and lstSorted Names at data entry time?
what is the alternative - to only sort and output when cmdFind is clicked?? is it deemed bad practice to enter them during data entry time? if yes i will change that, just could do with some expert guidance to determine if its good or bad..here i just need your advise, if its bad i will change it
3) You say "i want to improve the program and store the names and marks in 1 record per student". Are you talking about saving the data in a database so that you can use the same data next time or are you talking about just keeping the data together in your program? I ask because your Students array is getting filled with one record for each name which contains a "record" for each name and its associated mark but you aren't doing anything with it except to fill it.
I am talking about just keeping the data together in my program..
Answer: Im a bit confused at this stage aswell, work with me and I am sure we will crack it....I had to first " Create a program for users to enter student’s name and exam mark. When the Add button was clicked, the entered data are added to the arrays and list boxes (one array and list box for names, the other array and list box for marks).
is this done correctly? because I declared the array i.e. "students" in my module, a record is "an element in the array - i.e. studentname or studentmarks - so there are two records in one array" and finally "student(element).studentname is one field in the record of which i have a total of two fields"
BUT I think there maybe an error I have 1 array only, but I need 2, one for names the other for marks....how can I split the current code into two arrays, perhaps I dont need a seperate module at all? im not sure what the "right" way is of doing it..
then I have to:
Improve the program, where the students’ names and marks are stored in a record, rather than arrays - so i think I need to get rid of the "students" array altogether, keep the studentname AND studentmarks elements which are seperate records and possibly can get rid of field i.e. student(element).studentname because I will not be using the array anymore...
IMO I think you just omit "students" plus the "new added" array's of which was "students array" is declared in my module - what do you think? but I am unsure of how you would write the program just using the two records i.e. studentname and studentmarks so that when you click on the cmdadd button it places the student name and mark in 1 record instead of into an array...
The key is to differeniating between arrays and records...so you make 1 version of the program using arrays and another version using records only - which im trying to learn so if apologise if my terminalogy is incorrect...
4) You asked "is there a way of cleaning up the sorting code so that only one sort occurs i.e. marks by descending order whilst keeping the name?" and the answer is yes but to be sure I understand you, if the unsorted data looks like this
I figured it out by duplicating your code and tweaking it but to answer your question, yes - if you were to clean up the code I would want it to look like in your example as it does now after I used your code but its just that I have two versions of your code in my program when I wondered if I could have one copy of it but get it to work as you did below....look at the last project I uploaded this will clarify what I mean by using your code twice...
thanks for your continued help and support your guidance is really helping me...
1) You're confusing listboxes with certain styles of comboboxes. You can't type data directly into a listbox so they don't need to be disabled. BTW if you had more than about 10 names scrollbars would appear but you couldn't use them if the list was disabled.
2) I would not add the data to the sorted lists until the data is actually sorted since, for one, they are in a frame that says "Sorted Output" which it isn't until it's sorted. You have several alternatives here.
--a) Don't add the data to the sorted lists until the Sort button is clicked
--b) Remove the sorted listboxes and sort the data in lstNames and lstMarks when the Sort button is clicked
--c) Remove the Sort button and sort the data in the sorted lists dynamically every time a new record is added to the unsorted lists
--d) Remove the Sort button and the sorted lists and sort the data in the unsorted lists dynamically every time a new record is added
3) In my estimation you haven't done exactly what your instructor asked for which apparently was "When the Add button was clicked, the entered data are added to the arrays and list boxes (one array and list box for names, the other array and list box for marks)", but you're close. While you have two listboxes you don't have to arrays; you only have one and that is Students and it contains both the names and marks. As I mentioned previously you aren't actually using the Students array. Are you supposed to? If you want two arrays it would be pretty simple to do.
We can talk about 3 some more if you want to but I'd like to suggest another improvement and that is to get rid of your Clear button and instead clear txtStudentName and txtStudentMark at the end of cmdAddRecord_Click()
1. yes thats correct, i will enable them...i forgot that rule - thank you
2. Here I already removed the code that adds the data to the sorted boxes when the add button is clicked and made the data appear only when the sort button is pressed - thank you
3. yes i agree, i made a bad error there ...... i do need two arrays, one for marks and one for student names - i dont even think i need a BAS module as thats is required only when there is more then 1 form (yes i am supposed to use the arrays, i thought i was when the data was going into the listbox but logically thinking about it, data is going inside the listboxes but not the arrays)...
im unsure how to create two arrays to add data to them and the listboxes..would you help me to understand as im close you say? i have uploaded the latest version of my project...
i think your improvement sounds great and removing the clear button altogether
moving forward, if two arrays are created and data is stored in them as well as the listboxes, then would it be hard to create the final updated version of the program which is storing the data in one record instead of using two arrays? does that make sense now???
When you say you want to store the name and mark in one record, unless you are talking about a database then you are already doing that when you add them to the Students array so you may not want to get rid of that array.
To create two separate arrays just do this in frmStudentMarks
Code:
Option Explicit
Dim Element As Integer
Private NameArray() As String
Private MarkArray() As Integer
and fill them in cmdAddRecord_Click just like you the Students array.
i removed the module as its not required.... here goes
Code:
Option Explicit
Dim Element As Integer
Private NameArray() As String
Private MarkArray() As Integer
Private Sub cmdAddRecord_Click()
'~~~ Checking. This should be done first !
If Val(txtStudentMark.Text) <= 0 Then
MsgBox "student mark cannot be less than zero. Please re-enter it"
Exit Sub
ElseIf Val(txtStudentMark.Text) > 100 Then
MsgBox "student mark cannot be greater than 100. Please re-enter it"
Exit Sub
End If
NameArray().StudentName = txtStudentName.Text
MarkArray().StudentMark = txtStudentMark.Text
lstNames.AddItem txtStudentName.Text
lstMarks.AddItem txtStudentMark.Text
End Sub
that doesnt work, i receive an "invalid qualifer" error message...
im confused with how to get assign the array to "txtstudent and txtmark.text" any ideas?? is that my error??
Notice this code that used to be in cmdAddRecord_Click
Code:
Element = Element + 1
ReDim Preserve Students(1 To Element) '~~~> We are dynamically allocating size for the array
Students(Element).StudentName = txtStudentName.Text
Students(Element).StudentMark = txtStudentMark.Text
Do you see the difference from what you are doing?
ahhh haaa, i wasnt increasing the element by 1 each time a record was added, i wasnt assigning the data entered into the arrays...and as I am using a dynamic array, I wasnt allocating the size to the array...
is that right? perhaps im not explaining it correctly..but i changed the code to this and it works
Code:
Option Explicit
Dim Element As Integer
Private NameArray() As String
Private MarkArray() As Integer
Private Sub cmdAddRecord_Click()
'~~~ Checking. This should be done first !
If Val(txtStudentMark.Text) <= 0 Then
MsgBox "student mark cannot be less than zero. Please re-enter it"
Exit Sub
ElseIf Val(txtStudentMark.Text) > 100 Then
MsgBox "student mark cannot be greater than 100. Please re-enter it"
Exit Sub
End If
Element = Element + 1
ReDim Preserve NameArray(1 To Element)
ReDim Preserve MarkArray(1 To Element)
NameArray(Element) = txtStudentName.Text
MarkArray(Element) = txtStudentMark.Text
lstNames.AddItem txtStudentName.Text
lstMarks.AddItem txtStudentMark.Text
End Sub
please comment
p..s do i still need this in my program, im not sure what it does tbh...i removed it and my program works, as im not using the "student" array anymore i dont think its needed, what do you think?
Code:
Public Sub DisplayStudent(Index As Integer)
With Students(Index)
lstNames.Selected = True
lstMarks.Selected = True
End With
End Sub
now my program consists of 2 arrays right? markarray and namearray ???
going back to my other issue which was:
"Improve the program, where the students’ names and marks are stored in a record, rather than arrays "
do you understand what i need to do because im still a tad bit lost on that? please clarify
Do a Find in your program for DisplayStudent. Do you find any place where it is called? Your answer to that question should tell you all you need to know.
whats your thoughts?? First you need to tell me what you mean by a record. If you mean something like an entry in the former Students array then yes you need the Students array but that doesn't have to be in a module. You now know how to create an array in the form, right?
thank you so much
I will sleep once this is nearly finished, trust me i will sleep alot better
to be totally honest and upfront with you, i dont know what it means....
if i had to take a guess i would say it was the way I had it before i.e. "students" but I would have called that an array wouldnt you?
what do you make of it? "Improve the program, where the students’ names and marks are stored in a record, rather than arrays"
because one of my latter questions is to explain the characteristics of records and arrays, to which I would say, records allow the user to store mixed variables and arrays do not like in my program, student is string and mark is integer...
then a further question is: why in this program is it better to use a record instead of an array, to which I would answer because this program consists of mixed variables
so a record here is not something we are storing in a database, instead the two entities i.e. studentname and mark are being stored inside it, which IMO was the way I had it before...
but kind sir, what do you think as you are "the VB guru" and im a sleep deprived guy who cannot think straight and has until the other half comes in and drags me to bed not in a nice way....
so please do kindly advise the way you intrepret that especially knowing the latter questions...
the point of this is apparently to teach me arrays and records (without actually teaching me, if that makes any sense)
I will sleep once this is nearly finished, trust me i will sleep alot better
to be totally honest and upfront with you, i dont know what it means....
if i had to take a guess i would say it was the way I had it before i.e. "students" but I would have called that an array wouldnt you? Students is definitely an array. It is an array of type StudentType just as NameArray is an array of type String.
what do you make of it? "Improve the program, where the students’ names and marks are stored in a record, rather than arrays" I can't read your instructor's mind. A record could be an entry in an array (of ANY type), an entry in a database, or an entry in a text file. If I were forced to choose I would say a database but don't go that way unless you've talked about them in class.
because one of my latter questions is to explain the characteristics of records and arrays, to which I would say, records allow the user to store mixed variables and arrays do not like in my program, student is string and mark is integer...
then a further question is: why in this program is it better to use a record instead of an array, to which I would answer because this program consists of mixed variables Without knowing what a "record" is I can't say, but what you say is probably right.
so a record here is not something we are storing in a database, instead the two entities i.e. studentname and mark are being stored inside it, which IMO was the way I had it before...
but kind sir, what do you think as you are "the VB guru" and im a sleep deprived guy who cannot think straight and has until the other half comes in and drags me to bed not in a nice way....
so please do kindly advise the way you intrepret that especially knowing the latter questions...
the point of this is apparently to teach me arrays and records (without actually teaching me, if that makes any sense)
thank you so much, wish my grandad was like you Thanks
Option Explicit
'~~~ Creating the type
Private Type Student
strName As String
intMark As Integer
End Type
Private Sub Form_Load()
'~~~ Creating the "records"
Dim Student1 As Student
Dim Student2 As Student
Dim Student3 As Student
'~~~ Adding data to the first "record"
Student1.strName = "xirokx"
Student1.intMark = 90
'~~~ Adding data to the second "record"
Student2.strName = "MartinLiss"
Student2.intMark = 100
'~~~ Adding data to the third "record"
Student3.strName = "akhileshbc"
Student3.intMark = 50
'~~~ Displaying the "records" in Immediate Window (ctrl + G)
Debug.Print "~~ Student Details ~~"
'~~~ Record1
Debug.Print "Name: " & Student1.strName
Debug.Print "Mark: " & Student1.intMark
'~~~ Record2
Debug.Print "Name: " & Student2.strName
Debug.Print "Mark: " & Student2.intMark
'~~~ Record3
Debug.Print "Name: " & Student3.strName
Debug.Print "Mark: " & Student3.intMark
End Sub
.....
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video) My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet Social Group:VBForums - Developers from India
His Students array is already a user defined type.
I think because of using an array of the Student type, he might be confusing it !
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video) My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet Social Group:VBForums - Developers from India
In your case, you could call Student1 as a record.
Code:
Option Explicit
'~~~ Creating the type
Private Type Student
strName As String
intMark As Integer
End Type
Private Sub Form_Load()
'~~~ Creating the "records"
Dim Student1 As Student
'~~~ Adding data to the first "record"
Student1.strName = "xirokx"
Student1.intMark = 90
And you could call Students(10) as an array of records:
Code:
Option Explicit
'~~~ Creating the type
Private Type Student
strName As String
intMark As Integer
End Type
Private Sub Form_Load()
'~~~ Creating the "records"
Dim Students(10) As Student
That's what I have in my mind. My assumption might be wrong . Marty is the guru here. Let's wait for his reply....
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video) My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet Social Group:VBForums - Developers from India
When MSDN talks about a "record" they are talking about a database record, but in common usage it seems to be ambiguous so I think that the only person who can say what he/she meant is the teacher.
xirokx, did your teacher discuss writing to databases?
I think it is same as what I said in my above post (post #66)...
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video) My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet Social Group:VBForums - Developers from India
You can create dynamic arrays (of records) ! Search Google or MSDN to know more about creating and using dynamic arrays in VB6
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video) My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet Social Group:VBForums - Developers from India
Without any further input from your teacher all I can say is that while "entries" rather than "records" is the more common name for what are in arrays, I would say go for it.
Back in the day when we had to carry our horse to school we did not have databases. If we were lucky we could use a random access file. Now an array was considered to be a collection of like typed variables ie. int, char, float, string while a structure of different type variables was called a record because it was written out as one piece of data (record) to the RAF. If we had an array of them which was usual in the code it would be called an array of records. Of course this may be nothing like modern naming conventions but it used to work.
Slower than a crippled Vista
More buggy than a fresh XP install
Look! Down the road, some 50 miles behind the drunken snail.
It's Ubuntu!
If you are still working on this project then I've got another suggestion for you. Check to see if the Name that is trying to be entered is already in the list, and if it is display a message and don't allow it to be entered.
i sure am, i havent gotten around to coding the records as previously discussed - due to lifes chores...
thats a good idea martin, can you start me off
Im think a IF THEN selector statement but don't know how to discover duplicate entries, is there a special term I could use? p.s. im off on vacation for a week, I fly in 3 hours, so will pick this up when I return...
An experienced programmer would use an API and here is how it's done.
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_FINDSTRINGEXACT = &H1A2
Private Sub cmdAddRecord_Click()
If (SendMessage(lstNames.hwnd, LB_FINDSTRINGEXACT, -1&, _
ByVal CStr(txtStudentNames.Text))) > -1& Then
MsgBox "Name is already in the list"
Else
' Add the names to the list
End If
End Sub
However I suggest you instead just loop through the list and if you find a match for txtStudentNames.Text then display the error message. I'm purposely not showing you how to do this because I think you should be able to do it by yourself.
just to let you know I have given up...I am stranded in Europe due to the volcanic eruption..
I have been reading further into the requirement between arrays and a record of an array and believe its like I initially programmed the first task i.e. assign mixed data types to a record instead of using one data type per array..
will discuss further upon my return..
I need to work out how I can names and marks to my list boxes using a predefined record type that consists of two datatypes...Whilst posts #54 and #55 show me how to define different data types I need to now know how to call these data types in the code to add names and marks to both list boxes...
Last edited by xirokx; Apr 20th, 2010 at 07:52 AM.