CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 1999
    Location
    Pakistan
    Posts
    366

    Preventing the Database from crashing when the only Record is deleted

    Hello,
    How do I prevent my database from crashing when I delete the only record from the Database Recordset.Code PLease!!!
    Thankx in advance


  2. #2
    Join Date
    Oct 1999
    Location
    S.W. Wyoming
    Posts
    25

    Re: Preventing the Database from crashing when the only Record is deleted

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured