File and Error Handling

download File and Error Handling

of 69

Transcript of File and Error Handling

  • 7/29/2019 File and Error Handling

    1/69

    -

    Programming in Visual Basic 6.0

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    Update Edition

    Chapter 10

    Data Files

  • 7/29/2019 File and Error Handling

    2/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    2

    -

    Data Files

    Files stored on disk device

    Contain actual data

    Records ==> Rows or lines

    Fields ==> Data elements within row Usually stored in an organized manner

    Sorted by one of the fields

    Key Field, unique data item for each record

  • 7/29/2019 File and Error Handling

    3/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    3

    -

    Sequential File Organization

    Records end with CR, File ends with EOF

    Fields separated by comma delimiters

    Variable length

    Strings enclosed in quotes

    Numbers not enclosed in quotes Read only in the order it was written

    To read a field you must read all preceding fields of all

    preceding records

    "Start at the beginning and continue to the end"

  • 7/29/2019 File and Error Handling

    4/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    4

    -

    Sequential File Example

    "Lynne","Weldon","999 Wide Way","Aiken","SC","29803"

    "Jim","Buck","1 Cow Lane","Aiken","SC","29801" "Tom","Thumb","PO Box 200","Aiken","SC","29802-200"

  • 7/29/2019 File and Error Handling

    5/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    5

    -

    Random File Organization

    Records are fixed length and have a record

    number for reference

    Fields are fixed length and position

    Less data then length will be padded with spaces

    More data than length will be truncated Read or written in any order

    Think of the structure being "like a table"

  • 7/29/2019 File and Error Handling

    6/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    6

    -

    Random File Example

    Lynne Weldon 803-649-9999

    Jim Buckner 803-652-1111

    Tom Thumb 803-593-1234

  • 7/29/2019 File and Error Handling

    7/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    7

    -

    Processing Data Files

    Open file

    Read or Write

    Close file

  • 7/29/2019 File and Error Handling

    8/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    8

    -

    Open Statement

    Usually coded in Form_Load

    Tip: Instead of specifying entire path, always store the sequential

    files in the same directory as your VB Project and use App.Path to

    specify the path

    Open "fully qualified path for filename" For {Input|

    Output|Append|Random} As

    #FileNumber[Len=RecLength]

    { } indicates required - pick one

    [ ] indicates optionalFileNumber = 1 to 511, RecLength Max = 32,767

  • 7/29/2019 File and Error Handling

    9/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    9

    -

    Open For

    Input

    reads a previously stored file from disk Output

    writes file to disk beginning at BOF, overwritingprevious records

    Append Writes file to disk beginning at existing EOF

    Random Reads or writes random files

  • 7/29/2019 File and Error Handling

    10/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    10

    -

    Open Statement Examples

    Sequential

    Open "C:\VB6\myfile.txt" For Input As #6

    Open "A:\myfile.txt" For Output As #1

    Open App.Path & \myfile.txt " For Append As #2

    Random

    Open "A:\myfile.txt" For Random As #3 Len=60

    P i i Vi l B i 6 0 U d t Editi

  • 7/29/2019 File and Error Handling

    11/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    11

    -

    Actions Occurring Upon Open

    Directory is checked for file

    Does not exist - thencreate unless opened for input where itmust already exist

    Buffer in memory established Len determines buffer size

    When buffer is full VB writes data to disk File Pointer is created for current location

    Located at BOF for Input, Output, and Random

    Located at EOF for Append

    File Number established Must be unique while file open

    P i i Vi l B i 6 0 U d t Editi 12

  • 7/29/2019 File and Error Handling

    12/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    12

    -

    Locating a File

    Problem: Open statement requires fully qualified

    path for filename

    Alternative 1: Require that all data files be saved in the same

    directory as the VB project

    Use App.Path to specify the path as follows,Dim strPath as StringstrPath=App.Path & "\Filename.extension"Open strPath for Input as #4

    Alternative 2: Use Common Dialog with ShowOpen method to

    display Open dialog for user to locate file

    P i i Vi l B i 6 0 U d t Editi 13

  • 7/29/2019 File and Error Handling

    13/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    13

    -

    Locating a File-Using Common Dialog

    Dim strFileName as String

    With dlgCommon.DialogTitle = "Open".CancelError = False.Filter = "All Files (*.*)|*.*"

    .ShowOpenIf Len(.FileName) = 0 ThenExit Sub

    End IfstrFileName=.FileName

    End WithOpen strFileName for Input as #1

    P i i Vi l B i 6 0 U d t Editi 14

  • 7/29/2019 File and Error Handling

    14/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    14

    -

    Close Statement

    Terminates processing of a disk file

    Usually coded in Form_Unload,

    mnuFileSave, mnuFileSaveAs, mnuFileExit

    Close [#FileNumber]

    [] indicates optional

    If FileNumber is omitted then all open files are closed

    Programming in Visual Basic 6 0 Update Edition 15

  • 7/29/2019 File and Error Handling

    15/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    15

    -

    Close Statement Examples

    Close #6

    Close #1, #3

    Close

    Programming in Visual Basic 6 0 Update Edition 16

  • 7/29/2019 File and Error Handling

    16/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    16

    -

    Actions Occurring Upon Close

    Physically writes the last partially filled buffer to

    disk for sequential files (Write statement hasplaced data in the buffer)

    Writes EOF

    Releases the Buffer

    Releases the FileNumber

    Note: END closes all open files but is not the best way toclose files. You should close files explicitly!

    Programming in Visual Basic 6 0 Update Edition 17

  • 7/29/2019 File and Error Handling

    17/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    17

    -

    Getting Next Available FileNumber

    FreeFile Function

    System assigns next available number

    Advantages

    Never have FileNumber conflicts

    Don't have to keep track of FileNumbers

    Dim intFileNumber as Integer

    intFileNumber=FreeFile

    Open "Names.txt" For Output As #intFileNumber

  • 7/29/2019 File and Error Handling

    18/69-

    Programming in Visual Basic 6.0

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    Update Edition

    Sequential Files

    Programming in Visual Basic 6 0 Update Edition 19

  • 7/29/2019 File and Error Handling

    19/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    19

    -

    Reading a Sequential File

    Open an existing file for Input

    Use Input Statement to read the records

    Programming in Visual Basic 6 0 Update Edition 20

  • 7/29/2019 File and Error Handling

    20/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    20

    -

    Input Statement

    Usually coded in Form_Load

    If populating a listbox or combo use a Do

    Until EOF to read data

    Input #FileNumber,List of variables

    Input #2,strCourseNum, strClass, intHours

    Programming in Visual Basic 6 0 Update Edition 21

  • 7/29/2019 File and Error Handling

    21/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    21

    -

    Input Statement (cont.)

    If populating a listbox or combo use a

    Do Until EOF to read in Form_Load

    Dim strVideoTitle as String

    Do Until EOF(3)

    Input #3,strVideoTitle

    cboVideoTitle.AddItem strVideoTitleLoop

    Notice numbers match!

    Programming in Visual Basic 6 0 Update Edition 22

  • 7/29/2019 File and Error Handling

    22/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    22

    -

    Write Statement

    Use to place/save data in a sequential file

    File must already be opened "For Output"

    Place code in mnuFileSave, mnuFileCloseor Form_QueryUnload

    Follow with a Close statement

    Programming in Visual Basic 6.0 Update Edition 23

  • 7/29/2019 File and Error Handling

    23/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    23

    -

    Write Statement (cont.)

    Example

    Write #FileNumber,List of variable or fields

    Write #1,txtFirstName, txtLastName,

    txtStreet, txtCity, txtxState, txtPhone

    Write #2,strCourseNum, strClass,

    intHours

    Programming in Visual Basic 6.0 Update Edition 24

  • 7/29/2019 File and Error Handling

    24/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    24

    -

    Write Statement (cont.)

    If populating a listbox or combo use a

    For Next structure to write/save data

    DimintIndexas Integer

    Open "A:\videos.txt" For Output As #1

    ForintIndex=0 tocboVideoTitle.ListCount-1

    Write #1,cboVideoTitle.ListIndexNextintIndex

    Close #1

    Programming in Visual Basic 6.0 Update Edition 25

  • 7/29/2019 File and Error Handling

    25/69

    Programming in Visual Basic 6.0 Update Edition

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    25

    -

    Deleting a Sequential File

    Kill statement

    File cannot be open when Kill is used

    Kill "fully qualified path for filename"

    Kill "A:\Names.txt"

    Programming in Visual Basic 6.0 Update Edition 26

  • 7/29/2019 File and Error Handling

    26/69

    g g p

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    26

    -

    Renaming a Sequential File

    Name statement

    File cannot be open when Name is used

    Name "oldfully qualified path for filename"

    As "newfully qualified path for filename"

    Name "A:\Names.txt" As "A:\People.txt"

    Programming in Visual Basic 6.0 Update Edition 27

  • 7/29/2019 File and Error Handling

    27/69

    g g p

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Updating/Overwriting a Sequential File

    By updating we mean "saving changes made" Open the original file for Input

    Open a temp file for Output

    Input a record from the original into text boxes or other

    controls on a form that the user can change Write out the record to the temp file after the user changes

    the data

    Close both files

    Kill the original file

    Rename the temp file as the original file

    Programming in Visual Basic 6.0 Update Edition 28

  • 7/29/2019 File and Error Handling

    28/69

    g g p

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Inserting Records At the EOF

    Open the file for Append

    Allow the user to enter new data using

    textboxes or other controls on a form

    Write the data once a complete record of

    data has been entered

    Close the file after all new records have

    been inserted

    Programming in Visual Basic 6.0 Update Edition 29

  • 7/29/2019 File and Error Handling

    29/69

    g g p

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Creating a New File

    Open the file for Output

    If the filename does not exist VB will create

    the file

    Write data to the file from textboxes or

    other controls the user enters data into

    Close the file

    Programming in Visual Basic 6.0 Update Edition 30

  • 7/29/2019 File and Error Handling

    30/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Saving Changes to a File

    Standard practice is to ask the user if they want to

    save the changes before program termination Set up a module level Boolean variable to identify if

    changes have been made

    In Form_QueryUnload event

    Check module level Boolean variable to see if changes havebeen made

    If TRUE ask the user if they want to save

    If user responds "yes", save

    Code example p 403

  • 7/29/2019 File and Error Handling

    31/69-

    Programming in Visual Basic 6.0

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    Update Edition

    Trapping Program Errors

    Programming in Visual Basic 6.0 Update Edition 32

  • 7/29/2019 File and Error Handling

    32/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Error Handling

    Trap run-time errors rather than having user

    deal with them or program terminating Some problems cannot be avoided - BUT

    they must be anticipated

    Drive or printer not functioningImproperly formatted disk

    File not found

    Programming in Visual Basic 6.0 Update Edition 33

  • 7/29/2019 File and Error Handling

    33/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Most Common Run-Time Errors

    11 Division by 0

    13 Type mismatch***482 Printer error

    53 File not found

    61 Disk full

    68 Device unavailable

    71 Disk not ready

    75 Path/file access error

    76 Path not found

    Programming in Visual Basic 6.0 Update Edition 34

  • 7/29/2019 File and Error Handling

    34/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    When a run-time error occurs

    VB generates an error number

    VB checks the number against a table of

    known error codes

    Programmer can intercept the error codeand take action before VB terminates the

    project

    Programming in Visual Basic 6.0 Update Edition 35

  • 7/29/2019 File and Error Handling

    35/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Error Trapping Steps

    Turn on the error-handling feature using OnError statement in subprocedure

    Create error handling code routines

    Set them off from other code with line labels

    Write code to continue after the error is"handled"

    Programming in Visual Basic 6.0 Update Edition 36

  • 7/29/2019 File and Error Handling

    36/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    On Error Statement

    Use this statement at the beginning of a

    procedure to activate error trapping Designate a line label in the same procedure

    to go to if an error occurs

    Line labels - begin in column 1, end with colon:

    On Error GoTo ErrorHandlerRefers to

    line label

    Programming in Visual Basic 6.0 Update Edition 37

  • 7/29/2019 File and Error Handling

    37/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Error Handling Code

    Precede with Exit Sub statement (or Exit

    Function) Check the error number(s)

    Single error number -- If structure

    Multiple error numbers -- Select Case

    Inform user if necessary Designate next line of code to execute

    Resume

    Resume Next

    Resume line label

    Programming in Visual Basic 6.0 Update Edition 38

  • 7/29/2019 File and Error Handling

    38/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Resume

    What line of code should be executed after the

    error has been handled?

    Resume - line of code that caused error

    Resume Next - line of code that would logically

    be executed after the line of code that caused error

    Resume line label- line with indicated label

    Programming in Visual Basic 6.0 Update Edition 39

  • 7/29/2019 File and Error Handling

    39/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Err Object

    Intrinsic VB object (like Printer object you used

    in 1st semester VB class) Properties

    Number - error number, 0 to 65,535

    Source - object or application that caused error

    Description

    Method

    Raise - set an error number and/or cause it to occur

    Programming in Visual Basic 6.0 Update Edition 40

  • 7/29/2019 File and Error Handling

    40/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Error Handling Standards

    Use Resume if you identify the problem and the

    user could correct it Use Resume Next if you identify the problem

    and execution can proceed without running the

    error generating line of code

    Raise the error again (Err.Raise Err) if you

    cannot identify the problem so VB will handle it

    and generate a system error message

    Programming in Visual Basic 6.0 Update Edition 41

  • 7/29/2019 File and Error Handling

    41/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Error Handling Standards (cont.)

    Use Resume line labelif you want to exit the

    procedure

    Call you exit procedure (perhaps, mnuFileExit)

    to end without displaying any error message

    Turn off error handling/trapping with:

    On Error GoTo 0

    Programming in Visual Basic 6.0 Update Edition 42

  • 7/29/2019 File and Error Handling

    42/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Handling One Error Number

    Private Sub Whatever( )On Error GoTo ErrorHandler

    code to do whatever this subprocedure does

    Exit Sub

    ErrorHandler:

    If Err.Number=71

    msgbox to inform user of error for

    correction

    Resume

    ElseErr.Raise Err

    End If

    End Sub

    Programming in Visual Basic 6.0 Update Edition 43

  • 7/29/2019 File and Error Handling

    43/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Handling Multiple Error Numbers

    Private Sub Whatever( )

    On Error GoTo ErrorHandlercode to do whatever this subprocedure

    does

    Exit Sub

    ErrorHandler:

    Select Case Err.NumberCase 71

    Msgbox

    Case 53, 76

    Msgbox

    Case Else

    Err.Raise Err

    End Select

    Resume

    End Sub

  • 7/29/2019 File and Error Handling

    44/69

    -

    Programming in Visual Basic 6.0

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    Update Edition

    Random Files

    Programming in Visual Basic 6.0 Update Edition 45

    R d Fil O i i

  • 7/29/2019 File and Error Handling

    45/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Random File Organization

    Records are fixed length and have a record

    number (data type=long integer) for reference

    Fields are fixed length and position

    Less data then length will be padded with spaces More data than length will be truncated

    Read or written in any order

    Think of the structure being "like a table"

    Programming in Visual Basic 6.0 Update Edition 46

    R d Fil E l

  • 7/29/2019 File and Error Handling

    46/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Random File Example

    Lynne Weldon 803-649-9999

    Jim Buckner 803-652-1111

    Tom Thumb 803-593-1234

    Programming in Visual Basic 6.0 Update Edition 47

    D fi i th R d St t

  • 7/29/2019 File and Error Handling

    47/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Defining the Record Structure

    Must be done before reading or writing

    Use Type/End Type statements Code in General Declarations

    Use fixed length strings

    Specify length in Dim statementEx: Dim strFName as String * 20

    Number variables do not require explicit

    length

    Programming in Visual Basic 6.0 Update Edition 48

    T /E d T

  • 7/29/2019 File and Error Handling

    48/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Type/End Type

    Private Type Person

    intEmpNum AsInteger

    strFName AsString* 20

    strLName AsString*30strPhone AsString*12

    curRate AsCurrency

    End Type

    Dim mudtPersonRecord As PersonOpen App.Path & "\Names.dat"For Random as #1

    Len=Len (mudtPersonRecord)

    Note: mudt prefix for User Defined Type

    Programming in Visual Basic 6.0 Update Edition 49

    O St t t

  • 7/29/2019 File and Error Handling

    49/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Open Statement

    Once the random file is opened it can beused for both input and output unlikesequential files!

    If you open a file that does not exist, VBwill create it as an empty file

    Once opened, data are available forread/write operations one record at a time

    Programming in Visual Basic 6.0 Update Edition 50

    R di R d Fil

  • 7/29/2019 File and Error Handling

    50/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Reading a Random File

    Open an existing file for Random

    Use Get Statement to read the records

    Programming in Visual Basic 6.0 Update Edition 51

    G t St t t

  • 7/29/2019 File and Error Handling

    51/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Get Statement

    Usually coded in Form_Load

    If populating a listbox or combo use a Do

    Until reach last record to read data

    Get #FileNumber, [RecordNumber],RecordName

    Get #2, 4, mudtPersonRecordGet #2,intRecordNumber, mudtPersonRecord

    [ ] indicates optional, ifRecordNumber is omitted, the next record is read

    Programming in Visual Basic 6.0 Update Edition 52

    Put Statement

  • 7/29/2019 File and Error Handling

    52/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Put Statement

    Use to place/save data in a random file

    File must already be opened "For Random"

    Place code in mnuFileSave, mnuFileClose

    or Form_QueryUnload

    Follow with a Close statement

    Programming in Visual Basic 6.0 Update Edition 53

    P t St t t E l

  • 7/29/2019 File and Error Handling

    53/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Put Statement Example

    Put #FileNumber, [RecordNumber],RecordName

    Put #2,4, mudtPersonRecord

    Put #2,intRecordNumber, mudtPersonRecord

    [ ] indicates optional, ifRecordNumber is omitted, the next

    record is read

    Programming in Visual Basic 6.0 Update Edition 54

    Accessing the Fields

  • 7/29/2019 File and Error Handling

    54/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Accessing the Fields

    Get and Put operate on an entire record

    Reference the individual fields using dot

    notation for the User Defined Type

    Programming in Visual Basic 6.0 Update Edition 55

    Accessing the Fields ( t )

  • 7/29/2019 File and Error Handling

    55/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Accessing the Fields (cont.)

    Read using Get then update textbox

    Get #2, 1, mudtPersonRecord

    txtLName.Text=mudtPersonRecord.strLName

    Update field from textbox the Write using Put:

    mudtPersonRecord.strPhone=txtPhone.Text

    Put #2, 1, mudtPersonRecord

    Programming in Visual Basic 6.0 Update Edition 56

    LOF Function

  • 7/29/2019 File and Error Handling

    56/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    LOF Function

    Length of File function returns the size of the file

    in bytes Use instead of EOF used for sequential files

    To determine the highest record number in the file

    divide LOF by the size of one record

    LOF(FileNumber)

    LOF(3)

    intNumRecords=LOF(3)/Len(mudtPersonRecord)

    Programming in Visual Basic 6.0 Update Edition 57

    Seek Function

  • 7/29/2019 File and Error Handling

    57/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Seek Function

    Returns the current location of the pointer =

    the next record in the file to be processed

    Seek(FileNumber)

    intNextRecord=Seek(3)

    Programming in Visual Basic 6.0 Update Edition 58

    Trim Functions

  • 7/29/2019 File and Error Handling

    58/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Trim Functions

    Remove extra blank spaces in a string

    Trim ==> removes spaces from both ends LTrim==> removes spaces at left end

    RTrim ==> removes spaces at right end

    Trim(String)

    LTrim(String)

    RTrim(String)

    txtLName.Text=RTrim(mudtPersonRecord.strLName)

    Programming in Visual Basic 6.0 Update Edition 59

    Reading/Retrieving Records Sample Code

  • 7/29/2019 File and Error Handling

    59/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Reading/Retrieving Records Sample Code

    Sub GetRecord(lngRecNum as Long)

    Get #1, lngRecNum, mudtPersonRecordWith mudtPhoneRecordtxtEmpNum.Text = .intEmpNumtxtFName.Text = RTrim(.strFName)txtLName.Text = Rtrim(.strLName)txtPhone.Text = Rtrim(.strPhone)txtRate.Text = .curRate

    End WithEnd Sub

    Programming in Visual Basic 6.0 Update Edition 60

    Writing Records Sample Code

  • 7/29/2019 File and Error Handling

    60/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Writing Records Sample Code

    Sub PutRecord(lngRecNum as Long)

    With mudtPhoneRecord.intEmpNum=Val(txtEmpNum.Text).strFName = txtFName.Text.strLName = txtLName.Text.strPhone = txtPhone.Text.curRate = Val(txtRate.Text)

    End WithPut #1, lngRecNum, mudtPersonRecord

    End Sub

    Programming in Visual Basic 6.0 Update Edition 61

    Using ListBox to Store Random File Key Field (p 413)

  • 7/29/2019 File and Error Handling

    61/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Using ListBox to Store Random File Key Field (p 413)

    When you Get or Put a record you need to

    know the record number To keep track of record numbers store them

    in ItemData property of ListBox

    When user selects a value from the list, readits ItemData property to retrieve the desired

    record

    Programming in Visual Basic 6.0 Update Edition 62

    Updating a Random File

  • 7/29/2019 File and Error Handling

    62/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Updating a Random File

    Create routines to

    Edit existing records

    Add new records

    Delete existing records

    Programming in Visual Basic 6.0 Update Edition 63

    Edit Existing Records

  • 7/29/2019 File and Error Handling

    63/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Edit Existing Records

    Display the record

    Disable all command buttons except Save

    and Cancel

    Lock text boxes for fields containing data

    you do not want the user to modify

    Programming in Visual Basic 6.0 Update Edition 64

    Add New Records

  • 7/29/2019 File and Error Handling

    64/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Add New Records

    Clear all text boxes on form

    Disable all command buttons except Save

    and Cancel

    When user clicks Save write the new record

    at the end of the file

    If using a list box to store record numbers,

    update listbox by adding new data and

    record number

    Programming in Visual Basic 6.0 Update Edition 65

    Delete Existing Records

  • 7/29/2019 File and Error Handling

    65/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    Delete Existing Records

    No way to delete a record in place

    Various methods are used to indicate that a record is to betreated as "deleted"

    Write a special character in an existing field to indicate deleted

    Include a "Delete Code" field in the record description and mark it

    True to indicate deleted

    If using a list box to store record numbers, update listbox

    by deleting the record's associated data and record number

  • 7/29/2019 File and Error Handling

    66/69

    -

    Programming in Visual Basic 6.0

    2002 The McGraw-Hill Companies, Inc. All rights reserved.

    Update Edition

    Programming Hints

    Programming in Visual Basic 6.0 Update Edition 67

    InputBox Function

  • 7/29/2019 File and Error Handling

    67/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    InputBox Function

    Used to request simple input from user

    Similar in syntax and usage to the MsgBoxfunction

    Includes

    prompt to inform user of desired input

    text box for user to enter input

    OK and Cancel command buttons

    Programming in Visual Basic 6.0 Update Edition 68

    InputBox Syntax

  • 7/29/2019 File and Error Handling

    68/69

    2002 The McGraw-Hill Companies, Inc. All rights reserved.-

    InputBox Syntax

    VariableName= InputBox("Prompt"[, "Title"]

    [,Default] [, XPos] [, YPos])

    Where: Prompt - displays in the dialog to inform the user of what to

    input

    Title - displays in the title bar of the dialog

    Default - default value than displays in the text box of the

    dialog XPos - Horizontal position of the top left corner of the dialog

    YPos - Vertical position of the top left corner of the dialog

    Programming in Visual Basic 6.0 Update Edition 69

    InputBox Examples

  • 7/29/2019 File and Error Handling

    69/69

    InputBox Examples

    strName= InputBox ("Enter Your Name")

    intQuan= InputBox ("How many do you want?", _

    "Order Quantity", 1)