代码:
Option Explicit
Private Type stu
sNum As String * 10
sName As String * 20
Score As String * 4
End Type
Dim gstu As stu
Dim recordlen AS Integer
Dim currentrecord As Integer
Dim lastrecord As Integer
Public Sub SaveCurrent()
gstu.sNum = txtNum.Text
gstu.sName = txtName.Text
gstu.Score = txtScore.Text
Put #1, currentrecord, gstu
End sub
Public sub showcurrent()
Get #1,currentrecord,gstu
txtnum.text=gstu.sNum
txtName.text=gstu.sName
txtScore.text=gstu.Score
End sub
Private Sub cmdAdd_Click()
SaveCurrent
lastrecord = lastrecord + 1
gstu.sNum = ""
gstu.sName = ""
gstu.Score = ""
Put #1,lastrecord, gstu
currentrecord = lastrecord
ShowCurrent
txtNum.SetFocus
End Sub
Private Sub cmdFind_Click()
Dim nsearch As String
Dim found As Boolean
Dim recnum As Long
Dim fstu As stu
nsearch = InputBox("请输入要查找的学生的学号: ","查找")
If nsearch = "" Then
Exit Sub
End If
found = False
For recnum = 1 To lastrecord
Get #1, recnum, fstu
If nsearch = Trim(fstu.sNum) Then
found = True
Exit For
End If
Next
If found = True Then
SaveCurrent
currentrecord = recnum
ShowCurrent
Else
MsgBox "无学号为" + nsearch + "的成绩"
End If
End Sub
Private Sub cmdNext_Click()
If currentrecord = lastrecord Then
Beep
MsgBox "已显示完全部成绩!", vbOKOnly + vbExclamation, "错误"
Else
SaveCurrent
currentrecord = currentrecord + 1
ShowCurrent
End If
txtNum.SetFocus
End Sub
Private Sub cmdPrevious_Click()
If currentrecord = 1 Then
Beep
MsgBox "已到文件顶部! ", vbOKOnly + vbExclamation, "错误"
Else
SaveCurrent
currentrecord = currentrecord - 1
ShowCurrent
End If
txtNum.SetFocus
End Sub
Private Sub Form_Load()
recordlen = Len(gstu)
'打开文件,注意文件路径
Open "E:\成绩\11.txt" For Random As #1 Len = recordlen
currentrecord = 1
lastrecord = FileLen("e:\成绩\11.txt") / recordlen
If lastrecord = 0 Then
lastrecord = 1
End If
ShowCurrent
End Sub
Private Sub Form_Unload(Cancel As Integer)
Close #1
End Sub
Public Sub SaveCurrent()
gstu.sNum = txtNum.Text
gstu.sName = txtName.Text
gstu.Score = txtScore.Text
Put #1, currentrecord, gstu
End sub