You are on page 1of 5

ACCESSING DATA FILES

INTRODUCTION

Data comes in many forms. It can be a list of DVDs that is owed and want to keep
track of. That's where data storage comes in. There are many ways to store data for future
use. The most popular and powerful method is to create a database. But that can get quite
involved and it does require a certain amount of analysis knowledge and skill. A much more
accessible method and one which you have certainly used many times before, is to create a
data file. A file is a collection of data on a given subject, stored on a storage medium, usually
a disk or CD.

There are executable files, usually with the .EXE extension, library files (.DLL),
Word document files (.DOC) and a hundred other types. Many applications call for data to be
stored and then read back later for further processing. Think of a simple application: an
Address book to store people's names, addresses and phone numbers. It is possible to create
an Address book database and indeed, it is often the first one to learn how to do in database
courses. However, the task is more suited to data file processing. In some cases one may want
to just to create a form to input names, addresses and phone numbers and then want to store
all the information entered in a file so that one can print it or look-up numbers when needed.

RANDOM FILE ACCESS

Random files are record-based files with an internal structure that supports "direct
access" by record number. This means that your program can read from or write to a specific
record in a random access file, say the 50th record, without reading through the previous 49
records. Compare that to reading or writing a sequential file, where to get to a specific record,
you must read through all preceding records.

The difference between random access and sequential access can be likened to
accessing music on a CD versus a cassette tape. To get to song number 6, you can tell your
CD player to go directly to track 6, whereas on a cassette tape, you must fast-forward through
the first 5 songs to get to song number 6.

1
In the earlier days of BASIC, before the "client-server era" where RAD systems such
as VB, Delphi, and PowerBuilder interacted with desktop and ODBC databases such as MS-
Access, SQL Server, and Oracle, random access files were used as building blocks to put
together data access systems that could be considered early forms of desktop databases.

EXCEPTIONS IN DATA ACCESS

The following conditions may cause an exception:

The path is not valid for one of the following reasons: it is a zero-length string, it
contains only white space, it contains invalid characters, or it is a device path
(ArgumentException).
The path is not valid because it is Nothing (ArgumentNullException).
The file does not exist (FileNotFoundException).
The file is in use by another process or an I/O error occurs (IOException).
The path exceeds the system-defined maximum length (PathTooLongException).
A file or directory name in the path contains a colon (:) or is in an invalid format
(NotSupportedException).
There is not enough memory to write the string to buffer
(OutOfMemoryException).
The user lacks necessary permissions to view the path (SecurityException).

ACCESS METHODS AND EXPLANATION

The Open Statement for Random Access Files

The "full blown" syntax for the Open statement was given in the previous topic on
binary files. The syntax for the Open statement, as it pertains to random files, is as follows:

For reading from the random access file, use:

Open filename For Random Access Read As #filenumber Len = reclength

For writing to the random access file, use:

Open filename For Random Access Write As #filenumber Len = reclength

2
For both reading from and writing to the random access file (for example, you want to access
a particular record and then update one or more of its fields), use:

Open filename For Random Access Read Write As #filenumber Len = reclength

In the syntax formats above, reclength refers to the total length in bytes of all of the
fields (variables) you define as part of a user-defined Type (UDT) structure. The variable that
you base on the UDT serves as the storage facility, or record variable, into which a record
from the random file is read, or from which a record to the random file is written. For
example, the sample program for this topic will use a random access version of the employee
file we used in the topics on sequential files. The Type structure for the employee record will
be defined as follows:

Example:

Private Type EmployeeRecord

EmpName As String * 20

DeptNbr As Integer

JobTitle As String * 25

End Type

The record variable based on this EmployeeRecord Type will be defined as:

Private Type mudtEmpRecord As EmployeeRecord

Note that the String variables that make up this structure are defined as fixed-length
strings. This is important for efficient access of the random file. Given that (in VB6 and
lower), the size of an Integer is 2, the size of a Date is 8, and the size of a Single is 4, the total
length of the structure above is 59 (20 + 2 + 25 + 8 + 4)

Thus, an Open statement for the random employee file could be written as:

Open strRndEmpFileName For Random Access Read Write _

3
As #intRndEmpFileNbr Len = 59

or, even easier and more flexible:

Open strRndEmpFileName For Random Access Read Write _

As #intRndEmpFileNbr Len = Len(mudtEmpRecord)

In the syntax above, the Len function is used on the record variable, thus "letting the
computer do the work" of figuring out the total length of the structure. And, using this
method, if we add fields to or remove fields from the structure, we don't have to worry about
recalculating the total length.

The Get Statement

The Get statement is used read data from a file opened in random mode. The syntax,
as it applies to random files is:

Get [#]filenumber, [recnumber], varname

Recnumber is the record position within the file that is read. The record position is
"one-based", meaning the first record position in the file is 1 and so on.

The Put Statement

The Put statement is used write data to a file opened in random mode. The syntax, as
it applies to binary files is:

Put [#]filenumber, [recnumber], varname

It is possible to omit this entry, in which case the next record following the last Get or
Put statement is read. If you omit the record number entry, you must still include the
delimiting commas in the Get statement, for example:

Get #intRndEmpFileNbr, , mudtEmpRecord

Varname is the record variable into which the data will be read. The record variable is
a variable defined on the UDT record structure as described above.

4
Once the Get statement is executed, the data from the file record can be referenced
using the following syntax: recordname.fieldname

For example, a reference the EmpName field would be coded as:

mudtEmpRecord.EmpName

CONCLUSION

The first step in working with files in Visual Basic is to open the file. This is achieved
using the Visual Basic FileStream class. The FileStream constructor accepts the file name to
be opened as the first parameter, followed by a number of other parameters defining the mode
in which the file is to be opened.

These fall into the categories of FileMode, FileAccess and FileShare.Once a file has
been opened with the appropriate options, it can be written to using the Visual Basic
StreamWriter class. The StreamWriter constructor takes a FileStream as the sole parameter.
A Visual Basic can monitor a file and receive notification from the operating system when
the file is changed by any program. This is achieved using the Visual Basic
FileSystemWatcher class.

REFERENCES

https://msdn.microsoft.com/en-us/library/y32kbeb6.aspx
http://www.techotopia.com/index.php/Working_with_Files_in_Visual_Basic
http://www.vb6.us

You might also like