ISM 3230 Introduction to Programming

 Chapter 10: Data Files
Chapter 10 Student Learning OBJECTIVES:

Upon completion of this chapter, you will be able to:

        1. Create data files.
        2. Read and write records to disk.
        3. Determine the appropriate locations for data-file related code.
        4. Explain the purpose of a Close statement.
        5. Differentiate between sequential and random files.
        6. Trap user errors and handle errors to avoid run-time errors.
        7. Incorporate fixed-length strings into user-defined data types.
        8. Read and write objects in a random file.
        9. Perform add, delete, and edit operations on random files.
        10. Allow the user to input data using the InputBox function.

 

Data Organization Hierarchy:

Name Composition Example
Bit 0 or 1 10110 ...
Byte / Character 8 bits "C"
Field Meaningful set of characters Name ("Jane Doe")
Record Meaningful set of fields One student (name, SSN, GPA, etc.)
File List of records Students.dat
Database (DB) Set of integrated (linked) files Registration DB: students, classes, transcripts, ...

Data Files

1. opening the file

2. processing the data

3. closing the file

Opening Files

Open statement’s form:

Examples:

Open "A:\DataFile.Dat" For Input As #1

Open stFileName For Input As #intFileNumber

Close statement form: Close [#filenumber]

FreeFile Statements

Locating a File Number

Obtain an available file number this way:

Dim intFileNum As Integer

intFileNum = FreeFile Obtain next available file number

Open "File.dat" For Output As #intFileNum


Sequential File Organization

Input #fn, item1, item2,…, itemn

Reading a Sequential File

Example: reading sequential data file and populating two list boxes:

Do Until EOF(intFileNum)

Loop

Close #intFileNum close the input file

End of File (EOF)

Writing to a Sequential File

Trapping Program Errors

Examples of Handling Errors

Private Sub Form_Load()

On Error GoTo HandleErrors 'Turn on error trapping

Open "A:\Testdata.dat" For Input As #1

… (more code here)

Exit Sub 'exit to avoid executing error routine

HandleErrors:

some code to handle error

Resume

End Sub

The Err Object

Error Numbers

Coding Error-Handling Routines

Using Case to handle errors

Resume statement forms:

Exit and Exit Sub Statements

Saving Changes to a File

Unload and End

QueryUnload

QueryUnload procedure example:

Private Sub Form_QueryUnload(Candel As Integer, UnloadMode As Integer)

Dim intResponse As Integer

If mblnIsDirty Then

IntResponse = MsgBox("Data has changed. Save it?", _

vbYesNo + vbQuestion, "Coffee List Changed Warning")

If intResponse = vbYes Then

mnuFileSave_Click

End If

End If

End Sub

Sequential File Prog. Example


Random data files

Type FullName

strLastName As String * 20

strFirstName As String * 15

End Type

Put #filenumber, [recordnumber], Recordname

LOF - length of file


InputBox

Form:

VariableName = InputBox("Prompt", [... optional parameters] )

strName = InputBox("Enter your name.")

 


Chapter Summary:

OBJECTS

 

PROPERTIES

 

METHODS

 

STATEMENTS

 

FUNCTIONS

Err

 

Err.Number

 

Raise

 

Close

 

EOF

   

Locked

     

Exit Sub

 

FreeFile

   

Number

     

Exit Function

 

InputBox

   

Source

     

Get

 

LOF

           

Input

 

LTrim

           

On Error

 

RTrim

           

Open

 

Seek

           

Put

 

Trim

           

Resume

   
           

Write