?
Solved

Operation is not allowed when the object is open...VB 6

Posted on 2004-08-18
7
Medium Priority
?
1,007 Views
Last Modified: 2008-03-03
First run works fine,  The second run produces an error (3705).

    .
    .    
    rs.activeconnection = cnConn
    rs.locktype = 3                                <--- Runtime error 3705: Operation is not allowed
    rs.cursortype = 1                                                                 'when the object is open
    rs.cursorlocation = 3
    rs.Open strSQL2
    If rs.RecordCount > 0 Then
    .

----------
When it is called via:
.    
FolderIDVar = fnGetIDVal("FolderID", "tblFolder", "ItemStr", cboFolder)      '<-- this line runs fine
FileIDVar = fnGetIDValLong("FileID", "tblFile", "Folder_Id", FolderIDVar)      '<-- this line, when running fnGetIDValLong has error
.
.


Note: I open stablish the connection (cnConn) to the backend in form load event and close it/ set to nothing up on closing the form.  

Thanks,

Mike
0
Comment
Question by:Mike Eghtebas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 15

Expert Comment

by:SRigney
ID: 11835999
You can only set some of the properties of a recordset when it is closed.  Since you are about to re-open the recordset you should manually close it first.

rs.activeconnection = cnConn
   ' Close the recordset if it is not already closed.
   If Not rs.State = adStateClosed Then
      rs.Close
   End If
    rs.locktype = 3                                
    rs.cursortype = 1
    rs.cursorlocation = 3
    rs.Open strSQL2
    If rs.RecordCount > 0 Then
0
 

Expert Comment

by:alros
ID: 11838264
You are try to assign properties to the recordset which is open. first closed it and then assign properties and then open the recordset.

If rs.state = adstateopen then
   rs.close
end if
rs.locktype = 3
rs.cursortype=1
rs.cursorlocation=3
rs.Open sqlQuery, cnConn
If rs.RecordCount > 0 Then
...
0
 
LVL 19

Accepted Solution

by:
Shauli earned 2000 total points
ID: 11840131
Certain commands need to be set BEFORE you open a recordset. Some of them can be set WHILE you open a recordset. The cursor commands should always be set BEFORE you open a recordset, and the lock type can be set before or while you open a recordset.
Each time you open a recordset, you need to close it when you are done. Otherwise, it is left open. If a recordset is left open, and you try to set a command such as cursor location or lock type, then an error occures, again, simply because the recordset was left open.
The example below demonstrates how to open a recordset, do your stuff and close it when you are done:


    rs.CursorLocation = adUseClient  'set cursor location
    rs.Open strSQL2, cnConn, adOpenKeyset, adLockReadOnly 'open recordset and set locktype at the same time
        With rs
            If Not .EOF Then
                If .RecordCount > 0 Then
                    'do your stuff
                End If
            End If
        End With
    rs.Close    '<<<<<<<<<<<<<<< close when done

Note that if your sql statement DOES NOT contain the WHERE, then you'll have to loop through the records, and then your code would look like:

    rs.CursorLocation = adUseClient  'set cursor location
    rs.Open strSQL2, cnConn, adOpenKeyset, adLockReadOnly 'open recordset and set locktype at the same time
        With rs
            Do Until .EOF
                If .RecordCount > 0 Then
                    'do your stuff - like fill a combobox
                End If
                .MoveNext
            Loop
        End With
    rs.Close    '<<<<<<<<<<<<<<< close when done

S
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Shauli
ID: 11840152
...Note that if your sql statement DOES NOT contain the WHERE, then you'll have to loop through the records, and then your code would look like:...
I meant if your sql statement should bring more than one result :)

S
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 11843042
Thank you for the replies,

SqlStr="Select * MyTable Where ID=" & fnIdVar(cboSomething)

Both "SqlStr" and "fnIdVar(...)" was using the same connection at the same time.  Your description helped me see that.

Note: I am opening one connection to the backend in Form Load event and set it to nothing at Query Unload.

Mike
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11843261
That is the way to go, Mike. You open it upon form load, and close at QueryUnload. Glag it works for you and thanks for the points :)

S
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 11843635
Btw, this is my first VB project ever.  It is amazing how this collective conscoius (EE) heps.  You are very helpfull.

I have been doing Access for over 10 years.  When I just started it, it was very very difficult to get some answers.

Thanks

Mike
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question