Random Access 2
Up
This project is an improved version of the previous one. It includes the students' scores as well as their names, it stores and manipulates the data in an ARRAY, simply copying the contents of the array into the list boxes whenever necessary. And since it makes sense to keep a student's name and score together in a kind of 'super variable', the creation of such super-variables using user-defined variable types is explained.

Make a new EXE project and save it immediately (File>Save Project) into a new folder called random_access2 - save both the form file and the project file inside the new folder in the usual way, giving them the same name as the folder.

Add 2 labels, 2 listboxes, 2 commandbuttons and 2 textboxes, setting caption, backcolor, list and name properties as shown and setting the names to lblStudent, lblScore, lstStudent, lstScore, cmdAlphaSort, cmdNumSort, txtNewName, txtNewScore.

The Plan

This project is based on an array called 'studentscore' which contains both the students' names and their scores. The array is made up of variables of type studentrecordtype which is itself made up of a 20 character string (occupying 20 bytes) followed by an integer (occupying 2 bytes)

The name and score information is displayed in two separate text boxes. Whenever the array is modified the list boxes are both updated using the updatelists procedure. The text boxes are not meant to be seen - once you have checked how the project works you can shrink the form (in design mode, not run mode) until the text boxes can no longer be seen.

The Code

Here is the code for this project - I don't have time now to explain it in detail but it should work as is. Some comments have been included and I will add detailed explanations when I have time...

Private Type StudentScoreType ' Define user-defined type.
     name As String * 20 ' 20 bytes storage needed
     score As Integer ' 2 bytes storage needed, max integer value = approx 32000
End Type

Const numberofrecords = 10
Dim studentscore(numberofrecords) As StudentScoreType
Dim filename, recordnum ' Declare variable.

Private Sub Form_Load()
filename = App.Path & "\studentscoredata.dat"
Open filename For Random As #1 Len = Len(studentscore(1))
     For n = 1 To numberofrecords
          Get #1, n, studentscore(n)
     Next
Close #1
updatelists  ' user-defined subroutine - see below
End Sub

Private Sub Form_Unload(Cancel As Integer)
Open filename For Random As #1 Len = Len(studentscore(1))
     For n = 1 To numberofrecords
          Put #1, n, studentscore(n)
     Next
Close #1
End Sub

Private Sub lstStudent_Click()
tempindex = lstStudent.ListIndex ' these 3 lines deselect all items in the other list
lstScore.ListIndex = -1 ' no item selected
lstStudent.ListIndex = tempindex
recordnum = lstStudent.ListIndex + 1
txtnewName.Text = ""
txtnewName.SetFocus
End Sub

Private Sub lstScore_Click()
tempindex = lstScore.ListIndex
lstStudent.ListIndex = -1
lstScore.ListIndex = tempindex
recordnum = lstScore.ListIndex + 1
txtnewScore.Text = ""
txtnewScore.SetFocus
End Sub

Private Sub txtNewName_Change()
If txtnewName.Text <> "" Then
     studentscore(recordnum).name = txtnewName.Text
     updatelists  ' user-defined subroutine - see below
End If
End Sub
Private Sub txtnewScore_Change()
If txtnewScore.Text <> "" Then
     studentscore(recordnum).score = Val(txtnewScore.Text)
     updatelists  ' user-defined subroutine - see below
End If
End Sub

Private Sub updatelists()
For n = 1 To numberofrecords
     lstStudent.List(n - 1) = Trim(studentscore(n).name)
     lstScore.List(n - 1) = studentscore(n).score
Next
End Sub

Private Sub txtnewScore_KeyPress(KeyAscii As Integer)
' if the key pressed was not a number key then change it into nothing
' All non-numeric key presses will be blocked including decimal points and negative signs
If Chr(KeyAscii) < "0" Or Chr(KeyAscii) > "9" Then KeyAscii = 0
' check whether the new number would exceed 32000, the biggest number that
' can be stored in an INTEGER variable
If Val(txtnewScore.Text & Chr(KeyAscii)) > 32000 Then
     MsgBox ("Integer variables cannot contain numbers greater than 32000")
     KeyAscii = 0
End If
End Sub

Private Sub cmdNumSort_Click() ' uses bubble sort
Dim temp As StudentScoreType
For n = 1 To (numberofrecords - 1)
     For m = n + 1 To numberofrecords
          If studentscore(m).score < studentscore(n).score Then
               temp = studentscore(m)
               studentscore(m) = studentscore(n)
               studentscore(n) = temp
          End If
     Next m
Next n
updatelists
End Sub

Private Sub cmdAlphaSort_Click() ' uses bubble sort
Dim temp As StudentScoreType
For n = 1 To (numberofrecords - 1)
     For m = n + 1 To numberofrecords
          If studentscore(m).name < studentscore(n).name Then
               temp = studentscore(m)
               studentscore(m) = studentscore(n)
               studentscore(n) = temp
          End If
     Next m
Next n
updatelists
End Sub

Previous Up