Chapter 8 - Visual Basic Schneider1 Chapter 8 Sequential Files.

Slides:



Advertisements
Similar presentations
P1PMF Split1 QBASIC. P1PMF Split2QBasic Command Prompt Will launch the emulator DOS operating system? Press Alt + Enter to display the widescreen.
Advertisements

30/04/ Selection Nested If structures & Complex Multiple Conditions.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Chapter 8 - Visual Basic Schneider1 Chapter 8 Sequential Files.
File Handling Advanced Higher Programming. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a.
Chapter 8 - Visual Basic Schneider1 Chapter 8 Sequential Files.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Chapter 6 - Visual Basic Schneider
Arrays. What is the output of: Private Sub cmdButton_Click() Dim i As Integer, a(1 To 4) As integer Open "DATA.TXT" For Input As #1 For i = 1 To 4 Input.
1 Chapter 6 Repetition. 2 Outline & Objectives Loop Structure Loop Structure Elements of a Loop Structure Elements of a Loop Structure Processing Lists.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will –Learn about arrays One-dimensional arrays Two-dimensional arrays –Learn about searching.
Chapter 8 - VB.Net by Schneider1 Chapter 8 – Sequential Files 8.1 Sequential Files Creating a Sequential File Adding Items to a Sequential File Error Trapping.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
1 Chapter 9 Writing, Testing, and Debugging Access Applications.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Chapter 7 Code Tables. VB Code Box 7-1 Event Procedure for Compute Button Private Sub hsbExemptions_Change() txtExemptions.Text =Str(hsbExemptions.Value)
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 6: Sequential Data Files.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
The Repetition Process in Visual Basic. The Repetition Process The capability to repeat one or more statements as many times as necessary is what really.
5-1 Chapter 5 The Repetition Process in VB.NET. 5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the.
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1B) UTPA – Fall 2011.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
Sequential Files Chapter 13. Master Files Set of files used to store companies data in areas like payroll, inventory Set of files used to store companies.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Chapter 8 - Visual Basic Schneider
1.
1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been.
Sequential files School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 12, Monday 4/07/2003)
13-1 Sequential File Processing Chapter Chapter Contents Overview of Sequential File Processing Sequential File Updating - Creating a New Master.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
VAT Calculator program Controls Properties Code Results.
13- 1 Chapter 13.  Overview of Sequential File Processing  Sequential File Updating - Creating a New Master File  Validity Checking in Update Procedures.
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
Lesson 1. Security At the menu bar at the top you will see the word Tools. Click your mouse on Tools scroll down to Macro. Move the Mouse over and down.
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Slide 1 Controls v Control naming convention –Label: lblName –Command Button: cmdName –Text Box: txtName.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Data and variables in Visual Basic. Annoucement Lecture on Thursday 7:30PM C106 Visual Basic download: 
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Sequential Processing to Update a File Please use speaker notes for additional information!
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Visual Basic - Break Processing
Programming in visual basic .net Visual Basic Building Blocks
Chapter 6: Sequential Data Files
Chapter 7 Arrays.
Introduction to VB programming
Variables and Arithmetic Operations
Chapter 4 - Visual Basic Schneider
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Department Array in Visual Basic
Please use speaker notes for additional information!
An Introduction to Structured Program Design in COBOL
Visual Basic 6 Programming.
Text / Serial / Sequential Files
Chapter 8 - Visual Basic Schneider
Topics Introduction to File Input and Output
Text / Serial / Sequential Files
Presentation transcript:

Chapter 8 - Visual Basic Schneider1 Chapter 8 Sequential Files

Chapter 8 - Visual Basic Schneider2 Outline and Objective Creating Sequential Files Adding items to Sequential Files Using Sequential Files –Sorting Sequential Files –Merging Sequential Files

Chapter 8 - Visual Basic Schneider3 What is a File? A collection of related data kept in secondary storage Two types of files: program files, text(data) files Data files are usually made up of a collection of records. A record is collection of fields. Ali Name First second Huda Ahmed record field

Chapter 8 - Visual Basic Schneider4 Creating a Sequential File Choose a file name Choose a reference number Open the file for Output Write to the file Close the file Open “FileName.txt” for Output as #1 Write#1, “Hello”, 5 Close #1

Chapter 8 - Visual Basic Schneider5 Example: Private Sub cmdCreateFile_Click() Dim name1 As String, x as Integer ' Demonstrate use of Write # statement Open “Marks.TXT" For Output As #1 Write #1, “Ali" Write #1, 22 Write #1, “Ahmed", 23 name1 = “Huda“ x= 20 Write #1, name1, x+2 Close #1 End Sub File name Reference number Write to the file Close the file

Chapter 8 - Visual Basic Schneider66 Example: Private Sub cmdCreateFile_Click() Dim name1 As String, name2 As String Open “Data.txt" For Output As #1 Write #1, “CS" Write #1, 116 Write #1, “BASIC", 10, 20, ”One ” name1 = “Student“ name2 = “Test" Write #1, 2*3, “ID “ & name1, name2 Close #1 End Sub “CS” 116 “BASIC",10,20,”One ” 6,“ID Student”,”Test” Write the string CS surrounded by quotation marks into the file Write the number 116 without any leading or trailing space into the file Creates a new file for output

Chapter 8 - Visual Basic Schneider7

8

9

10 Caution If an existing file is opened for output, the computer will erase the existing data and create a new empty file.

Chapter 8 - Visual Basic Schneider11 Adding Items to a Sequential File Choose a reference number for the file Open the file for Append Write to the end of the file Close the file

Chapter 8 - Visual Basic Schneider12 Assume that the file “data.txt” contains the following entries: –10,20,30,40 What is the content of the file “data.txt” after the user click on the command button

Chapter 8 - Visual Basic Schneider13 Sequential File Different modes in which a file can be used: –Output –Input –Append A file should not be open in two different modes at the same time.

Chapter 8 - Visual Basic Schneider14 Incorrect Code Private Sub Command1_Click() Open "file.txt" For Output As #1 Open "file.txt" For Append As #1 Write #1, "col1", "col2" Close #1 End Sub

Chapter 8 - Visual Basic Schneider15 Incorrect Code Open "file.txt" For Output As #1 Write #1, "Number", "Name" Open "file.txt" For Append As #2 Write #1, 123, "Ali" Close #1 Close #2

Chapter 8 - Visual Basic Schneider16 Correct Code Private Sub Command1_Click() Open "file.txt" For Output As #1 Write #1, "Number", "Name" Close #1 Open "file.txt" For Append As #1 Write #1, 123, "Ali" Close #1 End Sub

Chapter 8 - Visual Basic Schneider17 ERROR: A file should not be open in two different modes at the same time.

Chapter 8 - Visual Basic Schneider18

Chapter 8 - Visual Basic Schneider19 Sorting Sequential Files The records of a sequential file can be sorted on any field by first reading the data into parallel arrays and then sorting on a specific field, and then rewriting the data into a file.

Chapter 8 - Visual Basic Schneider20 Merging Sequential Files Steps to merge two sorted files: 1. Open the two sorted files For Input and a third file For Output 2. Get the two items of data from each file 3. Compare and repeat until the EOF. If one item precedes the other, write it into the third file and get another item from the file If the two items are identical, write it into the third file and advance to the next items in both files. 4. Write the remaining items to the third file 5. Close the three files.

Chapter 8 - Visual Basic Schneider21 Common Errors Opening the file in wrong mode Not using Write statement every time you need to add data to a file Not closing the file

Chapter 8 - Visual Basic Schneider22 Deleting or Changing a Record! Create a new file Read and change records Create a new one Erase the old one –Kill “FileSpec” Rename the new file –Name “oldfilespec” As “newfilespec” 22

Chapter 8 - Visual Basic Schneider23 before After

Chapter 8 - Visual Basic Schneider24 before After

Chapter 8 - Visual Basic Schneider25 Error Trapping Visual Basic has a device, called error-trapping, for preventing some types of errors. If an error occurs while error-trapping is active, two things happen: 1. An identifying number is assigned to the Number property of an object called Err 2. The program jumps to error-handling routine.

Chapter 8 - Visual Basic Schneider26 Setting up error-trapping in a procedure : Make the first line of the procedure: On Error GoTo ErrorHandler Type the statements to carry out the procedure End the procedure by typing Exit Sub ErrorHandler: error-handling routine Resume

Chapter 8 - Visual Basic Schneider27 Example ( error-handling routine to handle a “division by zero” error) Private Sub cmdDivide_Click() On Error GoTo ErrorHandler Dim a As Single, b As Single, c As Single picResult.Cls a = Val(InputBox("Enter the numerator.")) b = Val(InputBox("Enter the denominator.")) c = a / b picResult.Print ”The result is = “; c

Chapter 8 - Visual Basic Schneider28 The error-handling routine errorHandler: ‘ Division by zero is error code 11 If Err = 11 Then picResult.Print "You tried to divide by 0, which is illegal" picResult.Print "Try again.” b=Val(InputBox(“Enter the denominator”)) End If Resume End Sub Line Label

Chapter 8 - Visual Basic Schneider29

Chapter 8 - Visual Basic Schneider30 Example ( error-handling routine to handle a “division by zero” error)

Chapter 8 - Visual Basic Schneider31

Chapter 8 - Visual Basic Schneider32

Chapter 8 - Visual Basic Schneider33

Chapter 8 - Visual Basic Schneider34

Chapter 8 - Visual Basic Schneider35 file1 contains: file2 contains: Private Sub Command1_Click() On Error GoTo fixMe x = "file1.txt" kill x Open x For Input As #1 Input #1, x Print x Close #1 Exit Sub fixMe: Print "welcome to error handling" x = "file2.txt" Resume End Sub welcome to error handling 5 Question:

Chapter 8 - Visual Basic Schneider36 Question: What is the output of the following code private sub command1_click() x = 12 y = 6 Print x / (y - 6); On Error GoTo myRoutine Print x / (y - 9); Exit Sub myRoutine: Print "Error"; y = y + 3 Resume End Sub Run time error (Division by zero)

Chapter 8 - Visual Basic Schneider37 Example