0 index
1 File Handling
2 Sequential Access Files
3 The OPEN Statement
4 Methods for the OPEN Statement
5 Choosing a method
6 The filenumber
7 The WRITE # statement
8 The Syntax
9 The INPUT # statement
10 The INPUT Syntax
11 The CLOSE statement
12 The EOF function
13 Syntax and Example
14 NOT operator
15 SETFOCUS method
16 On Error Resume Next

outline
created using slideshow.cgi by Andy Harris















CSCI N331 Visual Basic: n331/vb13filehandling
1. File Handling
  • Is a method of handling external data
  • Data is kept in FILES on the disk and a program can read and write information from or to these files.
  • The programmer is responsible for controlling the flow of this information



































CSCI N331 Visual Basic: n331/vb13filehandling
2. Sequential Access Files
  • These work like a cassette tape or VHS tape. You start at the beginning of the tape and listen or watch from beginning to end (if it is a good tape or movie)
  • This method can be slow if there is a large number of records
  • Records can have a variable length
  • This type of file access is useful when a flexible structure is more important than quick access



































CSCI N331 Visual Basic: n331/vb13filehandling
3. The OPEN Statement
  • Syntax: OPEN filename FOR method AS # filenumber
  • The filename is any valid DOS filename
  • Caution in allowing the user to create the file names



































CSCI N331 Visual Basic: n331/vb13filehandling
4. Methods for the OPEN Statement
  • INPUT: reads information from an existing file (retrieving existing file for input)
  • OUTPUT: creates a new file, adds information to it, overwrites with new information to an existing file
  • APPEND: adds information to the end of an existing file



































CSCI N331 Visual Basic: n331/vb13filehandling
5. Choosing a method
  • Usually OUTPUT is used only when creating a new file as it will destroy existing files with the same name
  • APPEND is generally used for output



































CSCI N331 Visual Basic: n331/vb13filehandling
6. The filenumber
  • Must be an integer, usually 1 or 2
  • This is unique for each file opened - it tells what file number you want to access
  • Keep this simple



































CSCI N331 Visual Basic: n331/vb13filehandling
7. The WRITE # statement
  • This is used to write to a file
  • Before you can write to the file, you must have opened the file via OPEN (Output/Append)



































CSCI N331 Visual Basic: n331/vb13filehandling
8. The Syntax
  • WRITE # FileNumber, Value(s)
  • The FileNumber must match the file number on the OPEN statement
  • The Value(s) would be the direct value or variables separated by commas



































CSCI N331 Visual Basic: n331/vb13filehandling
9. The INPUT # statement
  • This is used to get data from a file and the file must have been opened via the OPEN statement (Input)



































CSCI N331 Visual Basic: n331/vb13filehandling
10. The INPUT Syntax
  • INPUT # FileNumber, variable
  • Where the FileNumber is a valid, open file and variables separated by commas



































CSCI N331 Visual Basic: n331/vb13filehandling
11. The CLOSE statement
  • This statement is used to close the file and release its filenumber
  • The syntax: CLOSE # FileNumber



































CSCI N331 Visual Basic: n331/vb13filehandling
12. The EOF function
  • EOF (end of file) is a Boolean function - it is either TRUE or FALSE
  • Is TRUE if the program has reached the end of the file and is usually used in DO LOOPS with file handling



































CSCI N331 Visual Basic: n331/vb13filehandling
13. Syntax and Example
  • EOF (FileNumber)
  • Example:
       DO
         …..program code
       LOOP UNTIL EOF(1)



































CSCI N331 Visual Basic: n331/vb13filehandling
14. NOT operator
  • This sets a Boolean to its opposite value and is commonly used for check boxes and Boolean properties
  • Example:
    Text1.FontUnderlined = NOT Text1.FontUnderlined
  • If Font Underlined is true, it will become false, if false it will become true



































CSCI N331 Visual Basic: n331/vb13filehandling
15. SETFOCUS method
  • This method is used in conjunction with an object and that object receives the focus.
  • Example:
       Text1.SetFocus
  • By doing this the insertion cursor will appear in the text box.
  • Any subsequent key presses will be processed by the text box



































CSCI N331 Visual Basic: n331/vb13filehandling
16. On Error Resume Next
  • This is used when you anticipate an error in a procedure, especially in file handling
  • When an error occurs, the program skips to the next line
  • This is only good within the procedure where it is defined



































outline

File Handling

Sequential Access Files

The OPEN Statement

Methods for the OPEN Statement

Choosing a method

The filenumber

The WRITE # statement

The Syntax

The INPUT # statement

The INPUT Syntax

The CLOSE statement

The EOF function

Syntax and Example

NOT operator

SETFOCUS method

On Error Resume Next