Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 31st, 2006, 08:22 PM
Authorized User
 
Join Date: Jul 2006
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to naveed77 Send a message via Yahoo to naveed77
Default operation is not allowed when the object is opened

dear readers. plz help me out from the following problem in coding

i have made a form with images when i click on that image then data behind the image comes out on the other form if data is in single row then it can show but if more than one record then it goes using DataGrid. where i then select the required record. but i m facing problem in the following coding while going on the form in the load event. right after 2nd attempt on any image having more than one records. then i get an error

Operation is not allowed when the object is open

here is coding.

Dim db As New ADODB.Connection
Dim rs As New ADODB.Recordset
Public VarPlot As String
Public VarSVY As String

Private Sub Form_Load()
With db 'database connectivity with access

.Provider = "microsoft.jet.oledb.4.0" (The yellow mark comes over here
' .CursorLocation = adUseClient
         .Open App.Path & "\cantonment.mdb"
    End With

db.CursorLocation = adUseClient
If db.State = 0 Then
db.Open
End If

Set rs = db.Execute("select PROP_NO, MANAGED_BY,Class, LandLord,
HOLDER_OF_OCCUPANCY_RIGHTS, PLOT_NO from CANTT where SVY_NO='" & VarSVY & "' and Plot_No='" & VarPlot & "'")
Set DataGrid1.DataSource = rs
DataGrid1.Columns(0).Width = 0
End Sub

plz help me and thnx in advance
 
Old August 1st, 2006, 09:01 AM
Authorized User
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by naveed77
 dear readers. plz help me out from the following problem in coding

i have made a form with images when i click on that image then data behind the image comes out on the other form if data is in single row then it can show but if more than one record then it goes using DataGrid. where i then select the required record. but i m facing problem in the following coding while going on the form in the load event. right after 2nd attempt on any image having more than one records. then i get an error

Operation is not allowed when the object is open

here is coding.

Dim db As New ADODB.Connection
Dim rs As New ADODB.Recordset
Public VarPlot As String
Public VarSVY As String

Private Sub Form_Load()
With db 'database connectivity with access

.Provider = "microsoft.jet.oledb.4.0" (The yellow mark comes over here
' .CursorLocation = adUseClient
         .Open App.Path & "\cantonment.mdb"
    End With

db.CursorLocation = adUseClient
If db.State = 0 Then
db.Open
End If

Set rs = db.Execute("select PROP_NO, MANAGED_BY,Class, LandLord,
HOLDER_OF_OCCUPANCY_RIGHTS, PLOT_NO from CANTT where SVY_NO='" & VarSVY & "' and Plot_No='" & VarPlot & "'")
Set DataGrid1.DataSource = rs
DataGrid1.Columns(0).Width = 0
End Sub

plz help me and thnx in advance

It's because your creating a new database object each time you load the form. The database is opened when you click the first image, when you try to click another image it tries to open the database again but it's already open.

Also, you're opening the database in your with statement, you don't need to check the state of the database outside and open if it's closed, since it's already been opened in your with statement. If you remove that if statement and instead add an if statement before your with block to check if it's open, and close it if it is, then it works.

Code:
if db.state <> 0
  db.close
endif
With db  'database connectivity with access
  .Provider = "microsoft.jet.oledb.4.0"here
  .CursorLocation = adUseClient
  .Open App.Path & "\cantonment.mdb"
End With
...
...
End Sub


 
Old August 1st, 2006, 12:07 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

because db is declared at module level, it is not closed/destroyed when the form unload. you can follow DanRoches suggestion, but that will leave the db open even when the form is unloaded. To avoid this, you can explicitely close the db in the Form Unload event
 
Old August 1st, 2006, 01:56 PM
Authorized User
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by marcostraf
 because db is declared at module level, it is not closed/destroyed when the form unload. you can follow DanRoches suggestion, but that will leave the db open even when the form is unloaded. To avoid this, you can explicitely close the db in the Form Unload event

That is true. I would check the db state in unload event of form and close it if it's opened. Nice catch

 
Old August 2nd, 2006, 09:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:...Nice catch
been there, done that :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Operation is not allowed when the object is closed kingroon Classic ASP Databases 2 February 5th, 2008 10:29 AM
Operation is not allowed when object is opened. dpkbahuguna Beginning VB 6 2 April 3rd, 2006 10:53 PM
Operation is not allowed when the object is closed kah Javascript How-To 2 February 16th, 2005 07:20 AM
operation is not allowed when the object is close shoakat Classic ASP Databases 1 November 28th, 2004 06:16 AM
operation is not allowed when object is closed shoakat Classic ASP Databases 1 November 26th, 2004 12:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.