Operation is not allowed when the object is open

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lester09
    New Member
    • Oct 2015
    • 1

    #1

    Operation is not allowed when the object is open

    have created a form in which i call other forms from the main form ..
    When i click a particular button twice there is an error .. Can anyone help me to solve my problem.. The error is:::
    ERROR 3705
    "Operation is not allowed when the object is open"...

    Here i am l posting the code so that you all may get some idea...



    Code:
    Dim Connection As New ADODB.Connection
    Dim Recordset As New ADODB.Recordset
    
    
    Private Sub cmdAdd_Click()
    txtControl.Enabled = True
    txtSupply.Enabled = True
    txtPieces.Enabled = True
    txtQuantity.Enabled = True
    txtPrice.Enabled = True
    cmdSave.Enabled = True
    cmdAdd.Enabled = False
    txtControl.SetFocus
    
    
    End Sub
    
    
    Private Sub cmdBack_Click()
    Unload Me
    
    End Sub
    
    Private Sub cmdSave_Click()
    With Recordset
    
        .Open "Select * from tblSupplies", Connection, adOpenStatic, adLockOptimistic, adLockReadOnly
    End With
        Recordset.AddNew
    Recordset.Fields("Control").Value = txtControl.Text
    Recordset.Fields("Supplies").Value = txtSupply.Text
    Recordset.Fields("Pieces").Value = txtPieces.Text
    Recordset.Fields("Quantity").Value = txtQuantity.Text
    Recordset.Fields("Price").Value = txtPrice.Text
    
    cmdAdd.Enabled = True
    cmdSave.Enabled = False
    Recordset.Update
    
    
    MsgBox "You have successfully added a new record in the database", vbInformation + vbOKOnly
    Set Recordset = Nothing
    
    txtControl.Enabled = False
    txtSupply.Enabled = False
    txtPieces.Enabled = False
    txtQuantity.Enabled = False
    txtPrice.Enabled = False
    
    
    End Sub
    
    Private Sub Form_Load()
    Connection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Users\-\Desktop\RIS\DATABASE\RIS.mdb;"
    Recordset.CursorLocation = adUseClient
    
    
    
    End Sub

    Thanks
    Last edited by Rabbit; Oct 19 '15, 05:20 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    You need to close the connection at the end of the button press. When you click the button once it opens the connection. When you click the button a second time it's trying to open the same connection which is already open.

    Comment

    Working...