19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4. After all the data have been recorded into the file, close the file with the statement<br />

Close #n.<br />

The Append option for opening a file is intended to add data to an existing file. However, it<br />

also can be used to create a new file. If the file does not exist, then the Append option acts just<br />

like the Output option <strong>and</strong> creates the file.<br />

The three options, Output, Input, <strong>and</strong> Append, are referred to as modes. A file should<br />

not be open in two modes at the same time. For instance, after a file has been opened for output<br />

<strong>and</strong> data have been written to the file, the file should be closed before being opened for<br />

input.<br />

An attempt to open a nonexistent file for input terminates the program with the “File not<br />

found” error message. There is a function that tells us whether a certain file has already been<br />

created. If the value of<br />

Dir(“filespec”)<br />

is the empty string “”, then the specified file does not exist. (If the file exists, the value will<br />

be the file name.) Therefore, prudence often dictates that files be opened for input with code<br />

such as<br />

If Dir(“filespec”) “” Then<br />

Open “filespec” For Input As #1<br />

Else<br />

message = “Either no file has yet been created or ”<br />

message = message & “the file is not where expected.”<br />

MsgBox message, , “File Not Found”<br />

End If<br />

There is one file-management operation that we have yet to discuss—deleting an item of<br />

information from a file. An individual item of a file cannot be changed or deleted directly. A<br />

new file must be created by reading each item from the original file <strong>and</strong> recording it, with<br />

the single item changed or deleted, into the new file. The old file is then erased <strong>and</strong> the new<br />

file renamed with the name of the original file. Regarding these last two tasks, the <strong>Visual</strong><br />

<strong>Basic</strong> statement<br />

Kill “filespec”<br />

removes the specified file from the disk <strong>and</strong> the statement<br />

Name “oldfilespec” As “newfilespec”<br />

changes the filespec of a file. (Note: The Kill <strong>and</strong> Name statements cannot be used with open<br />

files. So doing generates a “File already open” message.)<br />

EXAMPLE 2<br />

The following program creates <strong>and</strong> manages a file of names <strong>and</strong> years of birth.<br />

Private Sub cmdAdd_Click()<br />

Dim message As String<br />

Object Property Setting<br />

frm8_1_2 Caption Access YOB.TXT<br />

lblName Caption Name<br />

txtName Text (blank)<br />

lblYOB Caption Year of Birth<br />

txtYOB Text (blank)<br />

cmdAdd Caption Add Above Person to File<br />

cmdLookUp Caption Look up Year of Birth<br />

cmdDelete Caption Delete Above Person from<br />

File<br />

Sequential Files 219

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!