Adodc控件与SQL数据库连接处理查询数据问题
以下是一个用户登录程序设计代码:
Const MaxLogTimes As Integer = 3
Private Sub cmdCancel_Click()
If MsgBox("你选择了退出系统登录,退出将不能启动管理系统!" & vbCrLf & "是否真的退出?", vbYesNo, "登录验证") = vbYes Then
End
End If
End Sub
Private Sub cmdOK_Click()
Dim strName As String, strPassword As String
'静态常量intLogTimes用于保存用户请求验证的次数
Static intLogTimes As Integer
intLogTimes = intLogTimes + 1 '保存登录次数
If intLogTimes > MaxLogTimes Then
'超过允许的登录次数,显示提示信息
MsgBox "你已经超过允许的登录验证次数!" & vbCr & "应用程序将结束!", vbCritical, "登录验证"
End '结束应用程序
Else
'进一步验证登录信息的合法性
strName = txtUserName.Text
strPassword = txtPassword.Text
'检验用户名和密码的合法性,并根据检验返回值执行相应的操作
Select Case Check_PassWord(strName, strPassword)
Case 0
'用户不是系统用户
MsgBox "用户不是系统用户,请检查用户名输入是否正确!", vbCritical, "登录验证"
txtUserName.SetFocus
txtUserName.SelStart = 0
txtUserName.SelLength = Len(txtUserName)
Case 1
'密码错误
MsgBox "密码错误,请重新输入!", vbCritical, "登录验证"
txtPassword = ""
txtPassword.SetFocus
Case 2
Unload Me '密码正确,卸载登录窗体
MsgBox "登录成功,将启动系统程序!", vbInformation, "登录验证"
FrmManPurchase.Enabled = True
Case Else
'登录验证未正常完成
MsgBox "登录验证未正常完成!请重新运行登录程序," & vbCrLf & "如果仍不能登录,请报告系统管理员!", vbCritical, "登录验证"
End Select
End If
End Sub
Private Function Check_PassWord(ByVal UserName As String, ByVal PassWord As String) As Byte
Adodc1.ConnectionString = "driver={SQL Server};server=MIJK;uid=;pwd=;database=ERP"
Adodc1.RecordSource = "select 密码 from user where 用户名='" & UserName & "'"
'判断有无查询结果
If Adodc1.Recordset.EOF Then
Check_PassWord = 0 '没有查询结果,表示该用户为非法用户
Else
'检查密码是否正确
If PassWord <> Trim(Adodc1.Recordset.Fields("密码").Value) Then
Check_PassWord = 1 '密码不正确
Else
Check_PassWord = 2 '密码正确
End If
End If
End Function
下面是报错的地方:
If Adodc1.Recordset.EOF Then 错误提示:对象变量或 With 块变量没有设置
请问这是怎么回事?
请各位帮忙解决问题!!
问题点数:30、回复次数:10Top
1 楼jobs002(Oh! Office)回复于 2006-03-12 18:28:39 得分 3
以前做的时候出现过这个问题,可一单步调试
Check_PassWord()可能有错Top
2 楼faysky2(出来混,迟早是要还嘀)回复于 2006-03-13 10:13:40 得分 5
加上Refresh就好了:
......
Adodc1.Refresh
If Adodc1.Recordset.EOF Then
......Top
3 楼rayxu(有心就成)回复于 2006-03-13 10:58:43 得分 0
同意楼上的。Top
4 楼rayxu(有心就成)回复于 2006-03-13 11:02:48 得分 0
其实你不用ADO会更好,直接用代码写。
Top
5 楼lihb03(冲天鹰一号)回复于 2006-03-13 13:01:33 得分 0
加上Adodc1.Refresh还是不行,
提示:对象Refresh的方法Adodc1失败Top
6 楼lihb03(冲天鹰一号)回复于 2006-03-13 13:13:02 得分 0
不用ADO怎样写代码啊?Top
7 楼faysky2(出来混,迟早是要还嘀)回复于 2006-03-13 13:21:24 得分 10
出错的可能原因:
1.Adodc1的ConntionString没写对(换种连接字符串试试:"Provider=SQLOLEDB.1;Password=;Persist Security Info=False;User ID=sa;Initial Catalog=ERP;Data Source=MIJK")
2.Adodc1.RecordSource的查询语句没写对(改一下试试:"select 密码 from [user] where 用户名='" & UserName & "'"
)
3.Adodc1的CommandType 属性没设置为adCmdUnknown或adCmdText
这3点都检查,看看Top
8 楼lihb03(冲天鹰一号)回复于 2006-03-14 10:29:53 得分 0
还是不行啊,各位大哥,哪位可以帮忙解决一下这个问题啊?Top
9 楼faysky2(出来混,迟早是要还嘀)回复于 2006-03-14 10:48:19 得分 0
提示的还是 “对象变量或 With 块变量没有设置”错误吗?只提示这个错误吗?
加上了Adodc1.Refresh没有,错误指向哪句呢?Top
10 楼faysky2(出来混,迟早是要还嘀)回复于 2006-03-14 10:51:51 得分 12
Adodc1.RecordSource = "select 密码 from user where 用户名='" & UserName & "'"
-------------------------------
这句有问题,因为user是sql里的关键字,得给它加上[]:
Adodc1.RecordSource = "select 密码 from [user] where 用户名='" & UserName & "'"
Top




