Click to See Complete Forum and Search --> : Preventing the Database from crashing when the only Record is deleted


chem1
October 26th, 1999, 12:06 PM
Hello,
How do I prevent my database from crashing when I delete the only record from the Database Recordset.Code PLease!!!
Thankx in advance

Reid Robbins
October 26th, 1999, 01:54 PM
There are undoubtedly many ways. I always enclose all database operations with:


If MyRecordset.RecordCount > 0 then
MyRecordset.MoveLast
MyRecordsetCount = MyRecordset.RecordCount ' Variable now has true count
MyRecordset.MoveFirst ' I'm now ready to traverse the entire database
'
'(Perform processing here)
'
End If




You cannot count on the RecordCount property containing a correct recordcount until you have populated the recordset with "MoveLast" (which you cannot do until you have verified there IS at least a single record). However, you CAN count on the RecordCount property giving a non-zero value (typically "1") anytime there is at least one record in the recordset.
This technique is essential, yet no panacea. If you are working in a networked environment with a shared database, you may well enter this "If" block and then find that in that microsecond "window", some user at another station has deleted any or all records. Therefore, error trapping is also an essential stategy. There is an old axiom "Never test for an error condition you don't know how to handle". So, I use traps around "update" calls, but often embrace other recordset methods with a plain "On Error Resume Next", which simply ignores any errors I need not be concerned with. With these two techniques (and undoubtedly numerous others could readily be devised), you can pretty much prevent any problems even in a multi-user environment.



Reid Allen Robbins
Green River, WY 82935