1 / 15

prepared by James T. Perry University of San Diego

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

Download Presentation

prepared by James T. Perry University of San Diego

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. prepared by James T. Perry University of San Diego

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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)

  8. 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,…)

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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)

  14. 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

  15. 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

More Related