Solved

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

Posted on 2004-08-18
7
1,003 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
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 500 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

726 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