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.

Write #2, nom, yob<br />

Else<br />

foundFlag = True<br />

End If<br />

Loop<br />

Close #1<br />

Close #2<br />

Kill “YOB.TXT”<br />

Name “TEMP” As “YOB.TXT”<br />

If Not foundFlag Then<br />

MsgBox “The name was not found.”, , “”<br />

Else<br />

txtName.Text = “”<br />

txtYOB.Text = “”<br />

End If<br />

End Sub<br />

Private Sub DisplayYearOfBirth()<br />

Dim nom As String, yob As Integer<br />

‘Find the year of birth for the name in txtName<br />

txtYOB.Text = “”<br />

Open “YOB.TXT” For Input As #1<br />

nom = “”<br />

Do While (nom txtName.Text) And (Not EOF(1))<br />

Input #1, nom, yob<br />

Loop<br />

If nom = txtName.Text Then<br />

txtYOB.Text = Str(yob)<br />

Else<br />

MsgBox “Person is not in file.”, , “”<br />

txtName.Text = “”<br />

End If<br />

Close #1<br />

End Sub<br />

[Run. After several names have been added, the file might look as shown in Figure 7-1.]<br />

“Barbra”,1942<br />

“Ringo”,1940<br />

“Sylvester”,1946<br />

FIGURE 7-1 Sample Contents of YOB.TXT<br />

■ ERROR TRAPPING<br />

If you try to Open a file on a diskette in drive A <strong>and</strong> there is no diskette in drive A, the program<br />

will crash with the error message “Disk not ready.” <strong>Visual</strong> <strong>Basic</strong> has a device, called<br />

error-trapping, for preventing this <strong>and</strong> many other types of errors. If an error occurs while<br />

error-trapping is active, two things happen. An identifying number is assigned to the Number<br />

property of an object called Err, <strong>and</strong> the program jumps to some lines of code called an errorh<strong>and</strong>ling<br />

routine, which takes corrective measures based on the value of Err.Number. Some<br />

errors <strong>and</strong> the values they generate are as follows:<br />

Sequential Files 221

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

Saved successfully!

Ooh no, something went wrong!