Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to...
-
Upload
hilary-logan -
Category
Documents
-
view
212 -
download
0
Transcript of Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to...
Irwin/McGraw-HillIrwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc.
PowerPoint® Presentationto accompany
prepared byJames T. Perry
University of San Diego
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill
Ch 10: Data Files
Create data files– Reading, writing, opening, closing
Differentiate sequential & random files Trap user errors and handle them Fixed length strings in user data types Read/Write random data files Updating a random file Using the InputBox function
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill
Data Files
Data files consist of records & fields Record key determines a record’s position
in a file Two common file organizations are
sequential and random Process a file by
1. opening the file2. processing the data,3. closing the file
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill
FreeFile and Close Statements
FreeFile function returns the next unused file number– intFileNumber = FreeFile– In large projects, file numbers are hard to track
and allocate Close issued automatically when you end Issue Close statement before leaving prog.
– Close #1 or Close #1, #2
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill
Sequential File Organization
Sequential file records are stored one after another
Use Write to output data Use Input to read data File must be opened prior to first use Form: Write #fn, item1, item2,…, itemn
Input #fn, item1, item2,…, itemn
EOF function signals end of file
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill
Writing to a Sequential File
Write statement places data in output Form: Write #file, o1, o2, …, on
You can separate fields with commas or semicolons
Output fields are separated with commas, and strings are quoted.
<Cr>/<Lf> ends each record Write #1, txtLname.Text, 54.89
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill
Reading Data in a Sequential File
Form: Input #fn, item1,…,itemn
Separate fields items with commas File number must be that of an open file When you read the last record, end-of-file
signals You detect end of file with the EOF
function: EOF(FileNumber)
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill
Trapping Program Errors
Errors may be trapped asynchronously Visual Basic generates an error number
whenever an error occurs To handle errors, you must
– Turn on error handling feature: On Error...– Create error handling code– Determine what is to be done after processing
the error (continue, end program,…)
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill
The Err Object
The Err object holds information about error that just occurred
Err.Source holds the source of the error Err.Number holds the error number Err.Description contains error description You can raise an error condition—turn on
an error—with: Err.Raise Number:=xx
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill
Coding Error-Handling Routines
On Error statement designates error handler
Code to handle errors follows line label Line label appears on line by itself and ends
with a colon A Case statement works well to sort out
errors with a "switchboard" Continue execution with Resume statement
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill
Exit and Exit Sub Statements
Exit Sub immediately exits current sub procedure
Exit Function immediately exits current function
Exit is used to prematurely halt execution of sub procedure or function when extraordinary conditions occur
Place Exit Sub above error handling label
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill
Saving Changes to a File
When data changes, ask users if they want to save the changes before program ends
Changed data is known as “dirty” data Keep track of data changes with a global
boolean flag Any procedure that allows data to change
should set the flag to True—indicating “dirty” data file
Check file just before ending program
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill
Sequential File Prog. Example
You can load a combo box by reading from a data file and executing the AddItem method
Reading file & list filling halts when EOF occurs on input file
(See Hands On Programming example)
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill
Random data files
You can read/write data in any order Open “filename” For Random As #1 LEN=x Get #filenumber, [record#], RecordName RecordName is a user-defined data type:
Type FullName
strLastName As String * 20
strFirstName As String * 15
End Type
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill
Random data files
Output: PutPut #filenumber, [recordnumber], Recordname
Put #1, iRecordNo, pCustomerRecord Get & Put statements read an entire record You refer to fields in user-defined structure
by recordname.fieldname lstName.AddItem mRec.LastName end of file is calculated via record lengths