Connecting Tech Pros Worldwide Forums | Help | Site Map

error regarding Private Sub cmdDelete_Click()

Familiar Sight
 
Join Date: Jan 2008
Posts: 137
#1: Jun 30 '08
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
Moderator
 
Join Date: Oct 2006
Location: Bangalore
Posts: 1,385
#2: Jun 30 '08

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
Familiar Sight
 
Join Date: Jan 2008
Posts: 137
#3: Jul 2 '08

re: error regarding Private Sub cmdDelete_Click()


Hi Veena..
I removed tht line but stil i m getting the same error...
Familiar Sight
 
Join Date: Jan 2008
Posts: 137
#4: Jul 2 '08

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
lotus18's Avatar
Site Addict
 
Join Date: Nov 2007
Location: Zamboanga City, Philippines
Posts: 857
#5: Jul 2 '08

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
Familiar Sight
 
Join Date: Jan 2008
Posts: 137
#6: Jul 3 '08

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
Familiar Sight
 
Join Date: Jan 2008
Posts: 137
#7: Jul 3 '08

re: error regarding Private Sub cmdDelete_Click()


I got it.. thankx a lot :)
Reply