Contents

 

MEM 800 503

 

Homework 9 (Week 9)

(Ref.: “Teach Yourself Visual Basic in 21 Days 5th Edition” Nathan Gurewich)

 

The phone exectuable file.

 

THE PHONE PROGRAM (CODE)

 

Option Explicit

Dim gPerson As PersonInfo

Dim gFileNum As Integer

Dim gRecordLen As Long

Dim gCurrentRecord As Long

Dim gLastRecord As Long

 

Private Sub cmdExit_Click()

SaveCurrentRecord

 

Close #gFileNum

 

End

 

End Sub

 

Private Sub cmdNew_Click()

 

SaveCurrentRecord

 

gLastRecord = gLastRecord + 1

gPerson.Name = ""

gPerson.Phone = ""

gPerson.Comments = ""

 

Put #gFileNum, gLastRecord, gPerson

 

gCurrentRecord = gLastRecord

 

ShowCurrentRecord

 

txtName.SetFocus

 

End Sub

 

Private Sub cmdNext_Click()

 

If gCurrentRecord = gLastRecord Then

  Beep

  MsgBox "End of file!", vbExclamation

Else

  SaveCurrentRecord

  gCurrentRecord = gCurrentRecord + 1

  ShowCurrentRecord

 End If

 

 txtName.SetFocus

  

End Sub

 

Private Sub cmdPrevious_Click()

 

If gCurrentRecord = 1 Then

 Beep

 MsgBox "Beginning of file!", vbExclamation

Else

 SaveCurrentRecord

 gCurrentRecord = gCurrentRecord - 1

 ShowCurrentRecord

End If

 

txtName.SetFocus

 

End Sub

 

Private Sub Form_Load()

 

gRecordLen = Len(gPerson)

 

gFileNum = FreeFile

 

Open "phone.dat" For Random As gFileNum Len = gRecordLen

gCurrentRecord = 1

 

gLastRecord = FileLen("phone.dat") / gRecordLen

 

If gLastRecord = 0 Then

  gLastRecord = 1

 End If

 

 ShowCurrentRecord

 

 

End Sub

 

Public Sub SaveCurrentRecord()

 

gPerson.Name = txtName.Text

gPerson.Phone = txtPhone.Text

gPerson.Comments = txtComments.Text

 

Put #gFileNum, gCurrentRecord, gPerson

 

End Sub

 

Public Sub ShowCurrentRecord()

 

Get #gFileNum, gCurrentRecord, gPerson

 

txtName.Text = Trim(gPerson.Name)

txtPhone.Text = Trim(gPerson.Phone)

txtComments.Text = Trim(gPerson.Comments)

 

frmPhone.Caption = "Record " + Str(gCurrentRecord) + "/" + Str(gLastRecord)

 

End Sub