对象变量或with未设置,这怎么改?

yuzhifu1 2009-10-17 02:53:08
提示:实时报错“91”,对象变量或with未设置,这怎么改?谢谢!
Private Sub cmdOK_Click()
Dim txtSQL As String
Dim rstLogin As ADODB.Recordset
Dim flag As String
gUserName = ""
gUserKind = ""
If Trim(txtUserName.Text = "") Then
MsgBox "用户名不能为空!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
Else
txtSQL = "select * from tbUser where UserId = '" & txtUserName.Text & "'"
flag = ExecuteSQL(txtSQL, rstLogin, False)
If rstLogin.EOF = True Then
MsgBox "没有这个用户,请重新输入!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
Else
If Trim(rstLogin.Fields(1)) = Trim(txtPassword.Text) Then
gUserName = Trim(txtUserName.Text)
gUserKind = Trim(rstLogin.Fields(2))
gLoginSucceeded = True
rstLogin.Close
Me.Hide
frmMain.Show
Else
MsgBox "密码不正确,请重新输入!", vbOKOnly + vbExclamation, "警告"
txtPassword.SetFocus
txtPassword.Text = ""
End If
End If
End If
miCount = miCount + 1
If miCount = 3 Then
Me.Hide
Exit Sub
End If
End Sub
...全文
128 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
icon_wind 2009-10-18
  • 打赏
  • 举报
回复
jf
贫下码农 2009-10-17
  • 打赏
  • 举报
回复
从代码表面上看,应该是这几句中的某一句出错吧:

If rstLogin.EOF = True Then

If Trim(rstLogin.Fields(1)) = Trim(txtPassword.Text) Then

gUserKind = Trim(rstLogin.Fields(2))

rstLogin.Close

实时报错“91”,对象变量或with未设置,一般是这个 rstLogin 数所集没连接数据或值是 Nothing,当读取调用一个 Nothing 的对象时就会提示这个错误,避免现这种错误最好加上 Try 块处理错误问题及用:
If Not IsNothing(rstLogin) Then


End If
语句防止错误发生。

另外,你还是把这个连接数据库表的函数 ExecuteSQL(txtSQL, rstLogin, False) 的代码码贴出来看看吧,也有可能是这里面的问题!


古今多少事 2009-10-17
  • 打赏
  • 举报
回复
感觉你缺少遍历记录集的语句,如:
For i = 0 To rstLogin.Count - 1
n(i) = Trim(rstLogin.Fields(2))
rstLogin.MoveNext
Next

上面循环中的代码只是个例子。
wjq 2009-10-17
  • 打赏
  • 举报
回复
这是VB6吧……

跟踪到报错的语句,看是不是有变量或者对象没有赋值
yanlongwuhui 2009-10-17
  • 打赏
  • 举报
回复
跟踪下,是否rstLogin为nothing
在flag = ExecuteSQL(txtSQL, rstLogin, False)语句后面应该先判断rstLogin是否为nothing
standhong 2009-10-17
  • 打赏
  • 举报
回复
http://standhong.blog.163.com/edit/
这里面有.但是我的是VS2008+ACCESS 的DEMO.
standhong 2009-10-17
  • 打赏
  • 举报
回复
我怎么没有看到连接数据库的语句.难道你是在其它地方连的?

16,552

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧