菜鸟请教问题!

游子 2010-03-11 02:04:20
Private Sub cmdOK_Click()
Dim txtSQL As String
Dim mrc As ADODB.Recordset
Dim MsgText As String
UserName = ""
If Trim(txtUserName.Text = "") Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
Else
txtSQL = "select * from user_Info where user_ID = '" & txtUserName.Text & "'"
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.EOF = True Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
Else
If Trim(mrc.Fields(1)) = Trim(txtPassword.Text) Then
OK = True
mrc.Close
Me.Hide
UserName = Trim(txtUserName.Text)
Else
MsgBox "输入密码不正确,请重新输入!", vbOKOnly + vbExclamation, "警告"
txtPassword.SetFocus
txtPassword.Text = ""
End If
End If
End If

miCount = miCount + 1
If miCount = 3 Then
Me.Hide
End If
Exit Sub
End Sub

上面这段登录代码,运行总提示对象变量或with块变量未设置。。。请教各位,谢谢。如能加我QQ更好:398706948 谢谢
...全文
126 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
gooore 2010-03-12
  • 打赏
  • 举报
回复
如果没有引用,编译是不能通过的,调试也应该不行。

addwatch检查对象是否为空,

gukuang78 2010-03-11
  • 打赏
  • 举报
回复
支持楼上的,先注释掉错误捕获语句,获取错误处理完后,再加上错误捕获语句
贝隆 2010-03-11
  • 打赏
  • 举报
回复
是你的语句有问题,把错误捕获语句去掉,再全编译运行就可以发现错误之处了。
游子 2010-03-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lost_painting 的回复:]
Set mrc = ExecuteSQL(txtSQL, MsgText)

把 ExecuteSQL 贴出来

下断点调试进去.
[/Quote]
select * from user_info where user_ID='admin'
我在数据库里运行 没有问题的啊。。。请教 谢谢。
chinaboyzyq 2010-03-11
  • 打赏
  • 举报
回复
检查控件与控件名以及是否引用了Microsoft ActiveX Data Objects 2.5 Library
king06 2010-03-11
  • 打赏
  • 举报
回复
.........你这代码是人家写好的,没什么问题,看我3楼回复.
游子 2010-03-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bcrun 的回复:]
可能是你这一句
Set mrc = ExecuteSQL(txtSQL, MsgText)
中mrc没被指向可用的对象,建议你在这处设个断点,单步调试看看中间变量值:)
[/Quote]
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset
'executes SQL and returns Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String

On Error GoTo ExecuteSQL_Error

sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & _
" query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic
'rst.MoveLast 'get RecordCount
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & _
" 条记录 "
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function

ExecuteSQL_Error:
MsgString = "查询错误: " & _
Err.Description
Resume ExecuteSQL_Exit
End Function
游子 2010-03-11
  • 打赏
  • 举报
回复
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset
'executes SQL and returns Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String

On Error GoTo ExecuteSQL_Error

sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & _
" query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic
'rst.MoveLast 'get RecordCount
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & _
" 条记录 "
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function

ExecuteSQL_Error:
MsgString = "查询错误: " & _
Err.Description
Resume ExecuteSQL_Exit
End Function
king06 2010-03-11
  • 打赏
  • 举报
回复
查询语句放到数据库中检验一下就知道是什么问题了。
鸭梨山大帝 2010-03-11
  • 打赏
  • 举报
回复
Set mrc = ExecuteSQL(txtSQL, MsgText)

把 ExecuteSQL 贴出来

下断点调试进去.
bcrun 2010-03-11
  • 打赏
  • 举报
回复
可能是你这一句
Set mrc = ExecuteSQL(txtSQL, MsgText)
中mrc没被指向可用的对象,建议你在这处设个断点,单步调试看看中间变量值:)

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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