Solved

Operation is not allowed when the object is open

Posted on 2010-09-10
4
801 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 74

Accepted Solution

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

   objConn.Open (strConn)
   objConn.Mode = adModeRead

to

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

Expert Comment

by:PJBX
Comment Utility
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
Comment Utility
I have no idea. The book told me to...is the book evil?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
>>  ...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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
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…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

744 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

17 Experts available now in Live!

Get 1:1 Help Now