?
Solved

Operation is not allowed when the object is open

Posted on 2010-09-10
4
Medium Priority
?
811 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

801 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