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.

The two-step procedure for reading data from a record is as follows:<br />

1. Execute the statement<br />

Get #n, r, recVar<br />

to assign record r of file #n to the record variable recVar.<br />

2. Use the field variables of the record variable to either display values with<br />

picBox.Print or to transfer values to other variables with assignment statements.<br />

EXAMPLE 2<br />

The following program displays the entire contents of 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 />

Private Sub cmdDisplay_Click()<br />

Call DisplayFile<br />

End Sub<br />

Private Sub DisplayFile()<br />

Dim recordNum As Integer<br />

‘Access the r<strong>and</strong>om-access file COLLEGES.TXT<br />

Dim college As collegeData<br />

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

picOutput.Cls<br />

picOutput.Print “College”; Tab(30); “State”, Tab(45); “Year founded”<br />

For recordNum = 1 To 3<br />

Get #1, recordNum, college<br />

picOutput.Print college.nom; Tab(30); college.state; _<br />

Tab(45); college.yrFounded<br />

Next recordNum<br />

Close #1<br />

End Sub<br />

[Run, <strong>and</strong> click the comm<strong>and</strong> button.]<br />

The total number of characters in the file with reference number n is given by the value<br />

of the function<br />

LOF(n)<br />

The number of the last record in the file can be calculated by dividing this value by the record<br />

length. The LOF function, rather than the EOF function, should be used to determine when the<br />

end of the file has been reached. For instance, in Example 2, the For statement in the Sub procedure<br />

DisplayFile can be written as<br />

R<strong>and</strong>om-Access Files 253

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

Saved successfully!

Ooh no, something went wrong!