• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1018
  • Last Modified:

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

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
Mike Eghtebas
Asked:
Mike Eghtebas
1 Solution
 
SRigneyCommented:
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
 
alrosCommented:
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
 
ShauliCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ShauliCommented:
...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
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
 
ShauliCommented:
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
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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

Industry Leaders: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now