VB6运行时错误3709连接不能用于执行操作 - 它关闭或无效 [英] VB6 run-time error 3709 the connection cannot be used to perform the operation-it is either closed or invalid

查看:208
本文介绍了VB6运行时错误3709连接不能用于执行操作 - 它关闭或无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次运行程序时,我总是会收到此错误。

  Dim  rs  As   ADODB.Recordset 
rs.Open 选择*来自tbluser其中uname ='& txtuser.Text& ',cnn,adOpenStatic,adLockReadOnly
如果 rs.RecordCount< 1 然后
MsgBox 无效的用户名!,vbCritical, 登录错误
txtuser.SetFocus
退出 Sub
否则
如果 txtpass.Text = rs!pword 那么
卸载
加载Form2
Form2.Show
退出 Sub
否则
MsgBox 密码无效!,vbCritical, 登录错误
txtpass.SetFocus
退出 Sub
结束 如果
结束 如果
设置 rs = Nothing



模块代码:

 选项 明确 

公开 cnn 作为 ADODB.Connection
公共 Sub getconnected()
设置 cnn = ADODB.Connection
cnn。 CursorLocation = adUseClient
cnn.ConnectionString = Provider = Microsoft.Jet.OLEDB.4.0; Data Source = & App.Path& \Database1.mdb& ; Persist Security Info = False;

cnn.Open
结束 Sub





突出显示的代码:

 rs.Open  选择*来自tbluser其中uname ='& txtuser.Text&  ',cnn,adOpenStatic,adLockReadOnly 



我刚刚在Dim rs上添加了Module1.getconnected作为新的ADODB.Recordset并且它正在工作,但基于我复制的原始程序,没有Module1.getconnected

解决方案

如果您想检查该用户是否存在并使用 Button1_Click()事件,请尝试以下方法:

 私有  Sub  Button1_Click(...)
Dim cnn As ADODB.Connection
Dim rs 作为 ADODB.Recordset

错误 转到 Err_Handler

' 连接数据库
设置 cnn = ADODB.Connection()
cnn.CursorLocation = adUseClient
cnn.ConnectionString = Provider = Microsoft.Jet.OLEDB.4.0; Data Source =& App.Path& \Database1.mdb& ; Persist Security Info = False;
cnn.Open
' 创建新记录集对象
设置 rs = ADODB.Recordset()
rs.Open 选择*来自tbluser其中uname ='& txtuser.Text& ',cnn,adOpenStatic,adLockReadOnly
如果 rs.RecordCount<> 1 然后
Msgbox 用户数:& rs.RecordCount,vbQuestion, 这怎么可能?
转到 Exit_Sub
结束 如果

' 只有一个用户!!!
' 做你想做的事;)
MsgBox < span class =code-string>欢迎!& vbCr& Herzlich Willkomen!& vbCr& Witaj!,vbInformation, 消息...

Exit_Sub:
开启 错误 恢复 下一步
rs.Close
cnn.Close
Set rs = Nothing
set cnn = Nothing
退出 Sub

Err_Handler:
Msgbox Err.Description,vbExclamation, < span class =code-string>错误& Err.Number
恢复 Exit_Sub
结束


I always get this error everytime I ran the program.

Dim rs As New ADODB.Recordset
rs.Open "Select * From tbluser Where uname = '" & txtuser.Text & "'", cnn, adOpenStatic, adLockReadOnly
If rs.RecordCount < 1 Then
MsgBox "Invalid Username!", vbCritical, "Login Error"
txtuser.SetFocus
Exit Sub
Else
If txtpass.Text = rs!pword Then
Unload Me
Load Form2
Form2.Show
Exit Sub
Else
MsgBox "Invalid Password!", vbCritical, "Login Error"
txtpass.SetFocus
Exit Sub
End If
End If
Set rs = Nothing


MODULE CODE:

Option Explicit

Public cnn As ADODB.Connection
Public Sub getconnected()
  Set cnn = New ADODB.Connection
  cnn.CursorLocation = adUseClient
  cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Database1.mdb" & ";Persist Security Info=False;"
  
  cnn.Open
    End Sub



THE HIGHLIGHTED CODE:

rs.Open "Select * From tbluser Where uname = '" & txtuser.Text & "'", cnn, adOpenStatic, adLockReadOnly


I just added Module1.getconnected on top of Dim rs As New ADODB.Recordset and it''s working but based on the original program that I copied there''s no Module1.getconnected

解决方案

If you want to check that user exists or not and you use Button1_Click() event, try this:

Private Sub Button1_Click(...)
Dim cnn As ADODB.Connection
Dim rs As New ADODB.Recordset

On Error Goto Err_Handler

'connect to database
Set cnn = New ADODB.Connection()
cnn.CursorLocation = adUseClient
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Database1.mdb" & ";Persist Security Info=False;"
cnn.Open
'create new recordset object
Set rs = New ADODB.Recordset()
rs.Open "Select * From tbluser Where uname = '" & txtuser.Text & "'", cnn, adOpenStatic, adLockReadOnly
If rs.RecordCount <> 1 Then
    Msgbox "Users count: " & rs.RecordCount, vbQuestion, "How is this possible?"
    Goto Exit_Sub
End If

'only one user!!!
'do what you want to do ;)
MsgBox "Welcome!" & vbCr & "Herzlich Willkomen!" &  vbCr & "Witaj!", vbInformation, "Message..."

Exit_Sub:
    On Error Resume Next
    rs.Close
    cnn.Close
    Set rs = Nothing
    set cnn = Nothing
    Exit Sub

Err_Handler:
    Msgbox Err.Description, vbExclamation, "Error " & Err.Number
    Resume Exit_Sub
End Sub


这篇关于VB6运行时错误3709连接不能用于执行操作 - 它关闭或无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆