这是我做的一个类模块:
Option Explicit
''''''''''用户名''''''''''
Public UserName As String
Public OldUserName As String
Public NewUserName As String
'''''''''密码'''''''''''''''''
Public PassWord As String
Public OldPassWord As String
Public NewPassWord As String
'''''''''描述、备注'''''''''''
Public Purview As String ''''描述
Public ReMark As String ''''备注
Public ValidatePWDCode As String ''''密码验证码
Public IfPassPWDValidate As Boolean ''''是否通过密码验证
Public ErrCount As Integer ''''错误次数
Public OperatorEnumes As OperatorEnume ''''操作级别
'''''''操作级别枚举'''''''''''
Public Enum OperatorEnume
Guest = 0
Administrator = 2603
SuperUserAdministrator = 2756
End Enum
Public ViseLetter As Boolean '''''''签署函
Private blnIfPassNameAndPwd As Boolean ''''是否通过用户名和密码验证(内部使用)
Private Db As New ADO_DBOption.DataBaseOperation
Private Rs As New ADODB.Recordset
Private LinkString As String
Private Const UserName1 = "admin"
Private Const PassWord1 = ""
Private Const TableName As String = "users"
Private FieldName As String
Private GroupBy As String
Private OrderBy As String
Private Condition As String
'''''''''''是否通过用户名和密码验证属性
Public Property Get PassNameAndPwd(ByVal blnPassNameAndPwd As Boolean) As Boolean
PassNameAndPwd = ValidateNameAndPWD(blnPassNameAndPwd)
End Property
'''''''''''''''输入用户名和密码,并验证'''''''''''''''''
Private Function ValidateNameAndPWD( _
ByVal blnPassNameAndPwd As Boolean _
) As Boolean
'''''如果传值通过,则通过,不需再验证''''
If blnPassNameAndPwd = True Then
ValidateNameAndPWD = blnPassNameAndPwd
Exit Function
End If
'''''如果传值未通过,则需验证''''
Dim FrmEx As New FrmExamination
With FrmEx
If blnIfPassNameAndPwd = False Then
.Show 1
blnIfPassNameAndPwd = FrmEx.blnIfPassCheck
Me.ErrCount = .ErrCount
Me.OperatorEnumes = .OperatorEnumes
Me.UserName = .UserName
Me.PassWord = .PassWord
End If
End With
If blnIfPassNameAndPwd = True And Me.ErrCount <= 2 Then
ValidateNameAndPWD = True
Else
ValidateNameAndPWD = False
End If
Set FrmEx = Nothing
End Function
Private Sub Class_Initialize()
blnIfPassNameAndPwd = False
LinkString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Users.mdb;Persist Security Info=False"
FieldName = "*"
Set Rs = Db.GetRecordset(LinkString, UserName, PassWord, TableName, FieldName)
End Sub
Private Sub ShowEditAdministratorWindow( _
ByVal UserName As String, _
ByVal PassWord As String)
With FrmEdit
.Ex.UserName = Me.UserName
.Ex.PassWord = Me.PassWord
.Show
End With
End Sub
Private Sub ShowEditSuperUserWindow()
FrmEditG.Show
End Sub
'''''''''''''验证用户名和密码,返回用户级别''''''''''''
Public Function Check( _
ByVal strUserName As String, _
ByVal strPassWord As String) As Boolean
blnIfPassNameAndPwd = False
Me.UserName = strUserName
Me.PassWord = strPassWord
Me.OperatorEnumes = Guest
If Rs.AbsolutePosition = adPosUnknown Then Exit Function
Rs.MoveFirst
Do Until Rs.EOF
If Me.UserName = Rs.Fields(1) And Me.PassWord = Rs.Fields(2) & "" Then
blnIfPassNameAndPwd = True
Exit Do
Else
End If
Rs.MoveNext
Loop
Rs.MoveFirst
Do Until Rs.EOF
If Me.UserName = Rs.Fields(1) And Me.PassWord = Rs.Fields(2) & "" Then
If Rs.Fields(3) = "管理员" Then
Me.OperatorEnumes = Administrator
ElseIf Rs.Fields(3) = "超级用户" Then
Me.OperatorEnumes = SuperUserAdministrator
Else
Me.OperatorEnumes = Guest
End If
Exit Do
End If
Rs.MoveNext
Loop
Check = blnIfPassNameAndPwd
End Function
''''''''''验证密码''''''''''''
Public Function ValidatePassWord( _
ByVal strPassWord As String, _
ByVal strValidatePWDCode As String _
) As Boolean
If strPassWord = strValidatePWDCode Then
ValidatePassWord = True
Else
ValidatePassWord = False
End If
End Function
Public Sub Management()
On Error Resume Next
If PassNameAndPwd(False) = True Then
If OperatorEnumes = SuperUserAdministrator Then
ShowEditSuperUserWindow
ElseIf OperatorEnumes = Administrator Then
ShowEditAdministratorWindow Me.UserName, Me.PassWord
Else
MsgBox "操作权限不够!", 48
End If
Else
MsgBox "未通过用户和密码验证!", 48
End If
End Sub
Private Sub Class_Terminate()
blnIfPassNameAndPwd = False
End Sub