Download presentation
Presentation is loading. Please wait.
1
8 Records 25/07/2019
2
Learning Objectives State: The difference between records and arrays.
How to set up a record structure and declare an array of records. How to store and retrieve the contents of an array of records. How to declare a variable that is available on other forms. How to refer to a variable declared as above another form. 25/07/2019
3
A Record Can store many items of data regardless of data type.
An array: Can only store data of one type. 25/07/2019
4
An array of Records Each column is a field. Each row is a record.
Account Number Surname Forename Balance 45278 Smith Sally €10.00 1208 Jones John €20.00 3217 Cain Shazad €100.00 4310 White Peter €250.00 25/07/2019
5
Declaring an array of records
Declare a single record globally. Structure AccountStructure Dim AccountNumber As Integer Dim Surname As String Dim Forename As String Dim Balance As Decimal End Structure Declare an array of this record. Dim Accounts(4) As AccountStructure 25/07/2019
6
Processing Records Accounts(1).Forename = “Sally”
Accounts(3).Balance = 100 lblDisplay.Text = Accounts(1).AccountNumber 25/07/2019
7
Declaring an array of records
Declare a single record globally. Structure … Dim … As … End Structure Declare an array of this record. Dim …(…) As … Record Name Field Names & Data Types No. of records req. Record Array Name Record Name (used above)
8
Declaring an array of records
This is like declaring your own data type. The data type is a 1x1 array of field names of various data types. Then you declare a variable which is a 1D array of the 1x1 data type. Which will result in a 2D array of records. 25/07/2019
9
…(Element).FieldName Processing records Array of Records Name
25/07/2019
10
8 Student Test Marks Specification:
Allow the user to enter up to 10 students and their test marks into an array. Allow the user to search for student and see his/her test mark. Allow the user to see all students and their marks. Allow the user to reset and enter a new set of students and their marks.
11
8 Student Test Marks txtName txtMark butAdd lblMark txtSearchName
butDisplayAStudent butDisplayAllStudents butReset
12
Set up an record structure
Name Mark Globally: 'The following will set up a Student Record with the "Name" and "Mark" fields. Structure StudentStructure Dim Name As String Dim Mark As Integer End Structure
13
Set up an array of records for 10 students
Name Mark Globally: 'Set up the Students Array with space 10 students. 'It will be used to store each student's name and mark. Dim Students(10) As StudentStructure
14
Global Variable – NumberOfStudents
Also globally: 'Will be used to hold the number of students currently in the StudentsArray. Dim NumberOfStudents As Integer
15
butAdd 'Increment the Number Of Students to move to next element in the Students Array. NumberOfStudents = NumberOfStudents + 1
16
butAdd 'Store the name entered into the Name field of the Students Array. Students(NumberOfStudents).Name = txtName.Text 'Store the mark entered into the Mark field of the Students Array. Students(NumberOfStudents).Mark = txtMark.Text
17
butAdd 'Clear the name and mark entered. txtName.Text = ""
txtMark.Text = "" lblMark.Text = "" 'Clear the list box which may be displaying previously entered students. lstDisplayStudentMarks.Items.Clear() 'Put the cursor back into the name text box ready for a new student. txtName.Focus()
18
butDisplayAStudent 'Will be used to store the required student name.
Dim SearchName As String 'Will be used to represent each element of the array. Dim Element As Integer
19
butDisplayAStudent 'Store the required student.
SearchName = txtSearchName.Text
20
butDisplayAStudent 'Loop through each element of the Students array and look for the student name entered. For Element = 1 To 10 'Has the student name been found? If Students(Element).Name = SearchName Then 'If so, show the student's mark. lblMark.Text = Students(Element).Mark Exit Sub End If Next Element 'If the loop is finished then the student was not found. So tell the user. MsgBox("The name was not found!")
21
butDisplayAllStudents
'Clear the list box from last time. lstDisplayStudentMarks.Items.Clear()
22
butDisplayAllStudents
'Will be used to represent each element of the array. Dim Element As Integer
23
butDisplayAllStudents
'Loop through the Students array and add each student's name and mark (with a space between) to the list box. For Element = 1 To 10 lstDisplayStudentMarks.Items.Add(Students(Element).Name & " " & Students(Element).Mark) Next Element
24
butReset 'Set the number of students back to 0. NumberOfStudents = 0
'Clear all text boxes, labels and the list box ready for a fresh set of students. txtName.Text = "" txtMark.Text = "" txtSearchName.Text = "" lblMark.Text = "" lstDisplayStudentMarks.Items.Clear() 'Finish by the placing the cursor in txtName ready for a new student name. txtName.Focus()
25
butReset Now: What happens? What should happen? Why does this happen?
Enter 5 student names and marks. Search for a student you have entered and check the correct mark is returned. Search for a student you have not entered and check you get a “Not found” message. Display all students and their marks and check you get back the same results as you entered. Click the reset button. Enter 2 new student names and marks. Display all students and their marks. What happens? What should happen? Why does this happen? 25/07/2019 25
26
butReset 'Loop through the array and clear each name and mark.
Add the following code to the Reset button. ‘All programs assume that the start values are 0 and ‘arrays may contain values from previous ‘processing. If they do then programs will use this ‘data and give incorrect results. 'Loop through the array and clear each name and mark. Dim Element As Integer For Element = 1 To 10 Students(Element).Name = "" Students(Element).Mark = 0 Next Element
27
8 Student Test Marks Run the program and test it.
28
Commenting on Records In presentation 8 I will only ask for comments to Records. Your comments MUST explain: What is the record for? And if it is important: How many elements and why this number? And when it is being used: What are you storing or retrieving? When (after and before what) are you doing this and why does it have to be done there? When in the procedure code or, if it is on its own, in which procedure (button, checkbox, textbox, etc…)?
29
Extension “Estate Agency Records” Program 1
An Estate Agency has a program which stores details of the last 10 queries made. These are held in an array which contains, for each query made, the following: The name of the customer The number of bedrooms requested The date on which the query was made Whether the customer asked for details of a property or not (As Boolean – Yes/No). Write a program which allows: 10 queries to be stored. When a name is entered elsewhere on the form (textbox and button) it displays all their query’s details. A button to display all queries stored (in another separate list box). A reset button. 25/07/2019
30
Extension “Health Club” Program 2
Store the following details of members of a health club in an array of records: Name Date of Birth (DOB) Weight (kg) Height (m) Write a program which allows: Member details (as above) to be stored. When a name is entered elsewhere on the form (textbox and button) it displays all their query’s details. A button to display all member details stored (in another separate list box). A reset button. 25/07/2019
31
Plenary What is the difference between records and arrays? 25/07/2019
32
A Record Can store many items of data regardless of data type.
An array: Can only store data of one type. 25/07/2019
33
Plenary How can we set up a record structure and declare an array of records? 25/07/2019
34
Declaring an array of records
Declare a single record globally. Structure AccountStructure Dim AccountNumber As Integer Dim Surname As String Dim Forename As String Dim Balance As Decimal End Structure Declare an array of this record. Dim Accounts(4) As AccountStructure 25/07/2019
35
Plenary How do we store and retrieve the contents of an array of records? 25/07/2019
36
…(Element).FieldName Processing records Array of Records Name
25/07/2019
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.