Click to See Complete Forum and Search --> : delete method


Shah
September 16th, 1999, 10:58 PM
hello,

How do I prevent from getting an error when deleting the last record of a recordset using VB5.

sample code:-
Private Sub cmdDelete_Click()
Dim response
response = MsgBox("Delete current Record", vbOKCancel, "DELETE RECORD")

If response = vbOK Then ' User chose Yes.
With Data1.Recordset
.Delete
.MoveNext
If .EOF Then .MoveLast
End With
End If
End Sub

Thank you.

santulan
September 16th, 1999, 11:16 PM
Shah,
Use the following code

private Sub cmdDelete_Click()
Dim response
on error goto ThisError 'Extra care for trapping the error
response = MsgBox("Delete current Record", vbOKCancel, "DELETE RECORD")

If response = vbOK then ' User chose Yes.
With Data1.Recordset
if NOT .EOF
.Delete
end if
.MoveNext
If .EOF then .MoveLast
End With
Goto Last
thisError:
Last:
End If
End Sub




I hope this solves your problem. Do not forget to disable On error Goto command so that you can trap other errors...

Santulan

Shah
September 17th, 1999, 02:20 AM
Thanks for the tip. The function work but after running, the routine my cursor change to the hour glass and it never stop.

santulan
September 17th, 1999, 03:40 AM
Shah,

Please try to remove this line
If .EOF then .MoveLast
This may play innocence.

Thanks.
Santulan

wolfen_76
September 17th, 1999, 09:51 AM
while not data1.eof
data1.delete
wend

Leandro Avelar
September 17th, 1999, 12:27 PM
Hi Shah,

Itīs too easy, in the begining of the sub write on error resume next.
It works pretty well and itīs faster than others commands.

So would you like to learn how to use SQL to work with your databases, itīs better than data control, not so easy but more flexible.

Private Sub cmdDelete_Click()
Dim response

ON ERROR RESUME NEXT --> When occur any error it will jump to the next command line

response = MsgBox("Delete current Record", vbOKCancel, "DELETE RECORD")

If response = vbOK Then
With Data1.Recordset
.Delete ---> IT DELETE THE REGISTER
.MoveNext --> IT CONFIRM THE DELETION
If .EOF Then .MoveLast --> IF eof THEN WILL GENERATE AN ERROR BECAUSE DONīT HAVE
A REGISTER TO MOVE LAST
End With
End If
End Sub


Leandro de Avelar
System Engineer
National Institut of Telecommunications