Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

error regarding Private Sub cmdDelete_Click()

Question posted by: squrel (Member) on June 30th, 2008 09:55 AM
HI..
I need help over here... i have a delete button but is giving me error as " operation is not allowed when the object is closed"
Can anyone help me here, plz
thanks a lot


Private Sub cmdDelete_Click()
Dim X As String
Dim Rs As New ADODB.Recordset
If Rs.State = adStateOpen Then Rs.Close
'Rs.Open
X = MsgBox("Are you sure you want to delete this record?", vbYesNo, "Deletion Confirmation")
If X = vbYes Then
Rs.Delete
Rs.Close
MsgBox "Record Deleted!", , "Message"
Else
MsgBox "Record Not Deleted!", , "Message"
End If
End Sub
QVeen72's Avatar
QVeen72
Expert
1,258 Posts
June 30th, 2008
03:20 PM
#2

Re: error regarding Private Sub cmdDelete_Click()
Hi,

You are Closing the recordset object. Recordset object has to be Open, to delete the Record

remove this line :If Rs.State = adStateOpen Then Rs.Close

Regards
Veena

Reply
squrel's Avatar
squrel
Member
54 Posts
July 2nd, 2008
05:18 AM
#3

Re: error regarding Private Sub cmdDelete_Click()
Hi Veena..
I removed tht line but stil i m getting the same error...

Reply
squrel's Avatar
squrel
Member
54 Posts
July 2nd, 2008
05:44 AM
#4

Re: error regarding Private Sub cmdDelete_Click()
Dear Veena,
i have changed my code to this...

Private Sub cmdDelete_Click()
Dim X As String
Dim RECORD As String
Dim Rs As New ADODB.Recordset
' Rs.Open
'Rs.Open ("select * from CASEMASTER where Id = '" & txtId.Text & "'"), Db, adOpenKeyset, adLockPessimistic
X = MsgBox("Are you sure you want to delete this record?", vbYesNo, "Deletion Confirmation")
If X = vbYes Then
Rs.Open ("select * from CASEMASTER where Id = '" & txtId.Text & "'"), Db, adOpenKeyset
Rs.Delete
Rs.Close
MsgBox "Record Deleted!", , "Message"
Else
MsgBox "Record Not Deleted!", , "Message"
Call Grid_Data
End If
End Sub


the selected record is deleting for the first time but when i select another record is giving me this error " Either BOF or EOF is true, or the current record has been deleted".... and the second probelm is my gird is not gettind refreshed...
plz help

Reply
lotus18's Avatar
lotus18
Site Addict
806 Posts
July 2nd, 2008
02:32 PM
#5

Re: error regarding Private Sub cmdDelete_Click()
Try this

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelete_Click()
  2. Dim X As String
  3. Dim RECORD As String
  4. Dim Rs As New ADODB.Recordset
  5. X = MsgBox("Are you sure you want to delete this record?", vbYesNo, "Deletion Confirmation")
  6. If X = vbYes Then
  7. Rs.Open ("Delete from CASEMASTER where Id = '" & txtId.Text & "'"), Db, adOpenKeyset
  8. Rs.Close
  9. Set Rs=Nothing
  10. MsgBox "Record Deleted!", , "Message"
  11. Call Grid_Data
  12. Else
  13. MsgBox "Record Not Deleted!", , "Message"
  14. Call Grid_Data
  15. End If
  16. End Sub


At line 7 and 8 instead of opening the recordset try the execute command
Expand|Select|Wrap|Line Numbers
  1. db.Execute "Delete from CASEMASTER where Id = '" & txtId.Text & "'"
I assume that your db is your active connection and take a look also at line 11 I added Call Grid_Data to refresh the list assuming that this is your subroutine to refresh the list.

Rey Sean

Reply
squrel's Avatar
squrel
Member
54 Posts
July 3rd, 2008
04:30 AM
#6

Re: error regarding Private Sub cmdDelete_Click()
Hi Lotus..
Thank u very much for ur help.. i did whtever u told me.. i m not getting any error bt the record is not getting deleted from the grid or database...
my code is this now :

Private Sub cmdDelete_Click()
Dim X As String
Dim RECORD As String
Dim Rs As New ADODB.Recordset
X = MsgBox("Are you sure you want to delete this record?", vbYesNo, "Deletion Confirmation")
If X = vbYes Then
Db.Execute "Delete from CASEMASTER where Id = '" & txtId.Text & "'"
'Rs.Open ("Delete from CASEMASTER where Id = '" & txtId.Text & "'"), Db, adOpenKeyset
'Rs.Close
Set Rs = Nothing
MsgBox "Record Deleted!", , "Message"
Call Grid_Data
Else
MsgBox "Record Not Deleted!", , "Message"
Call Grid_Data
End If
End Sub

Need ur help here... Thankx

Reply
squrel's Avatar
squrel
Member
54 Posts
July 3rd, 2008
05:07 AM
#7

Re: error regarding Private Sub cmdDelete_Click()
I got it.. thankx a lot :)

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,890 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Visual Basic Forum Contributors