Lab 17

 

  1. chapter 14 begins with the discussion of the file controls that we reviewed last week.  The program in fig14.4 connects the dirBox to the new drive selected whenever the dirBox changes (remember, each control initializes itself to the default drive, and directory so they all agree initially).  The program does not show connecting the FileListBox to the DirListBox, but the statement is shown on p 582 and it would be embedded in a DirListBox_Change() event procedure.  The event handler takes care of the problem of the drive not being available—the system cannot read its directory structure (Err.Number = 68).   The author has used different MessageBox formats throughout the text (since section 10.11), and here he illustrates a call to box that shows Retry and Cancel buttons.   He is suggesting that if the a: drive is not ready and you cancel a drive change to a: as a consequence, that you should move to c: as a consequence.   Change his code so that you stay on the drive setting you were previous to the change (You will need a global variable that gets initialized on Load to the default drive).  Insert your .frm file here. 

 

Option Explicit      ' General declaration

Dim x As Integer

 

Private Sub dirDirBox_Change()

    ' Update the file path to the directory path

   filFileBox.Path = dirDirBox.Path

End Sub

Private Sub drvDriveBox_Change()

   On Error GoTo errorhandler  

      ' Update the directory path to the drive

      dirDirBox.Path = drvDriveBox.Drive

      x = drvDriveBox.ListIndex

      Exit Sub      

errorhandler:

      Dim message As String     

      ' Check for device unavailable error

      If Err.Number = 68 Then

         Dim r As Integer        

         message = "Drive is not available."

         r = MsgBox(message, vbRetryCancel + vbCritical, _

                    "VBHTP: Chapter 14")                   

         ' Determine where control should resume

         If r = vbRetry Then

            Resume

         Else   ' Cancel was pressed.

            drvDriveBox.Drive = drvDriveBox.List(x)

            Resume Next

         End If        

      Else

         Call MsgBox(Err.Description, vbOKOnly + vbExclamation)

         Resume Next

      End If     

End Sub

Private Sub Form_Load()

x = drvDriveBox.ListIndex

End Sub

 

  1. In section 14.4 the author illustrates how to select which libraries the IDE will reference when it compiles your code.  The Microsoft Scripting Runtime library is NOT referenced by default, so you will have to go to the Projects menu and check it.  Done  
  2. When this library is installed, you can use the methods shown in Fig 14.8, but this is a different kind of VB component from those we have used so far.   It is a runtime object, not an OLE control, so it has no property table and the IDE cannot allocate its memory at design time as with the others.   Instead, as shown in Fig. 14.9, it is declared in a Dim statement in the General area (global, static) As New FileSystemObject.   Once the IDE has complied this declaration, the program of Fig. 14.4 can be extended to show in a multi-line text box all the information that the FileSystemObject provides.   How is the visible size of the textbox determined?  By the Height and Width properties that accompany a textbox.
    BY THE USING THE HEIGHT PROPERTY.
    By the height and width property.

The menu editor has been used at design time to create a File menu (shown in the first picture on p. 589)  and the menu, from the event procedures, include CreateFolder, DeleteFolder, and Exit items (we skipped over menus in section 10.9).   However, you can read the code and list below each of the FSO methods that are used in creating and destroying folders: 
mFileSysObj.DeleteFolder(s) à Deletes folder ‘s’.  mFileSysObj.CreateFoler(s)  à Creates folder ‘s’.
:FOLDEREXISTS, CREATEFOLDER,
ifFileSysObj.FolderExists(s)

mFileSysObj.CreateFolder(s)
 mFileSysObj.DeleteFolder

  1. Example program Fig 14.11 points out that where the DriveBox and the DirListBox have default settings, the FileListBox does not automatically select a file.  It has a List of file names, and it has a ListIndex property.   Find out what value FileListBox.ListIndex is initialized to. It is initialized to –1.
    -1
  2. Clicking on the FileListBox control (other than on its scroll bar) selects one of the displayed lines (strings), causing it to be highlighted.  How is this string used to find the size and date of the file by that name? The program does a string comparison until it finds the exact string and then reads and returns its properties to the textbox.
    IT USES THE FILESYSTEMOJECT TO GATHER THE INFORMATION
    It looks at the properties of the file.
    The File control of FSO maintains a listbox.  Clicking on an entry highlights it and initializes the ListIndex property to the value of the highlighted entry (the control’s code keeps track of the position of the slider and hence the elements that are currently displayed in the box and what pixel range is assigned to each display slot.  When the mouse position is returned with the click, it can determine which slot was clicked on and then highlight that slot change ListIndex to the number of the array element that was in that display slot at the time)..   The program recovers the value of filFile.ListIndex  and used it to access the listbox array and obtains the string that was highlighted.   This is concatenated with the Path property to produce the fully qualified file name, which is then given to the mFso.GetFile method to return theFile, a File object.  The properties of theFile are then displayed in the textbox.
  3. What does the name theFile mean to the compiler?  It is a File due to the declaration for this module.
    IT REFERS TO A SPECIFIC FILE AND METHODS CAN BE GATHERED ABOUT THAT FILE
    Has a value of nothing.
    The name is declared to reference an object of type File.  When declared, it references no object, but after a Set statement initializes the reference location the name can be used to refer to the object’s properties.
  4. How do multiple lines get entered into the textbox with a single assignment statement (see page 596)? 

txtBox.Text = "Created: " & theFile.DateCreated & _

                 vbNewLine & _

                 "Last Accessed: " & theFile.DateLastAccessed & _

                 vbNewLine & "Last Modified: " & _

                 theFile.DateLastModified & vbNewLine & _

                 "Drive: " & theFile.Drive & vbNewLine & _

                 "Size: " & theFile.Size & " bytes" & _

                 vbNewLine & _

                 "Path: " & theFile.Path & vbNewLine & _

                 "Short Name: " & theFile.ShortName 

The vbNewLine is the designation that instructs the program to place the string on the next line.
TXTDISPLAY.TEXT = TXTDISPLAY.TEXT & VB “TYPE: “ &_

VbNewLine

What is the difference between the contents of Path and ShortPath?  ShortPath contains the path as short path (i.e. each folder name and file name are in the 8.3 format).  Path contains the path in long name format.
 PATH CONTAINS LONG NAME FORMAT, SHORT PATH CDONTAINS SHORT PATH EX A:.\

The ShortPath contains the folder name and the file names in the 8.3 format and the Path puts the folder and file names in long name format.

The final FSO is TextStream, which can be thought of as a hose or drain built into the bottom of a bucket.   If you connect one end  to a source of text (a File) you can start and stop it and extract the amount of text that you want at one time.   The various Read and Skip statements measure the amount of text you want to extract, either by a character count or until a marker is reached (two markers are provided, what are they?).  AtEndOfLine, and AtEndOfStream
SELSTART, SELLENGTH
AtEndOfLine

AtEndOfString

  1. You can reverse the action by writing an amount of characters to the stream and thereby filling the bucket.   When you write can insert markers between groups of characters.  Explain what has to be done initially to connect the hose.

Need a Form_Load() sub procedure to initialize it.
YOU MUST INTIALLY OPEN
In the general declaration , a FileSystemObject object named mFileSysObj, a File varible named mFile, and a TextStream varible named mtextStream are declared.
The FSO method CreateTextFile interacts with the file system to make a new entry: “c:\clients.dat”.   The method GetFile creates the object that will be called mFile whose properties are the newly created file.   The OpenAsTextStream method of mFile is used to create the mTxtStream object that processes the contents of mFile as text.   mTxtStream’s write method can now be used to place text into the file.

  1. The “words” that you write to the TextStream can be grouped as “lines.” Each line can represent a record the information pertaining to one entity, and the words can be the different data values (fields) that describe that entity.   A file of entity records is usually homogeneous:  each line has the same number of words, each record has the same fields, each field has the same range of valid values.   The EOL marker separates to successive records, and some internal character is used to delimit each field (word).  But because the number of characters needed to represent different values are not the same (unless we pad and truncate the value’s string representation to force uniformity), records as in the example, each having three fields, still comprise different numbers of characters.   Once we have written a sequence of records to the file we want to “rewind” the file and read these same records out.   This requires that we parse the stream, finding the embedded markers and delimiters, and convert the string representations of the field values back into their original data types.   The programs to build and review a sequential file are given in Figures 14.17 and 14.19.   Explain how a MaskEdit control works.  Specialized version of a TextBox that allows for validation of input by ensuring that only data in the correct format is input.
    IT ONLY LETS CORRECT  DATA BE INPUTED.
    Maxedit allows for validation of input by ensuring that only data in the correct format is input
  2.   Explain what the With statement does (p. 601).  Allows for the setting of several properties for indicated control.  In this example, it is mskAccount.
    IT ALLOWS DATA ASSOCIATED WITH THAT STATEMENT TO BE ACCESSED
    It is used to set four maxedit properties.
  3. Simplify Fig 14.19 by using the Split function from p. 355.  Insert your revised smdRead_Click() subroutine here. 

Private Sub cmdRead_Click()

   Dim s As String, x() As String     

   On Error GoTo handler   ' Set error trap  

   ' Read the data

   s = mTxtStream.ReadLine

   x = Split(s) 

   ' Place only the String portion representing the

   ' name in the TextBox.

   mskAccount.Text = x(0)  

   ' Place the formatted dollar amount in the TextBox.

   ' mark2 is positioned at the beginning of the amount.

   txtName.Text = x(1)

   txtBalance.Text = x(2)

   Exit Sub  

handler:

   If Err.Number = 62 Then      ' EOF error

      Call mTxtStream.Close

      cmdRead.Enabled = False

      lblFileName.Caption = ""

   Else

      Call MsgBox(Err.Description)

   End If  

End Sub

Private Sub cmdRead_Click()

   Dim s As String, X() As String

   X = Split(s)

  1. Fig 14.20 illustrates reading through a sequential file, looking at each record’s fields, and deciding if this record qualifies for processing (display in the textbox).   The loop is Do <read record><examine/process> While <not EOF>.   The user presses one of three buttons, and the button’s array index tells the program what to look for in deciding whether a record qualifies.   How is the parsing of the stream in this case different from the parsing done in 14.19?

In some sense, it is random access due to the fact that we extract a particular portion of the string and then compare it using the if/then/else statement.
THE PARSE IS ACTUIALLY USED AND USED THROUGH MASKING, AND 14.20 USED A DO/WHILE LOOP TO PERFORM THE FUNCTION
In some cases it is random access due to the fact that we extract a pactilur portion of the string and then compare it using the if\then\else statement.

 

  1. In this specific example, the account number and last name do not change, but the Balance does.   Typically we would want to periodically find an individual’s record and update the balance.   To find the record we just have to read through the file until we find the record with the right account number.   We would then want to enter a new balance and write the record back to the file.   What problems does this need pose?  What capability would we need for the computer to have if we are to be successful?  Matching newly entered data with that of the old records.  The computer would need to match the newly entered data with that of the old records.  In other words, the program would need to be able to place the new information in the correct location in order to update the old information.
    You do not know where in the record the original data is being taken out of to replace it in the same position.  The record would need to set in some type of table to be able to be able to access the data in logical order and to know the position of the data.

    For example, if we could read the entire stream into memory as one long string, would we then be able to update the field in place?  If this wouldn’t always work, what other capabilities might we wish for?  No, not always.  We would need a way to determine the correct location in the string and then be able to replace the old data in the string.  With the current methods we have now, this would entail lengthy programming and the insurance that all data entered would be in the exact same format.  We need Random Access.

Not unless the position of the data never changed.  Some way to divide the string into an array or table that will have positions set and not moved.