Lab 8.0 – Coordinated Files

 

Goals:

    w Use an array of arrays

    w Learn use of the Menu control

    w Use multiple data files

         X Create a “master file”

         X Manage data in various files

 

__1. Set up the basic file components for creating the data base.

          w Create files for starting up the data base

                       X Using Notepad, create a file, advlist, with these contents

                        Bruce

                        Bumblebee

                        file1

                        Sally

                        Strudel

                        file2

                       X And a file, file1, with these contents

                        Jerry

                        Reinsdorf

                        Alice

                        Appleby

                        Keisha

                        Kramer

                        Mike

                        Tyler

                        Ahmad

                        Rashad

                       X Plus a file, file2, with these contents

                        Ron

                        Jaworksi

                        Donovan

                        McNabb

                        Melissa

                        Meyers

                        Chamique

                        Holdsclaw

                        Jean

                        Kirkpatrick

 

__2. Create an array of simple data base records.

          w Start up a new VB project, prjSched, with one form, frmSched.

          w Add a module to the project for global data structures.

                       X From the menu bar choose Project, then click on Add Module.

                       X Click on New.

                       X Rename the module mdlDataStruc.

          w Within that module set up the data structures below

                        Public Type crsTaken

                           course As String * 6

                           section As String * 3

                           days As String * 3

                           time As String * 15

                        End Type

                        Public Type stuSched

                           firstname As String * 12

                           lastname As String * 12

                           major As String * 20

                           courses(1 To 7) As crsTaken

                        End Type

                        Public onestudent As stuSched

                        Public student(1 To 5) As stuSched

                        Public Type advisorType

                           firstname As String * 12

                           lastname As String * 12

                        End Type

                        Public advisor(1 To 2) As advisorType

                        Public advnum As Integer

                        Public stunum As Integer

                        Public crsnum As Integer

                        Public stufileopen As Boolean

          w How does this declaration differ from declarations we have done

              previously?  Why the differences?

 

__3. Begin to learn about the Menu control.

          w Enter the Menu Editor

                       X Bring the form to the forefront, if it is not already there

                       X Move the cursor along the toolbar to the 3rd icon from the left.

                   As you wait, the words Menu Editor should appear.

                       X Click on that icon.

          w Create the skeleton of a drop down menu.

                       X In the Caption: textbox type Main

                       X In the Name: textbox type mnuMain

                       X Click on OK

          w We now have the rudiments of a menu

                       X Notice the word Main in the upper left corner of the form

                       X Click on it.  The code window opens. 

                   We could write code at this point, but we will not do so. 

                   Instead we will be giving it some submenu items.

 

__4. Create submenu items for mnuMain.

          w Enter the Menu Editor again

          w Check online Help for information

                       X When the Menu Editor window opens, press F1

                       X Peruse the information there, noticing especially the

                   description of the Right, Left, Up and Down Arrows.

          w Create the first submenu item

                       X In the largest window at the bottom of the Menu Editor form,

                   position the cursor just below the word Main and click.

                       X Type Create Files and mnuCreate for the caption and name

                       X Click on the Right Arrow to make it a submenu item to Main.

                       X Click on the scroll arrow for Shortcut: and choose Ctrl+C

                   from the items presented.

                       X Open the code window for that submenu item and type:

                                    Debug.Print “OK”

                       X Test your code.  Does it work?

          w Create two more submenu items

                       X Create submenu item named Load Files and give it a

                   shortcut of Ctrl+L.

                       X Do not give it any code

                       X Create submenu item named Exit and give it a

                   shortcut of Ctrl+E.

                       X Give it this code

                                    End

          w Run your program and test the Exit submenu item.

 

__5. Create a framework for working with a data base of student schedules.

          w Add a frame control to the form.

          w Place on that frame textboxes for displaying one student schedule.

          w Add two listboxes to that frame

                       X One for listing advisee courses

                       X A second one for listing advisees

          w Add a listbox to the form, but outside the frame for listing advisor    w Write code to position and size the above items achieving an effect

             similar to the one below. 

 

 

 

__6. Now write code for creating the student files.

          w Open the code window for Create stuFiles.

          w Delete the old code.

          w Replace it with this code.

                         Open App.Path & "\advlist.txt" For Input As #1

                         Open App.Path & "\advisors" For Output As #2

                         Close #2

                         Call AddOneAdvisor(1)

                         Call AddOneAdvisor(2)

                         Close #1

                       X Why do we open the advisors file, then close it right away?

          w Now add the code for AddOneAdvisor

                        Public Sub AddOneAdvisor(ByVal advn As Integer)

                        Dim first As String

                        Dim last As String

                        Dim filename As String

                          Line Input #1, first

                          Line Input #1, last

                          Line Input #1, filename

                          lbxAdvisors.AddItem Trim(first) & " " & Trim(last)

                          Open App.Path & "\advisors" For Append As #2

                          Print #2, first

                          Print #2, last

                          Close #2

                         advisor(advn).firstname = first

                         advisor(advn).lastname = last

                         Call CreateStudentsFile(last, filename)

                        End Sub

                       X Why are we using Line Input #?

                       X What is the purpose of Trim?

                       X What does the variable filename refer to?

                       X What happens when a file is opened For Append?

                       X Why do we choose that particular file opening mode here?

          w Part of the code for CreateStudentsFile is below.  Complete it and

             add it to the program.

Public Sub CreateStudentsFile(ByVal lname As . . ., ByVal fname As . . .)

Dim . . .

Dim crs As Integer

 Open . . . & "\" & fname & ".txt" For . . . As #2

 Open . . . & Trim(lname) & "-stu" For . . .  As #3 Len = . . .

 For i = . . .

   Line Input #2, . . .

   Line Input #2, . . .

   onestudent.major = "Computer Science"

   For crs = 1 To 7

      onestudent.courses(crs).course = "none"

   Next crs

   Put . . .

 Next i

 Close . . .

 Close . . .

End Sub

          w Let’s think for a minute about what we’ve done.

                       X Our goal was to produce a file with a list of advisors.

                   And for each advisor we created a file with a list of advisees,

                   tied by name to the advisor.

                       X We started with the file advlist.  Was it necessary to start that

                   way?  Could we have input the advisor information at run time

                   from a textbox?  If not, why not?  If so, how?

 

__7. Create code for accessing the advisor file we created.

          w For the Load Files menu item, complete the code below.

                        Dim i As Integer

                         lbxAdvisors.Clear

                         Open App.Path & "\advisors" For . . . As #1

                         For i = 1 To 2

                          Input #1, . . .

                          Input #1, . . .

                          lbxAdvisors.AddItem advisor(i).firstname & " " & advisor(i).lastname

                         Next I

                         Close #1

                       X What does the Clear method accomplish?

                       X Why do we use it here?

                       X Why is it important for us to close the file at the end of this

                   procedure?

 

__8. Write code for accessing the advisee files we created.

          w Add to Form_Load this line of code.

                         stufileopen = False

          w For the click event of lbxAdvisors, complete the code below.

Dim stu As Integer

 advnum = lbxAdvisors.ListIndex +  . . .

 If stufileopen Then Close #9

 Open . . . & "\" & Trim(advisor(advnum).lastname) & "-stu" For . . . As #9 Len = . . .

 stufileopen = . . .

 lbxStudents.Clear

 For stu = 1 To 5

    Get #9, stu, onestudent

    lbxStudents.AddItem . . .

 Next stu

                       X What is the purpose of the Boolean variable stufileopen?

                       X Why do we check its value and take the action taken?

          w Now write code for the click event of lbxStudents.

                       X The code should fill in the 3 textboxes and the courses listbox

                   that are on the frame.

                       X Remember that the appropriate student file is still open.

                       X Give the global variable stunum its proper value.  We’ll need

                   that later for saving changes made.

 

__9. Finally we will produce an interface for supplying course information.

          w Add form frmCourse to the project.

          w Give it textboxes for displaying information about each course.

          w Give it two command buttons labeled UPDATE and OK.

                       X Give the OK button this code.

                         Me.Hide

                         frmSched.Show

                       X We will write code for the UPDATE button later.

          w Write code for the click event on lbxCourses to do the following:

                       X Set the global variable crsnum to the course clicked on.

                       X Make form frmSched disappear.

                       X Make form frmCourse appear.

                       X What code did you write?

          w Now write code for the Form_Activate event of frmSched.

                       X Fill in the textboxes of frmSched.

                       X Remember that global structure onestudent contains the

                   complete record of the chosen student and global variable

                   crsnum contains the index of the course clicked on.

          w Now write code for the UPDATE button of frmSched.

                       X The button does not save to long term storage (i.e., the disk)

                   but only to short term storage (i.e., the current student record).

                       X Remember, again, the global values at your disposal.

                       X What code did you write?

          w And now add another menu item for saving to disk any changes

             made to the current student data displayed.

                       X What code did you write?

 

__10. Run your program.  Test it thoroughly.

 

__11. You may have noticed that after clicking UPDATE any change to the course id does not appear in lbxCourses of frmSched, although choosing Save from the menu does make the change permanent.  To make that change is not quite a straightforward matter.

          w Extra: Write code to make this happen.