vb6.0 与access2003 登录界面中需要到数据库中验证输入的用户名(表中有几个账户)
请大家帮忙看看这段代码,我总是找不到错在哪里。刚开始学习。谢谢大家。急,在线等!
数据库:data.mdb
表名:Login
Dim db As Database
Dim rs As Recordset
Private Sub Command1_Click()
Set db = OpenDatabase(App.Path & "\data.mdb")
Set rs = db.OpenRecordset("select 登录姓名,登录密码 from Login where 登录姓名='" & Text1.Text & "'and 登录密码='" & Text2.Text & "")
If Text1.Text <> "" Then
If rs.EOF = True And rs.BOF = True Then
n = MsgBox("密码错误!请重试!", vbExclamation)
Text2.Text = ""
Text2.SetFocus
Else
MsgBox ("登陆成功!")
End If
rs.Close
Else
n = MsgBox("请选择用户名!", vbExclamation)
End If
End Sub
Private Sub Form_Load()
Set db = OpenDatabase(App.Path & "\data.mdb")
Set rs = db.OpenRecordset("select 登录姓名 from Login ") ’此处总是提示错误。(rs=nothing)
Dim i As String
Dim t As Integer
For t = 0 To Val(rs.RecordCount) - 1
i = Trim(rs.Fields("登录姓名").Value)
rs.MoveNext
Text1.AddItem i
Next t
rs.Close
End Sub
[解决办法]
'窗体模块'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'过程功能:进行登录用户合法性检查'功能描述:'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Private Sub picLogin_Click() Dim bolP As Boolean Dim strSql As String Dim lngP As Long Dim cnnP As ADODB.Connection Dim adoP As ADODB.RecordsetOn Error GoTo errSub bolLoginSuccess = False strSql = "Select * From TB_Login Where (FD_UserName ='" & cmbName.Text & "') And (FD_PassWord ='" & txtPWD.Text & "')" If funConnectDataBase(cnnP, adoP, strDB_SystemSettingFilePath, "volvo") Then Call subExcuteSQL(cnnP, adoP, strSql, True) If adoP.EOF Or adoP.BOF Then MessageBox Me.hWnd, "用户登录失败!请检查用户名或密码是否正确!", "系统提示", vbOKOnly + vbExclamation bolP = funCloseDataBase(cnnP, adoP) Exit Sub End If '记录登录用户的信息 With User .Name = adoP!FD_UserName .PWD = adoP!FD_PassWord .LoginTimes = CStr(Val(adoP!FD_LoginTimes) + 1) .Level = adoP!FD_UserLevel End With Else MessageBox Me.hWnd, "执行用户登录失败,打开用户数据表失败!错误描述:" & Err.Description, "系统提示", _ vbOKOnly + vbExclamation End End If bolLoginSuccess = True Unload Me Exit SuberrSub:End Sub'标准模块'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'函数功能:连接指定的数据库'参数说明:cnnP:数据库连接对象;' :adoP:数据集存储对象;' :strPath:数据库路径;' :strPassword:数据库密码;'返回说明:True:连接成功 False:连接失败'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Public Function funConnectDataBase(cnnP As ADODB.Connection, adoP As ADODB.Recordset, ByVal strPath As _ String, ByVal strPassword As String) As BooleanOn Error GoTo errFunction Set cnnP = New ADODB.Connection Set adoP = New ADODB.Recordset cnnP.Provider = "Microsoft.Jet.OLEDB.4.0" cnnP.Open "Data Source = " & strPath & ";jet oledb:database password=" & strPassword funConnectDataBase = True Exit FunctionerrFunction: funConnectDataBase = FalseEnd Function'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'函数功能:关闭数据库连接对象和数据文件的关联'参数说明:cnnP:数据库连接对象;' :adoP:数据库存储对象;'返回说明:True:关闭连接成功 False:关闭连接失败'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Public Function funCloseDataBase(cnnP As ADODB.Connection, adoP As ADODB.Recordset) As BooleanOn Error GoTo errFunction Set adoP = Nothing Set cnnP = Nothing funCloseDataBase = True Exit FunctionerrFunction: funCloseDataBase = FalseEnd Function'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'过程功能:对指定的对象执行指定的SQL语句'参数说明:cnnP:ADO连接对象' :adoP:ADO记录集对象' :strSql:SQL语句' :bolQueryRecord:是否是查询记录集'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Public Sub subExcuteSQL(cnnP As ADODB.Connection, adoP As ADODB.Recordset, strSql As String, bolQueryRecord _ As Boolean) If bolQueryRecord Then '如果是查询记录集 adoP.Open strSql, cnnP, adOpenStatic, adLockBatchOptimistic Else cnnP.Execute strSql End IfEnd Sub'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'过程功能:弹出消息提示框,做出相应的提示'参数说明:hwnd:消息框所有者的句柄;' lpText:消息内容;' lpCaption:消息框标题;' wType:提示符号组合,比如vbYesNo'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hWnd As Long, ByVal lpText As _ String, ByVal lpCaption As String, ByVal wType As Long) As Long
[解决办法]
Dim db As DatabaseDim rs As RecordsetPrivate Sub Command1_Click() Set db = OpenDatabase(App.Path & "\data.mdb") Set rs = db.OpenRecordset("select 登录姓名,登录密码 from Login where 登录姓名 = '" & text1.Text & "' and 登录密码 = '" & Text2.Text & "'")'此处两个双引号中间加一个单引号就可以 If text1.Text <> "" Then If rs.EOF = True And rs.BOF = True Then n = MsgBox("密码错误!请重试!", vbExclamation) Text2.Text = "" Text2.SetFocus Else MsgBox ("登陆成功!")End Ifrs.CloseElse n = MsgBox("请选择用户名!", vbExclamation) End IfEnd SubPrivate Sub Form_Load()Set db = OpenDatabase(App.Path & "\data.mdb")Set rs = db.OpenRecordset("select 登录姓名 from Login ") '此处总是提示错误。(rs=nothing)Dim i As StringDim t As Integer For t = 0 To Val(rs.RecordCount) - 1 i = Trim(rs.Fields("登录姓名").Value) rs.MoveNext text1.AddItem iNext trs.CloseEnd Sub
[解决办法]
Dim db As Database
Dim rs As Recordset
Private Sub Command1_Click()
Set db = OpenDatabase(App.Path & "\data.mdb")
Set rs = db.OpenRecordset("select 登录姓名,登录密码 from Login where 登录姓名 = '" & text1.Text & "' and 登录密码 = '" & Text2.Text & "'")'此处少了一个单引号
If text1.Text <> "" Then
If rs.EOF = True And rs.BOF = True Then
n = MsgBox("密码错误!请重试!", vbExclamation)
Text2.Text = ""
Text2.SetFocus
Else
MsgBox ("登陆成功!")
End If
rs.Close
Else
n = MsgBox("请选择用户名!", vbExclamation)
End If
End Sub
Private Sub Form_Load()
Set db = OpenDatabase(App.Path & "\data.mdb")
Set rs = db.OpenRecordset("select 登录姓名 from Login ")
Dim i As String
Dim t As Integer
For t = 0 To Val(rs.RecordCount) - 1
i = Trim(rs.Fields("登录姓名").Value)
rs.MoveNext
text1.AddItem i
Next t
rs.Close
End Sub