Solved

Operation is not allowed when the object is open

Posted on 2010-09-10
4
809 Views
Last Modified: 2013-11-28
Private Sub btnSearch_Click()

Dim objConn As ADODB.Connection
Dim objRST As ADODB.Recordset
Dim strSQL As String
Dim strConn As String
Dim strLicense As String

'Create the ADODB Connection and Recordset objects
Set objConn = CreateObject("ADODB.Connection")
Set objRST = CreateObject("ADODB.Recordset")

'Open the ADODB Connection
strConn = "provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\USER\Desktop\Images\database.mdb;"
strLicense = InputBox("Please enter the license number", "Enter License Number")
strSQL = "Select * from IndexData WHERE Field2 = '" & strLicense & "';"

objConn.Open (strConn)
objConn.Mode = adModeRead
objRST.Open strSQL, objConn, adOpenForwardOnly, adLockOptimistic
objRST.MoveFirst

'Print relevant provider information
While Not objRST.EOF
    Debug.Print objRST.Fields("Field2")
    Debug.Print objRST.Fields("Field1")
    Debug.Print objRST.Fields("Field3")
    Debug.Print objRST.Fields("DOCID")
    Debug.Print objRST.Fields("DISK")
    Debug.Print objRST.Fields("ImagePath")
    objRST.MoveNext
Wend

objRST.Close
objConn.Close
'Release the variables
Set objRST = Nothing
Set objConn = Nothing

End Sub

OBJCONN.MODE is where the error is occuring at. I am very new to VBA and haven't been in a VB class in over 7 yrs so any help would be greatly appreciated!
0
Comment
Question by:Warehouse13
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 33650579
Try swapping the lines:

   objConn.Open (strConn)
   objConn.Mode = adModeRead

to

   objConn.Mode = adModeRead
   objConn.Open (strConn)
0
 
LVL 6

Expert Comment

by:PJBX
ID: 33650710
The error is correct. You can only set this property when the connection is closed. The Mode  property is read/write while the object is closed and read-only while the object is open.

You are opening the connection on the line before. The Mode default is admodeRead, why are you setting that property? Is it needed?

http://msdn.microsoft.com/en-us/library/ms676693%28VS.85%29.aspx
0
 

Author Comment

by:Warehouse13
ID: 33650771
I have no idea. The book told me to...is the book evil?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33650795
>>  ...is the book evil?

If you burn it, and the flames are an eerie shade of red, then I would say yes.  :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

696 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