1.引用Microsoft ActiveX Data Objects 2.5 Library
2.窗体实现 文本框名称分别为:txtUserName txtPassWord
按钮名称分别为: cmdOK cmdCancel
.
3.代码实现
(1).用户名和口令的验证方法Check_PassWord()实现
Private Function Check_PassWord(ByVal UserName As String, _
ByVal PassWord As String) As Byte
On Error GoTo gpError
Dim objCn As New Connection, objRs As New Recordset, _
strCn As String
Dim strSQL As String
'建立数据库连接
objCn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51;" & _
"Data Source=" & App.Path & "\实例01.mdb"
objCn.Open
'执行查询命令,获得用户登录口令
strSQL = "SELECT 口令 FROM 系统用户 WHERE 用户名='" & _
UserName & "'"
Set objRs.ActiveConnection = objCn
objRs.Open (strSQL)
'判断有无查询结果
If objRs.EOF Then
Check_PassWord = 0
Else
'判断口令是否正确
If PassWord <> Trim(objRs.Fields("口令").Value) Then
Check_PassWord = 1
Else
Check_PassWord = 2
End If
End If
'关闭数据库连接,释放对象
objCn.Close
Set objRs = Nothing
Set objCn = Nothing
Exit Function
gpError:
'验证无法正常完成,返回错误代码
Check_PassWord = 255
Set objRs = Nothing
Set objCn = Nothing
End Function
(2).确定按钮实现
Private Sub cmdOK_Click()
Static intLogTimes As Integer
Dim intChecked As Integer, strName As String, _
strPassword As String
intLogTimes = intLogTimes + 1
If intLogTimes > MaxLogTimes Then
'超过允许的登录次数,显示提示信息
MsgBox "您已经超过允许验证次数!" & vbCr & "应用程序结束!", _
vbCritical, "登录验证"
End '结束应用程序
Else
strName = Trim(txtUserName.Text) '获得输入的用户名
strPassword = Trim(txtPassWord.Text) '获得输入的口令
'检查用户名和口令的合法性,并根据检验返回值执行相应的操作
Select Case Check_PassWord(strName, strPassword)
Case 0 '用户不是系统用户
MsgBox "<" & strName & ">不是系统用户,请检查用户名的输 _
入是否正确!", vbCritical, "登录验证"
txtUserName.SetFocus
txtUserName.SelStart = 0
txtUserName.SelLength = Len(txtUserName)
Case 1 '口令错误
MsgBox "口令错误,请重新输入", vbCritical, "登录验证"
txtPassWord.Text = ""
txtPassWord.SetFocus
Case 2 '口令正确
Unload Me
MsgBox "登录成功,将启动系统程序!", vbInformation, "登录验证"
'通常在此放置显示系统主窗体的语句,例如
'frmMain.show
Case Else '登录验证无法正常完成
MsgBox "登录验证未正常完成!请重新运行登录程序,_
" & vbCrLf & "如果仍不能登录,请报告系统管理员!", vbCritical, "登录验证"
End Select
End If
End Sub
(3).取消按钮实现
Private Sub cmdCancel_Click()
Dim intResult As Integer
'请求用户确认是否真的退出系统登录
intResult = MsgBox("您选择了退出系统登录,退出将不能启动管理系统!" & _
vbCr & "是否真的退出?", vbYesNo, "登录验证")
If intResult = vbYes Then End
End Sub