我用ACCESS 2003创建了一个库(C:\vbls\czryk.mdb),其中建一个用户表(czry),表中有字段用户名(yfm)及密码(mm),现求一段登录用户的身份验证代码用于学习,最好有详细备注说明,谢谢先!!!!!!
[此贴子已经被作者于2006-4-7 9:03:59编辑过]
我这个是SQL数据库的,你看一下吧,大体差不多(其中conn和connstr是在公共模块中定义的):
Option Explicit ' 强制要求显式声明变量
Dim times As Integer '确定登录次数
Private Sub Commok_Click()
Dim sql As String
Dim rs As New ADODB.Recordset
'数据库连接
connStr = "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=机动车与驾驶员档案管理"
On Error GoTo errConnect
If conn.State = adStateOpen Then
conn.Close '如果数据库已打开,则关闭
End If
conn.Open connStr '打开数据库
'判断用户名与密码是否为空
If Trim(Text1.Text) = "" Then
MsgBox "请输入用户名!", vbOKOnly + vbExclamation, "警告"
Text1.SetFocus
Exit Sub
End If
If Trim(Text2.Text) = "" Then
MsgBox "请输入密码!", vbOKOnly + vbExclamation, "警告"
Text2.SetFocus
Exit Sub
End If
'判断输入的用户名是否存在
If Trim(Text1.Text) <> "" Then
sql = "select * from 用户管理表 where username='" & Text1.Text & "'"
rs.Open sql, conn, adOpenKeyset, adLockPessimistic
If rs.EOF = True Then
MsgBox "没有这个用户", vbOKOnly + vbExclamation, "提示"
Text1.SetFocus
Else
'检验密码是否正确
If rs.Fields(1) <> Trim(Text2.Text) Then
MsgBox "密码不正确", vbOKOnly + vbExclamation, "提示"
Text2.SetFocus
Else
Unload Me
Load drivermain
drivermain.Show
Exit Sub
End If
End If
End If
'记录登录次数
times = times + 1
If times = 3 Then
Unload Me
Exit Sub
Else
Text1.Text = ""
Text2.Text = ""
Exit Sub
End If
'错误处理
errConnect:
MsgBox Err.Description, vbOKOnly + vbExclamation, "提示"
End Sub
Private Sub quit_Click()
Unload Me
Exit Sub
End Sub
Private Sub Form_Load()
times = 0 '初始化登录次数
End Sub