【聚杰网VC】VB+Access设计图书管理系统
显示目录
(6) 用户登录子窗体代码
运行的用户登录子窗体如图43所示。

图43 运行的用户登录子窗体
在本项目中,用户登录子窗体是运行的第一个界面,它的作用是检查用户名和密码是否正确。由于用户的资料是存放在数据库中,所以在启动该子窗体时,就已经连接了数据库。其代码如下:
Private Sub Form_Load()
Dim connectionstring As String
connectionstring = "provider=Microsoft.Jet.oledb.4.0;" & _
"data source=book.mdb"
conn.Open connectionstring
cnt = 0
End Sub
“确定”按钮的作用是检查输入的数据是否与数据库中的数据一致。
Private Sub Command1_Click()
Dim sql As String
Dim rs_login As New ADODB.Recordset
If Trim(txtuser.Text) = "" Then ' 判断输入的用户名是否为空
MsgBox "没有这个用户", vbOKOnly + vbExclamation, ""
txtuser.SetFocus
Else
sql = "select * from 系统管理 where 用户名='" & txtuser.Text & "'"
rs_login.Open sql, conn, adOpenKeyset, adLockPessimistic
If rs_login.EOF = True Then
MsgBox "没有这个用户", vbOKOnly + vbExclamation, ""
txtuser.SetFocus
Else ' 检验密码是否正确
用户名和密码通过后,要关闭本窗体并打开主窗体。
If Trim(rs_login.Fields(1)) = Trim(txtpwd.Text) Then
userID = txtuser.Text
userpow = rs_login.Fields(2)
rs_login.Close
Unload Me
MDIForm1.Show
Else
MsgBox "密码不正确", vbOKOnly + vbExclamation, ""
txtpwd.SetFocus
End If
End If
End If
' 只能输入3次
cnt = cnt + 1
If cnt = 3 Then
Unload Me
End If
Exit Sub
End Sub
(7) 值班管理子窗体代码
值班管理子窗体的作用是把值班人员的时间安排形成列表。运行的值班管理子窗体如图44所示。

图44 运行的值班管理子窗体
先定义连接数据库的变量:
Option Explicit
Dim rs_zhiban As New ADODB.Recordset
然后列出窗体部分的代码。
Private Sub cmdadd_Click()
On Error GoTo adderror
If cmdadd.Caption = "新增记录" Then ' 当此按钮的状态为为“增加记录”时
cmdadd.Caption = "确定" ' 按钮名称改“确定”
cmddel.Enabled = False
DataGrid1.AllowAddNew = True
DataGrid1.AllowUpdate = True ' 设定DataGrid可以增加记录
Else
If Not IsNull(DataGrid1.Bookmark) Then
If Not IsDate(Trim(DataGrid1.Columns("值班开始日期").CellText(DataGrid1.Bookmark))) Then
MsgBox "请按照格式yyyy-mm-dd输入值班开始日期", vbOKOnly + vbExclamation, ""
Exit Sub
End If
If Not IsDate(Trim(DataGrid1.Columns("值班开始时间").CellText(DataGrid1.Bookmark))) Then
MsgBox "请按照格式hh-mm输入值班开始时间", vbOKOnly + vbExclamation, ""
Exit Sub
End If
If Not IsDate(Trim(DataGrid1.Columns("值班截止日期").CellText(DataGrid1.Bookmark))) Then
MsgBox "请按照格式yyyy-mm-dd输入值班截止日期", vbOKOnly + vbExclamation, ""
Exit Sub
End If
If Not IsDate(Trim(DataGrid1.Columns("值班截止时间").CellText(DataGrid1.Bookmark))) Then
MsgBox "请按照格式hh-mm输入值班截止时间", vbOKOnly + vbExclamation, ""
Exit Sub
End If
If Trim(DataGrid1.Columns("值班人").CellText(DataGrid1.Bookmark)) = "" Then
MsgBox "值班人不能为空!", vbOKOnly + vbExclamation, ""
Exit Sub
End If
rs_zhiban.Update




