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.

252 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

EXAMPLE 1<br />

The following program creates <strong>and</strong> writes records to the r<strong>and</strong>om-access file COLLEGES.TXT.<br />

`In BAS Module<br />

Public Type collegeData<br />

nom As String * 30 ‘Name of college<br />

state As String * 2 ‘State where college is located<br />

yrFounded As Integer ‘Year college was founded<br />

End Type<br />

’In Form code<br />

Dim recordNum As Integer<br />

Private Sub cmdAddCollege_Click()<br />

‘Write a record into the file COLLEGES.TXT<br />

Dim college As collegeData<br />

college.nom = txtCollege.Text<br />

college.state = txtState.Text<br />

college.yrFounded = Val(txtYear.Text)<br />

recordNum = recordNum + 1<br />

Put #1, recordNum, college<br />

txtCollege.Text = “”<br />

txtState.Text = “”<br />

txtYear.Text = “”<br />

txtCollege.SetFocus<br />

End Sub<br />

Private Sub cmdDone_Click()<br />

Close #1<br />

End<br />

End Sub<br />

Private Sub Form_Load()<br />

‘Create COLLEGES.TXT<br />

Dim college As collegeData<br />

Open “COLLEGES.TXT” For R<strong>and</strong>om As #1 Len = Len(college)<br />

recordNum = 0<br />

End Sub<br />

[Run, <strong>and</strong> type into the text boxes the data shown in the following first window. Click the “Add College<br />

to File” comm<strong>and</strong> button. Record number 1 is added to COLLEGES.TXT <strong>and</strong> the text boxes are cleared.<br />

Proceed to record the data shown for the other two colleges <strong>and</strong> then click the “Done” comm<strong>and</strong> button.]

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

Saved successfully!

Ooh no, something went wrong!