本节将利用文件操作知识设计一个学生基本信息管理系统,该系统实现了对学生记录的添加、删除、修改、查找等操作。系统主界面如图10-6。
学生记录的定义如下:
Private Type StudentInfo
Name As String * 8 '姓名
Birthday As String * 10 '出生年月
Sex As Byte '性别,1表示男,0表示女
Specialty As String * 20 '专业
Remark As String '备注
End Type
其中,备注为变长字符串。为了存储变长记录,不能使用随机文件,因此选择使用二进制文件来存储。
当主窗体Load的时候,将从文件record.data文件中读出所有记录显示在DataGrid中;当单击添加记录或修改记录按钮时,弹出记录添加或记录修改对话框用来输入或修改学生信息;当单击删除记录时,将删除当前选定的记录。具体实现流程如下:
(1)向主窗体中添加1个DataGrid控件和3个按钮控件,排列如图10-6,设置相应的属性。
(2)在Form1中定义如下变量:
Private stuInfo As StudentInfo '学生信息记录
Public g_flag As Integer '2表示添加记录,1表示修改记录
Public adoRecordset As ADODB.Recordset '与DataGrid关联的Recordset对象
(3)当窗体载入时,设置DataGrid的相关属性,从文件中读入记录并显示在DataGrid1中,具体实现如下:
Private Sub Form_Load()
Set adoRecordset = New ADODB.Recordset
'设置DataGrid的列名
adoRecordset.Fields.Append "姓名", adVariant
adoRecordset.Fields.Append "出生年月", adVariant
adoRecordset.Fields.Append "性别", adVariant
adoRecordset.Fields.Append "专业", adVariant
adoRecordset.Fields.Append "备注", adVariant
adoRecordset.Open
'从文件中载入记录
Dim strPath As String
Dim i As Integer
Dim info As StudentInfo
strPath = App.Path + "\record.data"
Open strPath For Binary As #1
Do While Loc(1) < LOF(1)
Get #1, , info
adoRecordset.AddNew
adoRecordset.MoveLast
adoRecordset.Fields(0).Value = info.Name
adoRecordset.Fields(1).Value = info.Birthday
If info.Sex = 1 Then
adoRecordset.Fields(2).Value = "男"
Else
adoRecordset.Fields(2).Value = "女"
End If
adoRecordset.Fields(3).Value = info.Specialty
adoRecordset.Fields(4).Value = info.Remark
Close #1
Set dgShow.DataSource = adoRecordset '绑定 DataGrid 的数据源
dgShow.Columns(2).Width = 500
dgShow.AllowUpdate = False '设定不允许编辑
End Sub
(4)在工程中添加一个Form,命名为DgInfo,界面设计如图10-7。
(5)单击添加记录、修改记录按钮时,将打开DgInfo对话框,具体实现如下:
Private Sub btAdd_Click() ‘添加记录按钮事件
g_flag = 2
DgInfo.Show 1 ‘打开一个有模式的对话框
End Sub
Private Sub btModify_Click() ‘修改记录按钮事件
g_flag = 1
DgInfo.Show 1 ‘打开一个有模式的对话框
End Sub
(6)单击删除记录时,具体实现如下:
Private Sub btDelete_Click()
adoRecordset.Delete '删除记录
adoRecordset.UpdateBatch
End Sub
(7)当主窗体关闭时,删除原文件,将Recordset中的记录逐个写回文件,具体实现如下:
Private Sub Form_Unload(Cancel As Integer)
Dim strPath As String
Dim info As StudentInfo
Dim i As Integer
strPath = App.Path + "\record.data"
Kill strPath '删除原文件
Open strPath For Binary As #1 Len = Len(stuInfo) '打开一个新文件
If adoRecordset.RecordCount > 0 Then
adoRecordset.MoveFirst
'循环将Recordset中记录写入文件
Do While i < adoRecordset.RecordCount
info.Name = adoRecordset.Fields(0).Value
info.Birthday = adoRecordset.Fields(1).Value
If adoRecordset.Fields(2).Value = "男" Then
info.Sex = 1
Else
info.Sex = 0
End If
info.Specialty = adoRecordset.Fields(3).Value
info.Remark = adoRecordset.Fields(4).Value
Put #1, , info
adoRecordset.MoveNext
i = i + 1
Loop
Close #1
End If
End Sub
(8)DgInfo窗体载入的时候,根据是添加记录还是修改记录决定显示样式,处理如下:
Private Sub Form_Load()
If Form1.g_flag = 1 Then
Me.Caption = "修改记录"
txName.Text = Form1.adoRecordset.Fields(0).Value '从Recordset中获取当前记录信息
txBirthday.Text = Form1.adoRecordset.Fields(1).Value
cbSex.Text = Form1.adoRecordset.Fields(2).Value
txSpecial.Text = Form1.adoRecordset.Fields(3).Value
txRemark.Text = Form1.adoRecordset.Fields(4).Value
Else
Me.Caption = "添加记录"
End If
End Sub
(9)DgInfo窗体中,当单击添加或修改时,添加或修改记录,具体实现如下:
Private Sub OKButton_Click()
If Trim(txName.Text) <> "" Then
If Form1.g_flag = 1 Then '修改记录
Form1.adoRecordset.Fields(0).Value = txName.Text
Form1.adoRecordset.Fields(1).Value = txBirthday.Text
Form1.adoRecordset.Fields(2).Value = cbSex.Text
Form1.adoRecordset.Fields(3).Value = txSpecial.Text
Form1.adoRecordset.Fields(4).Value = txRemark.Text
MsgBox "修改记录成功!"
Else '添加记录
Form1.adoRecordset.AddNew
Form1.adoRecordset.MoveLast
Form1.adoRecordset.Fields(0).Value = txName.Text
Form1.adoRecordset.Fields(1).Value = txBirthday.Text
Form1.adoRecordset.Fields(2).Value = cbSex.Text
Form1.adoRecordset.Fields(3).Value = txSpecial.Text
Form1.adoRecordset.Fields(4).Value = txRemark.Text
MsgBox "添加记录成功!"
End If
End If
End Sub