VB6 - 对象打开时不允许操作 [英] VB6 - operation is not allowed when the object is open

查看:147
本文介绍了VB6 - 对象打开时不允许操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我收到错误对象打开时不允许操作....这是我的代码



我有什么试过:



表格2

Why am I getting an error "Operation is not allowed when the object is open"....Here is my code

What I have tried:

Form 2

Option Explicit
Public Report As New CrystalReport1
Public mvCn As New ADODB.Connection

     Public Function printReport()

     Dim strConnectionString As String
     Dim rs As ADODB.Recordset
     Dim strScript As String
 
     strConnectionString = "Provider=SQLOLEDB............"
 
     mvCn.ConnectionString = strConnectionString
     mvCn.CommandTimeout = 0
     mvCn.CursorLocation = adUseClient
     mvCn.Open

     strScript = strScript & "SELECT * FROM employee" & vbCrLf

     Set rs = mvCn.Execute(strScript)

     Report.Database.SetDataSource rs
     Report.AutoSetUnboundFieldSource crBMTNameAndValue
    
     CRViewer1.ReportSource = Report
     CRViewer1.ViewReport
    
     Set Report = Nothing

     End Function



表格1。 ....在这里调用我的函数printReport


Form 1.....Call my function "printReport" here

Option Explicit

Private Sub Command1_Click()

Form2.printReport

End Sub



我在这里收到错误


Im getting error here

mvCn.ConnectionString = strConnectionString

推荐答案

但你永远不会关闭连接。



这必须发生在第二个热门的代码。您的连接范围是全局的,因此每次遇到代码时,您都使用相同的实例。第一次运行时,您设置连接字符串并打开连接。第二次运行连接仍然是打开的(非常糟糕)。



您的连接应尽可能在最低范围内。这就是为什么你通常会在'使用'块中看到它。这使得连接范围甚至低于功能本身。



始终关闭连接

始终处置连接



这些不仅仅是指导方针,而是法律! :Þ



希望有所帮助^ _ ^

Andy



< b>编辑:使用代码更新



but you never close the connection.

this must occur on the second hot of the code. Your connection scope is global so each time your code is hit you are using the same instance of it. The first time it runs through, you set the connection string and open the connection. The second time it runs the connection is still open (very bad).

Your connection should be at the lowest scope possible. That's why you will usually see it in a 'using' block. This makes the connection scope even lower that the function itself.

Always close connections
Always dispose of connections

These are't just guidelines, it's the law! :Þ

Hope that helps ^_^
Andy

updated with code

Option Explicit
Public Report As New CrystalReport1

Public Function printReport()
     
     Dim strConnectionString As String
     Dim rs As ADODB.Recordset
     Dim strScript As String
 
     strConnectionString = "Provider=SQLOLEDB............"

     Using mvCn As New ADODB.Connection
        'mvCn does not exist outside of this block!
        mvCn.ConnectionString = strConnectionString
        mvCn.CommandTimeout = 0
        mvCn.CursorLocation = adUseClient
        mvCn.Open

        strScript = strScript & "SELECT * FROM employee" & vbCrLf

        Set rs = mvCn.Execute(strScript)
        mvCn.Close()
        'Always close before leaving scope.  This should happen anyway but it's always good to be sure
     End Using
     'That's it.  mvCn no longer exists.  When the code runs again mvCn will be a new instance

     Report.Database.SetDataSource rs
     Report.AutoSetUnboundFieldSource crBMTNameAndValue
    
     CRViewer1.ReportSource = Report
     CRViewer1.ViewReport
    
     Set Report = Nothing

End Function


错误信息非常清楚。 mvCn 已经打开,因为它没有关闭。你不能再打开它了!

你必须检查连接是否打开:

Error message is quite clear. mvCn is already opened, becuase it wasn't closed. You cannot open it again!
You have to check if connection is open:
'check connection state
If mvCn.State = ConnectionState.Open Then mvCn.Close()
'open connection
mvCn.Open()


这篇关于VB6 - 对象打开时不允许操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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