Avatar of Warehouse13
Flag for United States of America

asked on 

Operation is not allowed when the object is open

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

'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")

'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!
Microsoft Access

Avatar of undefined
Last Comment

8/22/2022 - Mon