150 likes | 248 Views
prepared by James T. Perry University of San Diego. 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
E N D
prepared by James T. Perry University of San Diego
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
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 file 2. processing the data, 3. closing the file
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
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
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
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)
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,…)
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
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
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
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
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)
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
Random data files • Output: Put Put #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