Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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

Private Sub Command1_Click()

Form2.printReport

End Sub

Im getting error here
mvCn.ConnectionString = strConnectionString
Posted
Updated 7-Feb-17 22:36pm
v2
Comments
Maciej Los 8-Feb-17 4:04am    
VB6? Seems, it's VB.NET...
jhovyn 8-Feb-17 4:08am    
I'm using VB6.0 and SQL

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

EDIT: updated with code

VB
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
 
Share this answer
 
v2
Comments
jhovyn 8-Feb-17 4:11am    
Thank you for your reply @Andi...I already did that but still error exist...Do you have any idea where do i put that closing connection?
Andy Lanng 8-Feb-17 4:52am    
updated
jhovyn 8-Feb-17 20:45pm    
@Andy Lanng
Thank you sir for your help..I followed your code above but when I click the button my crystal report is not displaying or appearing...
Andy Lanng 9-Feb-17 4:10am    
Sounds like the problem may lie elsewhere. Post a new question to get attention. I'm not very good with Crystal so I can offer very limited help :S
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:
VB.NET
'check connection state
If mvCn.State = ConnectionState.Open Then mvCn.Close()
'open connection
mvCn.Open()
 
Share this answer
 
Comments
jhovyn 8-Feb-17 4:24am    
Thank you for your reply @Maciej Los..
Can you please show me where will I put that code in my current program :( ..Im still newbie in programming
jhovyn 8-Feb-17 4:38am    
I've added this code but still error occured :(
If mvCn.State = 1 Then
Public Function printReport()
Dim strConnectionString As String
Dim rs As ADODB.Recordset
Dim strScript As String

strConnectionString = "Provider=SQLOLEDB...." 'Don't show off your connection details
mvCn.ConnectionString = strConnectionString

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

If mvCn.State = 1 Then
mvCn.Close
End If

End Function

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900