Link to home
Start Free TrialLog in
Avatar of kennys
kennys

asked on

Deleteing a record

I am new to VB6 and am writing a program which makes a database from code.

If the user deletes a record from the database all is well but when the last record is deleted, I get a run-time
error "3021" No current record.

My question is :- How do I error trap this.

I want to close the form and return to another form when the last record is deleted.

I repeat, I am new to VB.
ASKER CERTIFIED SOLUTION
Avatar of JasonSantiago
JasonSantiago

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JasonSantiago
JasonSantiago

Or you could do something like:

Sub MySub()
on error goto hError:

some code here....
.....
....

  exit sub
hError:
  if err.number = 3021 then
    unload me
  end if
End Sub 'MySub

But this really will only move your logical error to another place, as I said before you are doing something wrong.
Avatar of hes
Post your code for the delete
Avatar of kennys

ASKER

This is the code


Private Sub cmdDelete_Click()
   
    'delete the selected record
   
    Data1.Recordset.Delete
    Data1.Recordset.MoveNext
   
    'clear the list boxes
    lstBox1.Clear
    lstBox2.Clear
    lstBox3.Clear
    lstBox4.Clear
    lstBox5.Clear
   
    'reload the form
    Call Form_Load

End Sub
Private Sub cmdDelete_Click()
     
    'delete the selected record
     
    Data1.Recordset.Delete
  If not Data1.Recordset.EOF then
    Data1.Recordset.MoveNext
     
    'clear the list boxes
    lstBox1.Clear
    lstBox2.Clear
    lstBox3.Clear
    lstBox4.Clear
    lstBox5.Clear
     
    'reload the form
    Call Form_Load
   Else
     Unload me
   End if
End Sub


After you delete the last record move to the next record as you are doing. But immediately after executing MoveNext check for EOF. and if it's EOF you could take the desired action (e.g., unload the form). But just deleting the last row woudl not return you EOF status, you have to move the record pointer to the next record.
I hope this will solve your problem.
Avatar of kennys

ASKER

Many thanks