0 index
1 Databases
2 Random Access
3 Random Access Files
4 User defined TYPES
5 The TYPE statement
6 Description
7 Example
8 Use of record (compound) variables
9 Files (description)
10 OPEN statement
11 PUT statement
12 Syntax:
13 GET statement
14 Syntax:
15 Example:
16 Notes on use of GET and PUT
17 Sample GET procedure
18 Notes:
19 PUT procedure
20 Parallel Arrays
21 Linked Lists
22 Binary tree
23 Indexing
24 Relational databases
25 Sorting algorithms

outline
created using slideshow.cgi by Andy Harris















CSCI N331 Visual Basic: n331/vb16randomaccess
1. Databases
  • Sequential access is used when records with be of differing length
  • Allow more flexibility, but more difficult programming
  • Require programmer to keep tack of the record number
  • Can use custom variable types



































CSCI N331 Visual Basic: n331/vb16randomaccess
2. Random Access
  • Individual pieces of data may be referenced without having to process sequentially all of the information that precedes that data in the file.



































CSCI N331 Visual Basic: n331/vb16randomaccess
3. Random Access Files
  • The simplest is to require that all records in a file are of the same fixed length
  • Using fixed length makes it easy for a program to calculate the exact location of any record relative to the beginning of the file
  • Data can be inserted, updated or deleted and each random-access file will have one of these three access types



































CSCI N331 Visual Basic: n331/vb16randomaccess
4. User defined TYPES
  • A compound variable that combines many standard variable types
  • Usually refers to a RECORD in a database and is also called a RECORD variable
  • Usually fixed length which is ideal for ramdom-access



































CSCI N331 Visual Basic: n331/vb16randomaccess
5. The TYPE statement
  • TYPE newType
        var1 as datatype
        var2 as datatype
        var3 as datatype
    END TYPE
    GLOBAL variable AS newType



































CSCI N331 Visual Basic: n331/vb16randomaccess
6. Description
  • newType is your custom data type
  • Usually ends in TYPE (by our convention)
  • This is NOT a variable. It is a new variable TYPE (like string, integer, etc.)
  • var1, var2, etc. are regular variable names
  • Datatype refers to standard variable types (string, integer, single, etc.)



































CSCI N331 Visual Basic: n331/vb16randomaccess
7. Example
  • Type AddressType
        Name as String * 30
        Phone as String * 15
        Add as String * 50
    End Type
    Global Address as AddressType



































CSCI N331 Visual Basic: n331/vb16randomaccess
8. Use of record (compound) variables
  • Are much like Object Oriented Programming syntax
  • Variable.Record (e.g., Address.Name = "Andy")



































CSCI N331 Visual Basic: n331/vb16randomaccess
9. Files (description)
  • Must be fixed-length records for random-access file
  • More like a CD player. Individual records can be accessed directly
  • Frequently use custom data types



































CSCI N331 Visual Basic: n331/vb16randomaccess
10. OPEN statement
  • OPEN filename for RANDOM AS #filenumber LEN = LEN(RecordVariable)
  • Example:
      OPEN "A:\Address.dat" for Random as #1 LEN = LEN (Address)
  • The same open statement is used for reading, writing, and appending.



































CSCI N331 Visual Basic: n331/vb16randomaccess
11. PUT statement
  • Is used to Copy information from a variable to the file
  • The filename must have been opened with an OPEN for RANDOM command



































CSCI N331 Visual Basic: n331/vb16randomaccess
12. Syntax:
  • PUT # fileNumber, RecordNumber, RecordVariable
  • Example:
    to place the current contents of the address variable to the 4th position in the file:
      PUT #1, 4, Address
  • To place the current contents of the address variable in the position in the file contained in the recNum variable:
      PUT #1, recNum, Address



































CSCI N331 Visual Basic: n331/vb16randomaccess
13. GET statement
  • Identical to the PUT statement except it transfers information FROM the database TO a variable
  • Filename must have been opened with an OPEN for RANDOM command



































CSCI N331 Visual Basic: n331/vb16randomaccess
14. Syntax:
  • GET # FileNumber, RecordNumber, RecordVariable
  • Note: Filename must have been opened with an OPEN for RANDOM command



































CSCI N331 Visual Basic: n331/vb16randomaccess
15. Example:
  • To place the current contents of the 4th position in the file to the address variable
      GET #1, 4, Address



































CSCI N331 Visual Basic: n331/vb16randomaccess
16. Notes on use of GET and PUT
  • Both are frequently put in separate procedures to simplify use
  • Both require an OPEN statement before use and a CLOSE statement after



































CSCI N331 Visual Basic: n331/vb16randomaccess
17. Sample GET procedure
  • (assumes Record variable called Address)
  • Sub GetAddress
       OPEN FileName for Random as #1 Len = Len(Address)
       Get #1, RecNum, Address
       Close #1
       TxtName.text = Address.Name
       TxtPhone.text = Address.Phone
       TxtAdd.text = Address.Add
    End Sub



































CSCI N331 Visual Basic: n331/vb16randomaccess
18. Notes:
  • The previous code assumes access to variables FileName, RecNum and Address (the Record Variable)
  • Notice that we first GOT the variable from the file, then copied it to the form
  • This code would probably be at form-level



































CSCI N331 Visual Basic: n331/vb16randomaccess
19. PUT procedure
  • A similar PUT procedure would:
       Copy the fields from the form objects to the variable
       Open the file, put the variable to the file
       Close the file



































CSCI N331 Visual Basic: n331/vb16randomaccess
20. Parallel Arrays
  • Loads all records into a record array (record variable) or a series of arrays in memory
  • Provides fast execution
  • Loads from the disk only once
  • Only allows a number of records that will fit into memory at one time
  • Information is volatile until saved.



































CSCI N331 Visual Basic: n331/vb16randomaccess
21. Linked Lists
  • Each record contains a field that points to the next record
  • Done by adding extra field(s) in custom variable type
  • There is a Pointer field containing the record number of the next/previous record in the field
  • Allows for convenient sorting, but slow searching
  • Double-linked lists



































CSCI N331 Visual Basic: n331/vb16randomaccess
22. Binary tree



































CSCI N331 Visual Basic: n331/vb16randomaccess
23. Indexing
  • A separate file or array is maintained comparing the record number to the position in a sorted list
  • Various indices can be created sorting by different criteria
  • This can be easily created with sorted list box technique in Visual Basic



































CSCI N331 Visual Basic: n331/vb16randomaccess
24. Relational databases
  • Information is contained in more than one table of data
  • One table may track personnel, another may track payroll
  • Links between tables
  • Allows flexibility



































CSCI N331 Visual Basic: n331/vb16randomaccess
25. Sorting algorithms
  • Bubble sort - slow but unwieldy - easy to write
  • Shell sort - more efficient but more complex
  • Quick sort - among the best overall techniques, uses recursion



































outline

Databases

Random Access

Random Access Files

User defined TYPES

The TYPE statement

Description

Example

Use of record (compound) variables

Files (description)

OPEN statement

PUT statement

Syntax:

GET statement

Syntax:

Example:

Notes on use of GET and PUT

Sample GET procedure

Notes:

PUT procedure

Parallel Arrays

Linked Lists

Binary tree

Indexing

Relational databases

Sorting algorithms