Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Operation is not allowed when the object is open

Posted on 2010-09-10
4
Medium Priority
?
815 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
[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
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

618 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