Chapter 14 Sequential Files

Similar documents
Chapter 1. Block Diagram. Text .. 1

Introduction to Computer Use II

Answer: C. 7. In window we can write code A. Immediate window B. Locals window C. Code editor window D. None of these. Answer: C

The Windows Scripting Host comes with a secret script extension, called

GUJARAT TECHNOLOGICAL UNIVERSITY DIPLOMA IN INFORMATION TECHNOLOGY Semester: 4

An InputBox( ) function will display an input Box window where the user can enter a value or a text. The format is

variables programming statements

The FileSystemObject Object. Active Server Pages

Visual Programming 1. What is Visual Basic? 2. What are different Editions available in VB? 3. List the various features of VB

INTRODUCTION TO VISUAL BASIC 2010

Start Visual Basic. Session 1. The User Interface Form (I/II) The Visual Basic Programming Environment. The Tool Box (I/II)

AddPrinterConnection method

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Tools for the VBA User

MICROSOFT EXCEL 2000 LEVEL 5 VBA PROGRAMMING INTRODUCTION

Understanding the MsgBox command in Visual Basic

Programming with visual Basic:

Understanding File Management

VBA Macro for Micro Focus Reflections Face-to-Face Orlando March 2018

Learning the Pro/ENGINEER Interface

Dive Into Visual C# 2008 Express

WORKING WITH WINDOWS SHELL

Using the Script Editor

Setting Access Controls on Files, Folders, Shares, and Other System Objects in Windows 2000

Windows Me Navigating

SEE5A/ Visual Basic Unit : I - V

Server Edition USER MANUAL. For Microsoft Windows

Using Visual Basic Studio 2008

Tech-Talk Using the PATROL Agent COM Server August 1999 Authored By: Eric Anderson

File Organization and Management

Windows File Management

Microsoft Visual Basic 2005 CHAPTER 6. Loop Structures

A Back-End Link Checker for Your Access Database

BasicScript 2.25 User s Guide. May 29, 1996

CS313D: ADVANCED PROGRAMMING LANGUAGE

Spectrometer Visible Light Spectrometer V4.4

MULTIPROG QUICK START GUIDE

CSCE Introduction to Computer Systems Spring 2019

Lab Determining Data Storage Capacity

Content: Step-by-Step instructions to duplicate MS Outlook files to a file. Purpose: To move content from one computer to another.

OLI Engine in Aspen Hysys

Contents. More Controls 51. Visual Basic 1. Introduction to. xiii. Modify the Project 30. Print the Project Documentation 35

The Lackner Group. Addendum to the Administrator s Guide for FileMaker Server 5.5 (February 14, 2003)

Server Edition. V8 Peregrine User Manual. for Microsoft Windows

'... '... '... Module created: unknown '... Proj finished: March 21, 2012 '... '...

3. IMPORTANT: When prompted, accept the default Installation directory.

GETTING STARTED WITH VBA

Travel Lite ( 旅 ) USER MANUAL. 2.5 SATA to USB EXTERNAL ENCLOSURE

Programming Concepts and Skills. Arrays continued and Functions

University of Technology Laser & Optoelectronics Engineering Department Visual basic Lab.

3 INSTALLING WINDOWS XP PROFESSIONAL

2Practicals Visual Basic 6.0

Modifying image file contents with Ghost Explorer. This section includes the following topics:

Geography 281 Mapmaking with GIS Project One: Exploring the ArcMap Environment

Welcome Application. Introducing the Visual Studio.NET IDE. Objectives. Outline

Geography 281 Mapmaking with GIS Project One: Exploring the ArcMap Environment

COP 1170 Introduction to Computer Programming using Visual Basic

BackupVault Desktop & Laptop Edition. USER MANUAL For Microsoft Windows

SolidWorks A Visual Basic for Applications tutorial for SolidWorks users SDC PUBLICATIONS

1 Preface About this Manual Intended Audience Revision History Document Conventions Version...

How to set up an Amazon Work Profile for Windows 8

Managing Files & Folders

SMART Recorder. Record. Pause. Stop

Copyrighted Material. Copyrighted. Material. Copyrighted

1) Identify the recording mode, by which you can record the non-standard object in QTP

The Item_Master_addin.xlam is an Excel add-in file used to provide additional features to assist during plan development.

In this chapter, I m going to show you how to create a working

Computer Science 110. NOTES: module 8

Using the Script Editor

1/29/2019. Storage Media. Storage Media

Lecture6 File Processing

Chapter 6 Introduction to Defining Classes

UNIT 1 INTRODUCTION TO VISUAL BASICS 6.0

'... '... '... Developer: William H. White (consultant) '... With: TEKsystems Inc. '... For: AIG. Financial Information Systems

Introduction to C# Applications

DataMaster for Windows

How to Transfer Files

Estuary Model MatLab Compiler Runtime for PCs

Select Objects for Use

Unit 6 - Software Design and Development LESSON 4 DATA TYPES

Fundamentals of Programming Session 25

Fundamentals of Programming Session 27

6/14/2010. VBA program units: Subroutines and Functions. Functions: Examples: Examples:

by Pearson Education, Inc. All Rights Reserved. 2

Visual Basic Program Coding STEP 2

AiM User Guide. Race Studio 3 Track Manager. Release 1.00

Lab - Create a Partition in Windows 8

Version S Cincinnati, Suite 105 Tulsa, OK (918) Fax (918)

Files. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

PROGRAMMING LANGUAGE 2 (SPM3112) NOOR AZEAN ATAN MULTIMEDIA EDUCATIONAL DEPARTMENT UNIVERSITI TEKNOLOGI MALAYSIA

Visual Basic. The Integrated Development Environment. Menu Bar

This guide will show you how to create a basic multi-media PowerPoint presentation containing text, graphics, charts, and audio/video elements.

CENTAUR S REAL-TIME GRAPHIC INTERFACE V4.0 OPERATOR S MANUAL

Unit 4. Lesson 4.1. Managing Data. Data types. Introduction. Data type. Visual Basic 2008 Data types

Introduction to Windows XP

Introduction to Data Entry and Data Types

GV 2 Devicemanagement 2

Partitioning and Formatting Reference Guide

Visual Basic.NET. 1. Which language is not a true object-oriented programming language?

Transcription:

CHAPTER 14 SEQUENTIAL FILES 1 Chapter 14 Sequential Files (Main Page) 14.1 DirListBox, FileListBox, and DriveListBox toolbox icons. 14.2 Some DirListBox, FileListBox, and DriveListBox common properties and methods. 14.3 Some FileListBox properties. 14.4 Demonstrating controls DirListBox, FileListBox, DriveListBox. 14.5 Data hierarchy. 14.6 Reference sdialog with Microsoft Scripting Runtim e selected. 14.7 File System Objects (FSOs). 14.8 FileSystemObject methods. 14.9 Using a FileSystemObject. 14.10 Some common File properties and methods. 14.11 Demonstrating the File FSO. 14.12 Some Folder properties and methods. 14.13 Using a Folder FSO. 14.14 Drive properties. 14.15 Using a Drive FSO. 14.16 TextStream properties and methods. 14.17 Writing data to a text file. 14.18 File open modes. 14.19 Reading from a sequential file. 14.20 A credit inquiry program.

CHAPTER 14 SEQUENTIAL FILES 2 DirListBox FileListBox DriveListBox Fig. 14.1 DirListBox, FileListBox and DriveListBox toolbox icons. Property/method Properties DragIcon DragMode Enabled List ListCount ListIndex MousePointer Visible Methods Drag Refresh SetFocus Icon which is displayed during drag-and-drop operation. Integer. Automatic or manual drag-and-drop. Boolean. Specifies whether or not the user is allowed to interact with the control. String array. Array that stores the Strings that appear in the controls. Integer. Number of items in the List properties array. Integer. Index of selected List property item. Index begins at 0 and is -1 when a value is not selected. Integer. Specifies the shape of the mouse pointer when over the control. Boolean. Specifies whether or not the control is visible. Starts, terminates, or aborts drag operations. Forces the control to repaint itself. Transfers the focus to the control. Fig. 14.2 Some DirListBox, FileListBox and DriveListBox common properties and methods. Property Archive Hidden MultiSelect Path Pattern ReadOnly System Boolean. Specifies whether or not archive attributes are displayed (default is True). Boolean. Specifies whether or not hidden attributes are displayed (default is False). Integer. Specifies whether or not the user can make multiple selections (multiple selection is not allowed by default). String. Specifies the current path. String. Specifies the type of files displayed in the FileListBox. Boolean. Specifies whether or not read-only attributes are displayed. Boolean. Specifies whether or not system attributes are displayed (default is False). Fig. 14.3 Some FileListBox properties.

CHAPTER 14 SEQUENTIAL FILES 3 1 ' Fig. 14.4 2 ' Demonstrating FileListBox, DirListBox, 3 ' and DriveListBox controls 4 Option Explicit ' General declaration 5 6 Private Sub dirdirbox_change() 7 8 ' Update the file path to the directory path 9 filfilebox.path = dirdirbox.path 10 End Sub 11 12 Private Sub drvdrivebox_change() 13 14 On Error GoTo error handler 15 16 ' Update the directory path to the drive 17 dirdirbox.path = drvdrivebox.drive 18 Exit Sub 19 20 errorhandler: 21 Dim message As String 22 23 ' Check for device unavailable error 24 If Err.Number = 68 Then 25 Dim r As Integer 26 27 message = "Drive is not available." 28 r = MsgBox(message, vbretrycancel + vbcritical, _ 29 "VBHTP: Chapter 14") 30 31 ' Determine where control should resume 32 If r = vbretry Then 33 Resume 34 Else ' Cancel was pressed. 35 drvdrivebox.drive = drvdrivebox.list(1) 36 Resume Next 37 End If 38 39 Else 40 Call MsgBox(Err., vbokonly + vbexclamation) 41 Resume Next 42 End If 43 44 End Sub Fig. 14.4 Demonstrating controls DirListBox, FileListBox and DriveListBox (part 1 of 2).

CHAPTER 14 SEQUENTIAL FILES 4 Initial GUI at execution. GUI after user has selected a folder. GUI after user has selected a drive. Fig. 14.4 Demonstrating controls DirListBox, FileListBox and DriveListBox (part 2 of 2). Sally Black Tom Judy Iris Blue Green Orange File Randy Red Judy Green Record J u d y Byte Byte 00000000 01001010 Field Character (Unicode character J) 0 Bit Fig. 14.5 Data hierarchy.

CHAPTER 14 SEQUENTIAL FILES 5 Fig. 14.6 References dialog with Microsoft Scripting Runtime selected. Object type FileSystemObject File Folder Drive TextStream Allows the programmer to interact with Files, Folders and Drives. Allows the programmer to manipulate Files. Allows the programmer to manipulate Folders (i.e., directories). Allows the programmer to gather information about Drives (hard disks, RAM disks computer memory used as a substitute for hard disks to allow high speed file operations, CD-ROMs, etc.). Drives can be local or remote. Allows the programmer to read and write text files. Fig. 14.7 File System Objects (FSOs). Methods CopyFile CopyFolder CreateFolder CreateTextFile DeleteFile DeleteFolder DriveExists FileExists FolderExists GetAbsolutePathName GetDrive Copies an existing File. Copies an existing Folder. Creates and returns a Folder. Creates and returns a text File. Deletes a File. Deletes a Folder. Tests whether or not a Drive exists. Tests whether or not a File exists. Returns Boolean. Tests whether or not a Folder exists. Returns Boolean. Returns the absolute path as a String. Returns the specified Drive. Fig. 14.8 FileSystemObject methods (part 1 of 2).

CHAPTER 14 SEQUENTIAL FILES 6 Methods GetDriveName GetFile GetFileName GetFolder GetParentFolderName GetTempName MoveFile MoveFolder OpenTextFile Returns the Drive drive name. Returns the specified File. Returns the File file name. Returns the specified Folder. Returns a String representing the parent folder name. Creates and returns a String representing a file name. Moves a File. Moves a Folder. Opens an existing text File. Returns a TextStream. Fig. 14.8 FileSystemObject methods (part 2 of 2). 1 ' Fig. 14.9 2 ' Demonstrating FileSystemObjects 3 Option Explicit ' General declaration 4 Dim mfilesysobj As New FileSystemObject ' General declaration 5 6 Private Sub dirdirbox_change() 7 8 ' Update the file path to the directory path 9 filfilebox.path = dirdirbox.path 10 End Sub 11 12 Private Sub drvdrivebox_change() 13 14 On Error GoTo error handler 15 16 ' Update the directory path to the drive 17 dirdirbox.path = drvdrivebox.drive 18 Exit Sub 19 20 errorhandler: 21 Dim message As String 22 23 ' Check for device unavailable error 24 If Err.Number = 68 Then 25 Dim r As Integer 26 27 message = "Drive is not available." Fig. 14.9 Using a FileSystemObject (part 1 of 4). 28 r = MsgBox(message, vbretrycancel + vbcritical, _ 29 "VBHTP: Chapter 14") 30 31 ' Determine where control should resume 32 If r = vbretry Then 33 Resume 34 Else ' Cancel was pressed. 35 drvdrivebox.drive = drvdrivebox.list(1) 36 Resume Next 37 End If 38 39 Else 40 Call MsgBox(Err., vbokonly + vbexclamation)

CHAPTER 14 SEQUENTIAL FILES 7 41 Resume Next 42 End If 43 44 End Sub 45 46 Private Sub filfilebox_click() 47 Call displaydata ' Update TextBox 48 End Sub 49 50 ' Programmer defined 51 Private Sub displaydata() 52 txtdisplay.text = "" 53 txtdisplay.text = "GetAbsolutePathName: " & _ 54 mfilesysobj.getabsolutepathname( _ 55 filfilebox.path) & vbnewline & _ 56 "GetDriveName: " & _ 57 mfilesysobj.getdrivename( _ 58 filfilebox.path) & vbnewline & _ 59 "GetParentFolderName: " & _ 60 mfilesysobj.getparentfoldername( _ 61 filfilebox.path) & vbnewline & _ 62 "GetTempName: " & mfilesysobj.gettempname 63 End Sub 64 65 Private Sub mnuitmcreatefolder_click() 66 Dim s As String 67 68 ' Get the Folder name 69 s = InputBox("Enter complete path and folder name:", "CREATE") 70 71 ' Test if the Folder already exists 72 If mfilesysobj.folderexists(s) Then 73 Call MsgBox("Folder already exists! Cannot create.") 74 Exit Sub 75 End If 76 77 Call mfilesysobj.createfolder(s) ' Create the Folder 78 Call dirdirbox.refresh ' Repaint DirListBox 79 End Sub 80 Fig. 14.9 Using a FileSystemObject (part 2 of 4). 81 Private Sub mnuitmdeletefolder_click() 82 Dim s As String 83 84 ' Get the Folder name 85 s = InputBox("Enter complete path and folder name:", "DELETE") 86 87 ' Test if the Folder already exists 88 If mfilesysobj.folderexists(s) = False Then 89 Call MsgBox("Folder does not exist! Cannot delete.") 90 Exit Sub 91 End If 92 93 Call mfilesysobj.deletefolder(s) ' Delete the Folder 94 Call dirdirbox.refresh ' Repaint DirListBox 95 End Sub 96 97 Private Sub mnuitmexit_click() 98 End ' Terminate execution 99 End Sub

CHAPTER 14 SEQUENTIAL FILES 8 Initial GUI at execution. GUI after user has selected c: drive and the Accessories menu. The TextBox displays information about file WORDPAD.EXE which the user clicked. Fig. 14.9 Using a FileSystemObject (part 3 of 4). GUI after the user selects Create Folder from the File menu and enters c:\program files\accessories\a FOLDER. Note that the folder is automatically inserted in alphabetical order.

CHAPTER 14 SEQUENTIAL FILES 9 GUI after the user selects Delete Folder from the File menu and enters c:\program files\accessories\a FOLDER. Fig. 14.9 Using a FileSystemObject (part 4 of 4). Property/method Properties DateCreated DateLastAccessed DateLastModified Drive Name ParentFolder Path ShortName Size Methods Copy Delete Move OpenAsTextStream Date. The date the File was created. Date. The date the File was last accessed. Date. The date the File was last modified. Drive. The Drive where the file is located. String. The File name. String. The File s parent folder name. String. The File s path. String. The File s name expressed as a short name. The size of the File in bytes. Copy this File. Same as FileSystemObject method Copy- File. Delete this File. Same as FileSystemObject method DeleteFile. Move this File. Same as FileSystemObject method Move- File. Opens an existing File as a text File. Returns TextStream. Fig. 14.10 Some common File properties and methods.

CHAPTER 14 SEQUENTIAL FILES 10 1 ' Fig. 14.11 2 ' Demonstrating File FSO 3 Option Explicit ' General declaration 4 Dim mfso As New FileSystemObject ' General declaration 5 6 Private Sub dirdirectory_change() 7 8 ' Update filfile's Path when dirdirectory changes 9 filfile.path = dirdirectory.path 10 End Sub 11 12 Private Sub filfile_click() 13 Dim thefile As File 14 15 txtbox.text = "" ' Clear TextBox 16 17 ' Determine which file name was selected and return 18 ' its File object. 19 Set thefile = mfso.getfile(filfile.path & "\" & _ 20 filfile.list(filfile.listindex)) 21 22 ' Display File information in PictureBox 23 txtbox.text = "Created: " & thefile.datecreated & _ 24 vbnewline & _ 25 "Last Accessed: " & thefile.datelastaccessed & _ 26 vbnewline & "Last Modified: " & _ 27 thefile.datelastmodified & vbnewline & _ 28 "Drive: " & thefile.drive & vbnewline & _ 29 "Size: " & thefile.size & " bytes" & _ 30 vbnewline & _ 31 "Path: " & thefile.path & vbnewline & _ 32 "Short Name: " & thefile.shortname 33 End Sub Initial GUI at execution. Fig. 14.11 Demonstrating the File FSO (part 1 of 2).

CHAPTER 14 SEQUENTIAL FILES 11 GUI after programmer clicks a file. Fig. 14.11 Demonstrating the File FSO (part 2 of 2). Property/method Properties Attributes DateCreated DateLastAccessed DateLastModified Drive IsRootFolder Name ParentFolder Path ShortName ShortPath Size Type Methods Delete Move Copy Integer. Value corresponding to Folder s attributes (read only, hidden, etc.) Date. The date the file was created. Date. The date the file was last accessed. Date. The date the file was last modified. Drive. The Drive where the folder is located. Boolean. Indicates whether or not a Folder is the root folder. String. The Folder s name. String. The Folder s parent folder name. String. The Folder s path. String. The Folder s name expressed as a short name. String. The Folder s path expresses as a short path. Variant. The total size in bytes of all subfolders and files. String. The Folder type. Delete this Folder. Same as FileSystemObject method DeleteFile. Move this Folder. Same as FileSystemObject method MoveFolder. Copy this Folder. Same as FileSystemObject method Copy- Folder. Fig. 14.12 Some Folder properties and methods.

CHAPTER 14 SEQUENTIAL FILES 12 1 ' Fig. 14.13 2 ' Demonstrating the Folder FSO 3 Option Explicit ' General declaration 4 Dim mfso As New FileSystemObject ' General declaration 5 6 Private Sub dirbox_click() 7 Dim f As Folder 8 Fig. 14.13 Using a Folder FSO (part 1 of 2). 9 ' Get a Folder to the selected item 10 Set f = mfso.getfolder(dirbox.list(dirbox.listindex)) 11 12 txtdisplay.text = "" ' Clear TextBox 13 14 ' Test for the root folder 15 If f.isrootfolder = False Then 16 txtdisplay.text = "Root folder: " & f.isrootfolder _ 17 & vbnewline & _ 18 "Parent Folder: " & f.parentfolder & _ 19 vbnewline & "Size: " & f.size 20 Else 21 txtdisplay.text = "Root folder: " & f.isrootfolder 22 End If 23 24 txtdisplay.text = txtdisplay.text & vbnewline & "Type: " & _ 25 f.type & vbnewline & _ 26 "Short Path: " & f.shortpath & _ 27 vbnewline & "Path: " & f.path & _ 28 vbnewline & "Short Name: " & f.shortname 29 End Sub Initial GUI at execution.

CHAPTER 14 SEQUENTIAL FILES 13 Initial GUI at execution. Fig. 14.13 Using a Folder FSO (part 2 of 2).

CHAPTER 14 SEQUENTIAL FILES 14 Property AvailableSpace DriveLetter DriveType FileSystem FreeSpace IsReady Path RootFolder SerialNumber TotalSize VolumeName Variant. The amount of available Drive space in bytes. String. The letter assigned the Drive (e.g., C ). Integer. The Drive type. Constants Unknown, Removable, Fixed, Remote, CDRom and RamDisk represent Drive types and have the values 0-5, respectively. String. The file system Drive description (FAT, FAT32, NTFS, etc.). Variant. Same as AvailableSpace. Boolean. Indicates whether or not a Drive is ready for use. String. The Drive s Path. Folder. The Drive s root Folder. Long. The Drive serial number. Variant. The total Drive size in bytes. String. The Drive volume name. Fig. 14.14 Drive properties. 1 ' Fig. 14.15 2 ' Demonstrating the Drive FSO 3 Option Explicit ' General declaration 4 Dim mfso As New FileSystemObject ' General Declaration 5 6 Private Sub Form_Load() 7 Call drvbox_change ' Call DriveListBox Change 8 End Sub 9 10 Private Sub drvbox_change() 11 Dim d As Drive 12 13 ' Get the Drive and assign it to d 14 Set d = mfso.getdrive(drvbox.list(drvbox.listindex)) 15 16 ' Enable error handler 17 On Error GoTo errhandler 18 lblstats(0).caption = "Letter: " & d.driveletter & _ 19 Space$(3) & "S/N #: " _ 20 & d.serialnumber & Space$(3) & _ 21 "Free space: " & d.freespace / _ 22 1000000 & " MB" 23 lblstats(1).caption = "Type: " & d.drivetype _ 24 & Space$(3) & "File System: " _ 25 & d.filesystem & Space$(3) & _ 26 "Total Size: " & d.totalsize / _ 27 1000000 & " MB" 28 Exit Sub 29 30 errhandler: 31 Call MsgBox(Err., vbcritical, "ERROR") 32 drvbox.drive = drvbox.list(1)

CHAPTER 14 SEQUENTIAL FILES 15 33 Exit Sub 34 End Sub Initial GUI at execution. Fig. 14.15 Using a Drive FSO (part 1 of 2). GUI after user selects e: drive. GUI after user selects a: drive. Fig. 14.15 Using a Drive FSO (part 2 of 2). Property/method Properties AtEndOfLine AtEndOfStream Column Line Methods Close Read ReadAll ReadLine Skip SkipLine Boolean. Specifies if the position is at the end of the line. Boolean. Specifies if the position is at the end of the stream. Long. The current character s position in. Long. The current line number. Closes the TextStream. Reads a specified number of characters from the TextStream. Returns a String. Reads the entire TextStream into a String. Returns a String. Reads a line from a TextStream. Returns a String. Skips a specified number of TextStream characters. Skips a line. Fig. 14.16 TextStream properties and methods (part 1 of 2).

CHAPTER 14 SEQUENTIAL FILES 16 Property/method Write WriteBlankLines WriteLine Writes a String to a TextStream. Writes a specified number of blank lines to a TextStream. Writes a String followed by an end-of-line character to a Text- Stream. Fig. 14.16 TextStream properties and methods (part 2 of 2). 1 ' Fig. 14.17 2 ' Writing to a sequential text file 3 Option Explicit ' General declaration 4 Dim mfilesysobj As New FileSystemObject ' General declaration 5 Dim mfile As File ' General declaration 6 Dim mtxtstream As TextStream ' General declaration 7 8 Private Sub Form_Load() 9 10 ' Create a text file 11 Call mfilesysobj.createtextfile("c:\clients.dat") 12 13 ' Once file is created, reference the file 14 Set mfile = mfilesysobj.getfile("c:\clients.dat") 15 16 ' Open a text stream for writing to the file 17 Set mtxtstream = mfile.openastextstream(forwriting) 18 19 ' Display path in lblfilename 20 lblfilename.caption = mfile.path 21 End Sub Fig. 14.17 Writing data to a text file (part 1 of 3). 22 23 Private Sub cmdwrite_click() 24 25 ' Write the data to the file 26 Call mtxtstream.writeline(mskaccount.text & " " & _ 27 txtname.text & " " & _ 28 txtbalance.text) 29 30 ' Clear MaskEdit and TextBoxes 31 txtname.text = "" 32 txtbalance.text = "" 33 34 ' Set several properties for mskaccount 35 ' using With statement 36 With mskaccount 37.Text = "000" ' Display all zeros in MaskEdit 38.SelStart = 0 ' Start highlighting at position 0 39.SelLength = 3 ' Highlight 3 characters 40.SetFocus ' Transfer focus 41 End With 42 End Sub 43 44 Private Sub Form_Terminate() 45 Call mtxtstream.close ' Close the text stream 46 End Sub

CHAPTER 14 SEQUENTIAL FILES 17 Initial GUI at execution. GUI as programmer is entering account information. Fig. 14.17 Writing data to a text file (part 2 of 3). GUI after user clicks Write. Focus shifts to MaskEdit where the three digits are selected. Fig. 14.17 Writing data to a text file (part 3 of 3). File Open Mode ForReading ForWriting ForAppending Open the file for reading only. Data cannot be written to the file. Open a file for writing. If the file already contains data, the data are truncated all data in the file are discarded. Open a file to allow writing at the end of the file. Fig. 14.18 File open modes.

CHAPTER 14 SEQUENTIAL FILES 18 1 ' Fig. 14.19 2 ' Reading from a sequential text file 3 Option Explicit ' General declaration 4 Dim mfilesysobj As New FileSystemObject ' General declaration 5 Dim mfile As File ' General declaration 6 Dim mtxtstream As TextStream ' General declaration 7 8 Private Sub Form_Load() 9 10 ' Get the file 11 Set mfile = mfilesysobj.getfile("c:\clients.dat") 12 13 ' Open a text stream for writing to the file 14 Set mtxtstream = mfile.openastextstream(forreading) 15 16 ' Display path in lblfilename 17 lblfilename.caption = mfile.path 18 End Sub 19 20 Private Sub cmdread_click() 21 Dim s As String 22 23 On Error GoTo handler ' Set error trap 24 25 ' Read the data 26 s = mtxtstream.readline 27 28 ' Parse String s to get values for MaskEdit 29 ' and TextBoxes 30 Dim mark1 As Integer, mark2 As Integer 31 32 mark1 = 4 ' Location of first space in s 33 34 ' Place only the String portion representing the 35 ' account in the MaskEdit. 36 mskaccount.text = Trim$(Mid$(s, 1, mark1)) 37 38 ' Position to first letter of name 39 mark1 = mark1 + 1 40 41 ' Determine location of second space character and 42 ' add 1 to include the space. 43 mark2 = InStr(mark1, s, " ", vbtextcompare) + 1 44 45 ' Place only the String portion representing the 46 ' name in the TextBox. 47 txtname.text = Trim$(Mid$(s, mark1, mark2 - mark1)) 48 49 ' Place the formatted dollar amount in the TextBox. 50 ' mark2 is positioned at the beginning of the amount. 51 txtbalance.text = Format$(Mid$(s, mark2, Len(s) - mark1), _ 52 "Currency") 53 Exit Sub Fig. 14.19 Reading from a sequential file (part 1 of 2). 54 55 handler: 56 If Err.Number = 62 Then ' EOF error 57 Call mtxtstream.close 58 cmdread.enabled = False 59 lblfilename.caption = ""

CHAPTER 14 SEQUENTIAL FILES 19 60 Else 61 Call MsgBox(Err.) 62 End If 63 64 End Sub Initial GUI at execution. GUI after user clicks the Read button once. GUI when the last set of information is read. Fig. 14.19 Reading from a sequential file (part 2 of 2).

CHAPTER 14 SEQUENTIAL FILES 20 1 ' Fig. 14.20 2 ' Credit inquiry program 3 Option Explicit ' General declaration 4 Dim mfso As New FileSystemObject ' General declaration 5 Dim mtype As Integer ' General declaration 6 Const mcredit = 0, mdebit = 1, mzero = 2 ' General declaration 7 8 Private Sub cmdbutton_click(index As Integer) 9 mtype = Index ' Assign cmdbutton control array index 10 Call openandreadfile ' Open and read file 11 End Sub 12 13 Private Sub openandreadfile() 14 Dim txtstream As TextStream, s As String 15 Dim balance As Currency, pos As Long 16 17 txtdisplay.text = "Accounts:" 18 19 ' Get "clients.dat" and open a TextStream for reading 20 Set txtstream = mfso.getfile("c:\clients.dat"). _ 21 OpenAsTextStream(ForReading) 22 Fig. 14.20 A credit inquiry program (part 1 of 3). 23 ' Loop until end of stream is found 24 Do 25 s = txtstream.readline ' Read one line 26 27 ' Find the position of the second space 28 pos = InStr(InStr(1, s, " ", vbtextcompare) + 1, s, " ", _ 29 vbtextcompare) 30 31 ' Extract the String that contains the balance 32 balance = Trim$(Mid$(s, pos, Len(s) - pos)) 33 34 ' Determine what if anything should be displayed 35 If (mtype = mcredit And balance < 0) Then 36 txtdisplay.text = txtdisplay.text & vbnewline & s 37 ElseIf (mtype = mdebit And balance > 0) Then 38 txtdisplay.text = txtdisplay.text & vbnewline & s 39 ElseIf (mtype = mzero And balance = 0) Then 40 txtdisplay.text = txtdisplay.text & vbnewline & s 41 End If 42 43 Loop While (txtstream.atendofstream = False) 44 45 Call txtstream.close ' Close TextStream 46 End Sub

CHAPTER 14 SEQUENTIAL FILES 21 Initial GUI at execution. GUI after user has pressed the Credit Balances button. GUI after user has pressed the Debit Balances button. Fig. 14.20 A credit inquiry program (part 2 of 3). Initial GUI at execution. Fig. 14.20 A credit inquiry program (part 3 of 3).