Question

Can anyone check my code and fixed it ? because I think there has a problem near at the ((if rs.EOF)) because i use this method 2times. and if i run it i got this error:

Error: Operation is not allowed when the object is open.


Public Sub addBorrower(BCat As String, BIname As String, BImod As String, BFname As String, BLname As String, BBD As String, BDR As String)

    Dim itemid As Integer

    qry2 = "select tblItem_id,item_name,item_model,category_name from tblitem inner join tblcategory on tblcategory.tblcategory_id=tblitem.tblcategory_id where tblitem.item_name='" + BIname + "' and tblitem.item_model='" + BImod + "' and tblcategory.category_name='" + BCat + "'"

    Set rs = conn.Execute(qry2)

    If Not rs.EOF Then 

        itemid = rs.Fields(0).Value
        qry1 = "select * from tblborrowers where tblItem_id=' + itemid + '"
        rs.Open qry1, conn

        If Not rs.EOF Then

            DateReturned = rs.Fields(5).Value
            If DateReturned = Null Then
                MsgBox "Cant add the Borrowers to the item because there's some user who used it that hav'nt returned Yet."
            Else
                qry1 = "insert into tblborrowers(Firstname,Lastname,tblItem_id,date_Borrowed,date_Returned)values('" + BFname + "','" + BLname + "','" + itemid + "','" + BDD + "','" + BDR + "')"
                MsgBox "Successfully added"
            End If

        Else
            qry1 = "insert into tblborrowers(Firstname,Lastname,tblItem_id,date_Borrowed,date_Returned)values('" + BFname + "','" + BLname + "','" + itemid + "','" + BDD + "','" + BDR + "')"
            MsgBox "Successfully added"
        End If

    Else
        MsgBox "Sorry the Item category,Item name and Item model did'nt match or exist. Please check.", vbCritical, "Notification"
    End If

    Set rs = Nothing

End Sub
Was it helpful?

Solution

You did very simple mistake.

You have open the Recordset here

Set rs = conn.Execute(qry2)

And, again you are opening the same recordset RS here

qry1 = "select * from tblborrowers where tblItem_id=' + itemid + '"
    rs.Open qry1, conn

If you want to Open the Rs again, then first Close it. Thats why it saying : Operation is not allowed when the object is open.

rs.Close
qry1 = "select * from tblborrowers where tblItem_id=' + itemid + '"
    rs.Open qry1, conn

Or, Use another Recordset.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top