Option Explicit Type personinfo name As String * 40 phone As String * 40 comments As String * 100 End Type Dim gperson As personinfo Dim gfilenum As Integer Dim grecordlen As Long Dim gcurrentrecord As Long Dim glastrecord As Long Private Sub savecurrentrecord() gperson.name = txtname.Text gperson.phone = txtphone.Text gperson.comments = txtcomment.Text Put #gfilenum,gcurrentrecord,gperson End Sub Private Sub showcurrentrecord() Get #gfilenum,gperson txtname.Text = Trim(gperson.name) txtphone.Text = Trim(gperson.phone) txtcomment.Text = Trim(gperson.comments) Me.Caption = "记录" + Str(gcurrentrecord) + "/" + Str(glastrecord) End Sub Private Sub cmddelet_Click() ' Dim dirresult Dim tmpfilenum Dim tmpperson As personinfo Dim recnum As Long Dim tmprecnum As Long If MsgBox("delete this record",vbYesNo) = vbNo Then txtname.SetFocus Exit Sub End If If Dir("phone.tmp") = "phone.tmp" Then Kill "phone.tmp" End If tmpfilenum = FreeFile Open "phone.tmp" For Random As tmpfilenum Len = grecordlen recnum = 1 tmprecnum = 1 Do While recnum < glastrecord + 1 If recnum <> gcurrentrecord Then Get #gfilenum,recnum,tmpperson Put #tmpfilenum,tmprecnum,tmpperson tmprecnum = tmprecnum + 1 End If recnum = recnum + 1 Loop Close gfilenum Kill "phone.dat" Close tmpfilenum Name "phone.tmp" As "phone.dat" gfilenum = FreeFile Open "phone.dat" For Random As gfilenum Len = grecordlen glastrecord = glastrecord - 1 If glastrecord = 0 Then glastrecord = 1 End If If gcurrentrecord > glastrecord Then gcurrentrecord = glastrecord End If Call showcurrentrecord txtname.SetFocus End Sub Private Sub cmdexit_Click() ' Call savecurrentrecord Close #gfilenum End End Sub Private Sub cmdnew_Click() ' Call savecurrentrecord glastrecord = glastrecord + 1 gperson.phone = "" gperson.name = "" gperson.comments = "" Put #gfilenum,glastrecord,gperson gcurrentrecord = glastrecord Call showcurrentrecord txtname.SetFocus End Sub Private Sub cmdnext_Click() If gcurrentrecord = glastrecord Then Beep MsgBox "end of file!",vbExclamation Else Call savecurrentrecord gcurrentrecord = gcurrentrecord + 1 Call showcurrentrecord End If txtname.SetFocus End Sub Private Sub cmdprevIoUs_Click() ' If gcurrentrecord = 1 Then Beep MsgBox "beginning of file!",vbExclamation Else Call savecurrentrecord gcurrentrecord = gcurrentrecord - 1 Call showcurrentrecord End If txtname.SetFocus End Sub Private Sub cmdsearch_Click() ' Dim nsearch As String Dim found As Integer Dim recnum As Long Dim tmpperson As personinfo nsearch = InputBox("search for:","found") If nsearch = "" Then txtname.SetFocus Exit Sub End If nsearch = UCase(nsearch) found = False For recnum = 1 To glastrecord Get #gfilenum,tmpperson If nsearch = UCase(Trim(tmpperson.name)) Then found = True Exit For End If Next If found = True Then Call savecurrentrecord gcurrentrecord = recnum Call showcurrentrecord Else MsgBox "记录" & nsearch & "没找到!" 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 Call showcurrentrecord End Sub