求助:VB运行时错误\"3704\",对象关闭时,不允许操作
这是修改用户密码的代码:PrivateSubCommand1_Click()DimRsAsNewADODB.RecordsetDimsqlAsStringDimsql2A...
这是修改用户密码的代码:
Private Sub Command1_Click()
Dim Rs As New ADODB.Recordset
Dim sql As String
Dim sql2 As String
If Text1.Text = "" Or Text2.Text = "" Or Text3.Text = "" Or Text4.Text = "" Then
MsgBox "输入的信息不完全,请输入完整的信息!", , "提示"
Exit Sub
End If
O
sql = "select * from us where u ='" & Text1.Text & "' and p ='" & Text2.Text & "'"
Rs.Open sql, Cn, adOpenDynamic, adLockPessimistic
If Rs.EOF = True Then
MsgBox "用户密码不匹配,无权更改!", vbOKOnly + vbExclamation, "警告"
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text1.SetFocus
Exit Sub
Else
If Text3.Text <> Text4.Text Then
MsgBox "两次输入的新密码不同!", vbOKOnly, "询问"
Text3.Text = ""
Text4.Text = ""
Text3.SetFocus
Else
sql2 = "select * from us where u ='" & Text1.Text & "'"
Rs!u = Text1.Text
Rs!p = Text3.Text
If Check1.Value = 1 Then
Rs!T = True
Else
Rs!T = False
End If
Rs.Update
cL
MsgBox "密码修改成功!", vbOKOnly, "修改密码"
Me.Hide
End If
End If
End Sub
Public Sub O()
Cn.ConnectionString = "provider=Microsoft.jet.oledb.4.0;data source=" & App.Path & "\db1.mdb;"
Cn.Open
End Sub
Public Sub cL()
Rs.Close
Cn.Close
Set Rs = Nothing
Set Cn = Nothing
End Sub
运行时提示错误, Rs.Close一句变黄。 展开
Private Sub Command1_Click()
Dim Rs As New ADODB.Recordset
Dim sql As String
Dim sql2 As String
If Text1.Text = "" Or Text2.Text = "" Or Text3.Text = "" Or Text4.Text = "" Then
MsgBox "输入的信息不完全,请输入完整的信息!", , "提示"
Exit Sub
End If
O
sql = "select * from us where u ='" & Text1.Text & "' and p ='" & Text2.Text & "'"
Rs.Open sql, Cn, adOpenDynamic, adLockPessimistic
If Rs.EOF = True Then
MsgBox "用户密码不匹配,无权更改!", vbOKOnly + vbExclamation, "警告"
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text1.SetFocus
Exit Sub
Else
If Text3.Text <> Text4.Text Then
MsgBox "两次输入的新密码不同!", vbOKOnly, "询问"
Text3.Text = ""
Text4.Text = ""
Text3.SetFocus
Else
sql2 = "select * from us where u ='" & Text1.Text & "'"
Rs!u = Text1.Text
Rs!p = Text3.Text
If Check1.Value = 1 Then
Rs!T = True
Else
Rs!T = False
End If
Rs.Update
cL
MsgBox "密码修改成功!", vbOKOnly, "修改密码"
Me.Hide
End If
End If
End Sub
Public Sub O()
Cn.ConnectionString = "provider=Microsoft.jet.oledb.4.0;data source=" & App.Path & "\db1.mdb;"
Cn.Open
End Sub
Public Sub cL()
Rs.Close
Cn.Close
Set Rs = Nothing
Set Cn = Nothing
End Sub
运行时提示错误, Rs.Close一句变黄。 展开
4个回答
展开全部
Private Sub Command1_Click() 中问题比较多,问题不在于 cL() 过程:
1、输入数据的判断必须放在打开数据库和数据表之前,我对你的程序修改了一下,应该不存在问题了:
Private Sub Command1_Click()
Dim Rs As New ADODB.Recordset
Dim sql As String
Dim sql2 As String
If Text1.Text = "" Or Text2.Text = "" Or Text3.Text = "" Or Text4.Text = "" Then
MsgBox "输入的信息不完全,请输入完整的信息!", 48, "提示"
Exit Sub
End If
If Text3.Text <> Text4.Text Then '数据输入判断应该在打开数据库之前。
MsgBox "两次输入的新密码不同!", vbOKOnly, "询问"
Text3.Text = ""
Text4.Text = ""
Text3.SetFocus
Exit Sub
End If
Call O '调用打开数据库
sql = "select * from us where u ='" & Text1.Text & "' and p ='" & Text2.Text & "'"
Rs.Open sql, Cn, adOpenDynamic, adLockPessimistic
If Rs.EOF = True Then
MsgBox "用户名或密码不正确,你无权更改!", 16, "警告"
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text1.SetFocus
Call cL ‘在结束过程前必须关闭记录集对象和数据库对象,调用过程必须使用关键字 Call
Exit Sub
Else
Rs!u = Text1.Text
Rs!p = Text3.Text
If Check1.Value = 1 Then
Rs!T = True
Else
Rs!T = False
End If
Rs.Update
Call cL
MsgBox "密码修改成功!", vbOKOnly, "修改密码"
Me.Hide
End If
End Sub
Public Sub O() '数据库调用过程
Cn.ConnectionString = "provider=Microsoft.jet.oledb.4.0;data source=" & App.Path & "\db1.mdb;"
Cn.Open
End Sub
Public Sub cL() '关闭记录集对象和数据库对象过程
Rs.Close
Cn.Close
Set Rs = Nothing
Set Cn = Nothing
End Sub
1、输入数据的判断必须放在打开数据库和数据表之前,我对你的程序修改了一下,应该不存在问题了:
Private Sub Command1_Click()
Dim Rs As New ADODB.Recordset
Dim sql As String
Dim sql2 As String
If Text1.Text = "" Or Text2.Text = "" Or Text3.Text = "" Or Text4.Text = "" Then
MsgBox "输入的信息不完全,请输入完整的信息!", 48, "提示"
Exit Sub
End If
If Text3.Text <> Text4.Text Then '数据输入判断应该在打开数据库之前。
MsgBox "两次输入的新密码不同!", vbOKOnly, "询问"
Text3.Text = ""
Text4.Text = ""
Text3.SetFocus
Exit Sub
End If
Call O '调用打开数据库
sql = "select * from us where u ='" & Text1.Text & "' and p ='" & Text2.Text & "'"
Rs.Open sql, Cn, adOpenDynamic, adLockPessimistic
If Rs.EOF = True Then
MsgBox "用户名或密码不正确,你无权更改!", 16, "警告"
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text1.SetFocus
Call cL ‘在结束过程前必须关闭记录集对象和数据库对象,调用过程必须使用关键字 Call
Exit Sub
Else
Rs!u = Text1.Text
Rs!p = Text3.Text
If Check1.Value = 1 Then
Rs!T = True
Else
Rs!T = False
End If
Rs.Update
Call cL
MsgBox "密码修改成功!", vbOKOnly, "修改密码"
Me.Hide
End If
End Sub
Public Sub O() '数据库调用过程
Cn.ConnectionString = "provider=Microsoft.jet.oledb.4.0;data source=" & App.Path & "\db1.mdb;"
Cn.Open
End Sub
Public Sub cL() '关闭记录集对象和数据库对象过程
Rs.Close
Cn.Close
Set Rs = Nothing
Set Cn = Nothing
End Sub
大雅新科技有限公司
2024-11-19 广告
2024-11-19 广告
这方面更多更全面的信息其实可以找下大雅新。深圳市大雅新科技有限公司从事KVM延长器,DVI延长器,USB延长器,键盘鼠标延长器,双绞线视频传输器,VGA视频双绞线传输器,VGA延长器,VGA视频延长器,DVI KVM 切换器等,优质供应商,...
点击进入详情页
本回答由大雅新科技有限公司提供
展开全部
定义RS为公有变量,也就是在代码最前面定义。然后如下判断
If Rs.State = 1 Then Rs.Close
关闭前先判断是否打开,打开则执行关闭,否则不执行。还有 open也一样 最好先判断是否是关闭状态再打开,如果已经打开了你再open就出错了。
If Rs.State = 1 Then Rs.Close
关闭前先判断是否打开,打开则执行关闭,否则不执行。还有 open也一样 最好先判断是否是关闭状态再打开,如果已经打开了你再open就出错了。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
rs对象作用范围只是在Command1_Click()事件里
和Public Sub cL()
Rs.Close
里的rs对象不是同一个 所以cl里的rs没open却close当然出错
和Public Sub cL()
Rs.Close
里的rs对象不是同一个 所以cl里的rs没open却close当然出错
追问
那应该怎改呢?
追答
Dim Rs As New ADODB.Recordset
放到最外面
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Rs是在Command1里声明的,而Sub cL()里的Rs没有显式的声明,跟Command1的Rs不是一个,因为根本就没打开,所以关闭的时候会出错。
避免这种声明的错误,可以在代码的最前面加上
Option Explicit
强制声明变量的语句,这样可以避免类似上面的这种隐性的错误。
避免这种声明的错误,可以在代码的最前面加上
Option Explicit
强制声明变量的语句,这样可以避免类似上面的这种隐性的错误。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询