Solved

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

Posted on 2004-08-18
7
984 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 19

Expert Comment

by:Shauli
Comment Utility
...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 33

Author Comment

by:Mike Eghtebas
Comment Utility
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
Comment Utility
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 33

Author Comment

by:Mike Eghtebas
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now