Random Access Files - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Random Access Files

Description:

Each variable in the program is assigned to a data type. Fixed - Length String variable: Stores character strings of fixed length ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 18
Provided by: fayen
Category:
Tags: access | awww | files | random

less

Transcript and Presenter's Notes

Title: Random Access Files


1
Chapter 9
  • Random Access Files

2
Outline and Objectives
  • Fixed-Length Strings
  • Records
  • Random Access Files

3
Fixed - Length String
  • Each variable in the program is assigned to a
    data type.
  • Fixed - Length String variable Stores character
    strings of fixed length
  • To declare a string of length n use the following
    statement
  • Dim var As String n
  • Note n is a positive integer

4
Example
  • Private Sub cmdGo_Click()
  • Dim city As String 9
  • Illustrate fixed-length strings
  • picOutput.Cls
  • picOutput.Print "123456789"
  • city "San Francisco"
  • picOutput.Print city
  • city "Detroit"
  • picOutput.Print city "MI"
  • picOutput.Print Len(city)
  • End Sub

5
Comparing Strings
  • Care must be taken to compare two fixed-length
    strings of different length.
  • Use the function RTrim to remove the right hand
    spaces from a variable - length or a fixed -
    length string.
  • Example RTrim(city)

6
Records
  • Record is a user defined data type
  • A record is a collection of fields
  • Each field in the record has a name, type and
    length which is the number of spaces allocated to
    it.

7
Syntax of a Record Definition
  • In General Declarations
  • Private Type recordType
  • field-name1 As fieldType1
  • field-name2 As fieldType2
  • End Type

8
Record Declaration
  • Dim record-variable As recordType

9
Example of Record Definition
  • Private Type studentRecord
  • name As String 30
  • idNumber As String 11
  • gpa As Single
  • numOfCredit As Integer
  • End Type

10
To create a record of a specific type
  • Dim student1 As studentRecord

11
Storing Data in a Record
  • student1.name John Smith
  • student1.idNumber 456-78-9012
  • student1.gpa 3.5
  • student1.numOfCredits 124

12
Methods of accessing a File
  • Sequential access
  • Data is accessed in the same order in which it is
    physically stored in the file.
  • Random access
  • Records are accessed directly from the file in
    random order.

13
Random Access Files
  • A Random Access file is usually made up of a
    collection of records.
  • Records are accessed directly from the file in
    random order.
  • Usually is done by using records number in the
    file.

14
Opening a Random Access File
  • One statement is suffices for creating,
    appending, writing and reading a random access
    file.
  • Open filespec For Random As n LenLen(recVar)

15
Example of entering a record into a file
  • Private Sub cmdAddCollege_Click()
  • Write a record into the file COLLEGES.TXT
  • Dim college As collegeData
  • college.nom txtCollege.Text
  • college.state txtState.Text
  • college.yrFounded Val(txtYear.Text)
  • recordNum recordNum 1
  • Put 1, recordNum, college
  • txtCollege.Text ""
  • txtState.Text ""
  • txtYear.Text ""
  • txtCollege.SetFocus
  • End Sub

Entering a record into a file
16
Example of reading a record from a file
  • Private Sub DisplayFile()
  • Dim recordNum As Integer
  • Access the random-access file COLLEGES.TXT
  • Dim college As collegeData
  • Open "COLLEGES.TXT" For Random As 1 Len
    Len(college)
  • picOutput.Print "College", , "State", "Year
    founded"
  • For recordNum 1 To 3
  • Get 1, recordNum, college
  • picOutput.Print college.nom, college.state,
    college.yrFounded
  • Next recordNum
  • Close 1
  • End Sub

To Read a data from a file
17
Summary of Using Random Access Files
  • Do not need to close between placing records into
    them or reading from them
  • Do not need to input the records in any specific
    order
  • To find out the most recent record number in a
    file n, then use the function Loc(n)
  • Each record should have the same length, which is
    any number from 1 to 32767
Write a Comment
User Comments (0)
About PowerShow.com