tutorial 81 field, record, data file field - a single item of information about a person, place, or...

33
Tutorial 8 1 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain all of the necessary data about a specific person, place, or thing Data file - a collection of related records

Upload: lindsey-stone

Post on 18-Jan-2018

222 views

Category:

Documents


0 download

DESCRIPTION

Tutorial 83 Random vs Sequential Records in a sequential access files are referred to as variable-length records Records in a random access file are referred to as fixed-length records Random access files are called direct access files

TRANSCRIPT

Page 1: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 1

Field, Record, Data File

Field - a single item of information about a person, place, or thing

Record - a group of related fields that contain all of the necessary data about a specific person, place, or thing

Data file - a collection of related records

Page 2: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 2

Random vs SequentialSequential access files are similar to cassette tapes

Random access files are similar to CDs

Each record in a random access file has a unique number, called a record number, that indicates its position in the file

Page 3: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 3

Random vs SequentialRecords in a sequential access files are referred to as variable-length records

Records in a random access file are referred to as fixed-length records

Random access files are called direct access files

Page 4: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 4

Sequential vs Random Access File

“Jackets”,45“Hats”,15“Gloves”,10

Sequential Access:

Random Access: Jackets-Hats Gloves

Page 5: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 5

Type StatementBefore you can create a random access file, you need to use the Type statement to define the file’s record structure

You enter the Type statement in the General declarations section of a code module

Page 6: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 6

Type Statement

Type structurename

fieldname1 As datatype

[fieldname2 As datatype]

[fieldnameN As datatype]

End Type

Option Explicit

Type ItemStruc

strName As String * 7

intPrice As Integer

End Type

Page 7: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 7

Declaring a Record Variable

Dim udtItemRec As ItemStruc

Public udtItemRec As ItemStruc

Page 8: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 8

Referencing a Field VariableTo refer to individual field variables within a record variable, precede the field variable’s name with the name of the record variable in which it is defined. Separate the record variable’s name from the field variable’s name with a period

udtItemRec.strName

udtItemRec.intPrice

Page 9: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 9

Open StatementOpen filename [For mode] As #filenumber Len = reclength

Open “a:\item.dat” For Random As #1 Len = Len(udtItemRec) filename is the name of the file you want to open

filenumber is the number assigned to the file

mode is always random

reclength must be a positive integer between 1 and 32767, inclusive

you calculate the record length by adding together the length of each field in the record variable

Page 10: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 10

Len FunctionLen(variablename)

variablename refers to the record variable’s name

measures the length of the record variable as specified in the Type statement

Page 11: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 11

Close Statement

Close[#filenumber]

filenumber is the number used in the Open statement to open the file

Page 12: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 12

Write # vs PutYou use the Write # statement to write a record to a sequential access file

You use the Put statement to write a record to a random access file

Page 13: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 13

Put StatementPut #filenumber, [recordnumber], variablename

Put #1, 3, udtItemRec

Put #1, intItemNum, udtItemRecfilenumber is the number used in the Open statement to open the filerecordnumber is the number of the record to be writtenvariablename is the name of the record variable

Page 14: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 14

Initializing Random Access FilesAlways initialize a random access file

Always prompt the user to verify that he or she wants to initialize the file

Before you can initialize a file, you must estimate the maximum number of records the file will contain

Page 15: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 15

Space Function

Space(number)strName = Space(20)

number represents the number of spaces you want to assign to the string

Page 16: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 16

GUI Rules

It is customary in Windows applications to prompt the user to verify that he or she wants to proceed with a destructive operation

Display messages to inform the user of the status of important events—such as “File was initialized.” or “File was not initialized.”

Page 17: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 17

Input # vs Get

You use the Input # statement to read a record from a sequential access file

You use the Get statement to read a record from a random access file

Page 18: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 18

Get StatementGet #filenumber, [recordnumber], variablename

Get #1, 3, udtItemRec

Get #1, intItemNum, udtItemRecfilenumber is the number used in the Open statement to open the filerecordnumber is the number of the record to be readvariablename is the name of the record variable

Page 19: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 19

Startup Form

When two or more forms are contained in a project, only one can be the startup form

Use the <application> Properties command on the Project menu to define the startup form for the project

Page 20: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 20

Control ArraysGroup of controls of the same type that have the same name and share the same set of event procedures

Each control in the array is identified by the array’s name and a unique index

The first control in the array has an index of 0

Page 21: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 21

Creating a Control ArrayIf the controls are not already on the form, place the first control on the form and set its name and other propertiesCopy the control to the clipboard, then paste the appropriate number of controls on the formWhen you are asked if you want to create a control array, click the Yes button

To make existing controls into an array, simply assign the same name to each control. When you are asked if you want to create a control array, click the Yes button

Page 22: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 22

Control Array Code Window

•Index As Integer appears in the Code windows for a control array•All controls in the array share the same set of Code windows•Index contains an integer that represents the value stored in the Index property of the control receiving the event

Page 23: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 23

Index Property vs Index Argument

The Index property stores a number that identifies each control in the arrayEach control in the array has a value in its Index propertyThe Index property is like an address

The Index argument found in the Code window is a local variable created by Visual BasicThe Index argument contains the address of the control receiving the event

Page 24: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 24

Call Statement

Call name[(argumentlist)]Call Initialize(dlgSeat.FileName)

In the argumentlist, you enter the information you want to pass to the sub procedure

Page 25: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 25

Intrinsic Color Constants

Page 26: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 26

Examples of Passing InformationCall DoubleNumber(intNum)

Private Sub DoubleNumber(intNum As Integer)

Call Display(strName, intAge)

Private Sub Display(strStudent as String, intNum as Integer)

Call Update(intNum)

Private Sub Update(ByVal intNumber As Integer)

Page 27: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 27

Referencing a Control in Another Form

The period between the form and the control, as well as the period between the control and the property, is called the dot member selection operator

form.controlfrmPatron.lblNum

form.control.propertyfrmPatron.lblNum.Caption

Page 28: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 28

Loading and Displaying a Form

Visual Basic has two statements and two methods that control the loading and displaying of forms.

Load statement

Unload statement

Hide method

Show method

Page 29: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 29

Load and Unload Statements

Load statementBrings a form into memory, but does not display the form on the screen Syntax: Load object

Unload statementRemoves a form from both memory and the screenSyntax Unload object

Page 30: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 30

Show and Hide Methods

Show methodDisplays a form on the screen; loads the form if it is not already in memorySyntax: object.Show [style], where style , which is optional, can be either 0 or 1

Hide methodRemoves a form from the screen, but leaves it in memorySyntax: object.Hide

Page 31: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 31

Style

0 or omitted means that the form is modeless

Example: MSDN Library window

1 means that the form is modal

Example: Visual Basic’s Open Project dialog box

Page 32: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 32

Trim, LTrim, RTrimTrim(string) - removes leading and trailing spaces from a string

LTrim(string) - removes leading spaces from a string

RTrim(string) - removes trailing spaces from a string

Page 33: Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain

Tutorial 8 33

Debugging TechniqueYou can use Visual Basic’s MsgBox statement to assist you when debugging an application

The syntax of the MsgBox statement is MsgBox prompt, where prompt is the message you want displayed in the dialog box

If you use the Err object’s Description property as the prompt, the MsgBox statement will display a message indicating the error that occurred in the procedure