實驗8-7
在教學篇例8.7的基礎上增加“修改確定”按鈕以及用於定位記錄的按鈕面板,如圖2.8.5所示,並編寫時間過程。
解題,代碼如下:
先建立標準模塊,代碼是:
Type studtype
iNo As Integer
strName As String * 20
strSex As String * 1
sMark As Single
End Type
再在窗體上建立相應的按鈕等控件,窗體代碼如下:
Dim Student As studtype
Dim Record_No As Integer
Sub Form_Load()
Open "C:\STUDENT.DAT" For Random As #1 Len = Len(Student)
Label1.Caption = LOF(1) / Len(Student)
Close #1
If Label1.Caption <> "0" Then Command3_Click '記錄不爲零,就顯示第一條記錄
End Sub
Sub Command1_Click() '追加記錄
With Student
.iNo = Val(Text1.Text)
.strName = Text2.Text
.strSex = IIf(Option1.Value, "1", "0")
.sMark = Val(Text3.Text)
End With
Open "C:\STUDENT.DAT" For Random As #1 Len = Len(Student)
Record_No = LOF(1) / Len(Student) + 1
Label1.Caption = Record_No
Text4.Text = Record_No '加這句,當場修改記錄就不用手工輸入了
Put #1, Record_No, Student
Close #1
End Sub
Sub Command2_Click() '顯示記錄
Open "C:\STUDENT.DAT" For Random As #1 Len = Len(Student)
Record_No = Val(Text4.Text)
Get #1, Record_No, Student
Text1.Text = Student.iNo
Text2.Text = Student.strName
If Student.strSex = "1" Then
Option1.Value = True
Else
Option2.Value = True
End If
Text3.Text = Student.sMark
Record_No = LOF(1) / Len(Student)
Close #1
End Sub
Private Sub Command3_Click() '第一條記錄
Open "C:\STUDENT.DAT" For Random As #1 Len = Len(Student)
Record_No = 1
Get #1, Record_No, Student
Text1.Text = Student.iNo
Text2.Text = Student.strName
If Student.strSex = "1" Then
Option1.Value = True
Else
Option2.Value = True
End If
Text3.Text = Student.sMark
Text4.Text = "1"
Close #1
End Sub
Private Sub Command4_Click() '下一條記錄
If Record_No = Val(Label1.Caption) Then
MsgBox "已經是最後一條記錄了"
Exit Sub '已經是最後一條記錄就退出。
End If
Open "C:\STUDENT.DAT" For Random As #1 Len = Len(Student)
Record_No = Record_No + 1
Get #1, Record_No, Student
Text1.Text = Student.iNo
Text2.Text = Student.strName
If Student.strSex = "1" Then
Option1.Value = True
Else
Option2.Value = True
End If
Text3.Text = Student.sMark
Text4.Text = Record_No
Close #1
End Sub
Private Sub Command5_Click() '前一條記錄
If Record_No = 1 Then
MsgBox "已經是第一條記錄了"
Exit Sub '已經是第一條記錄就退出。
End If
Open "C:\STUDENT.DAT" For Random As #1 Len = Len(Student)
Record_No = Record_No - 1
Get #1, Record_No, Student
Text1.Text = Student.iNo
Text2.Text = Student.strName
If Student.strSex = "1" Then
Option1.Value = True
Else
Option2.Value = True
End If
Text3.Text = Student.sMark
Text4.Text = Record_No
Close #1
End Sub
Private Sub Command6_Click() '最後一條記錄
Open "C:\STUDENT.DAT" For Random As #1 Len = Len(Student)
Record_No = Val(Label1.Caption)
Get #1, Record_No, Student
Text1.Text = Student.iNo
Text2.Text = Student.strName
If Student.strSex = "1" Then
Option1.Value = True
Else
Option2.Value = True
End If
Text3.Text = Student.sMark
Text4.Text = Label1.Caption
Close #1
End Sub
Sub Command7_Click() '修改記錄
With Student
.iNo = Val(Text1.Text)
.strName = Text2.Text
.strSex = IIf(Option1.Value, "1", "0")
.sMark = Val(Text3.Text)
End With
Open "C:\STUDENT.DAT" For Random As #1 Len = Len(Student)
Record_No = Val(Text4.Text)
Put #1, Record_No, Student
Close #1
End Sub