Results 1 to 24 of 24

Thread: Having trouble with the delete code!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Having trouble with the delete code!

    Once again I'm having trouble.

    I used the code from Beacon's tutorial :

    vb Code:
    1. Private Sub cmdDelete_Click()    
    2. If MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion, "Delete?") = vbNo Then      
    3. 'check if you really want to delete this record        
    4. Exit Sub 'exit the command    
    5. Else        
    6.     If Not (rs.BOF = True Or rs.EOF = True) Then            
    7.         rs.Delete 'delete the current record            
    8.             If Not (rs.BOF = True Or rs.EOF = True) Then                
    9.                rs.MoveNext 'move next            
    10.                   If rs.EOF Then rs.MoveLast                
    11.                   fillfields            
    12.            End If        
    13.     End If    
    14. End If
    15. End Sub

    but everytime I try to delete the record I get the 3021 error.

    "Either BOF or EOF is True, or the current record has been deleted.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Having trouble with the delete code!

    On what line does it occur?

    Does the record get deleted?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Having trouble with the delete code!

    Sorry about forgetting to include the line that gets the error.

    Its the .MoveLast line and yes the record gets deleted.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Having trouble with the delete code!

    Ok. I sort of tossed a mental coin between MoveNext and MoveLast and picked MoveNext. I was close.

    However, I just knew it had to be one of those two.

    Question number 1: Why are you using either of them?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Having trouble with the delete code!

    2 reason's:

    1. It was in the tutorial

    2. I would like the gui to reflect that the record is gone. Right now, if I get the error and close the program and restart the program the record is gone.

    I would like it so that when the user deletes the record it will disappear from the gui and shows another record instead.

    I hope I made sense

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Having trouble with the delete code!

    Ok....what is your GUI? How are the records being displayed now?

    Typically, I will load a ListView or ListBox or something via a Sub routine. When I delete a record or update a record, I simply clear out the control, and call that Sub again.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Having trouble with the delete code!

    My gui consist of txtboxes which are populated by the recordset.

    I thought maybe the fillfields would repopulate the boxes or refresh the txtboxes, but I guess not.

    So, you are saying that I should clear out the txtboxes and then recall the recordset?

    I'll give that a try

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Having trouble with the delete code!

    Quote Originally Posted by cfd33
    So, you are saying that I should clear out the txtboxes and then recall the recordset?
    Thats what I would do....

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Having trouble with the delete code!

    Ok, this is what I have done, but it is not working, I get another error.

    Row handle referred to a deleted row or a row marked for deletion.

    Code:
    Private Sub cmdDelete_Click()    
        If MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion, "Delete?") = vbNo Then      
    'check if you really want to delete this record        
    
             Exit Sub 'exit the command    
       Else            
              If Not (rs.BOF = True Or rs.EOF = True) Then             
                    rs.Delete 'delete the current record                        
                                           
              End If            
       End If 
       Clearboxes
       Fillfields   
    End Sub
    The error points to the first part of the Fillfields code

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Having trouble with the delete code!

    Quote Originally Posted by cfd33
    The error points to the first part of the Fillfields code
    What is that code?

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Having trouble with the delete code!

    Hack,

    Here is the entire project.

    Woops, forgot to save the changes, here is the saved project.
    Attached Files Attached Files
    Last edited by cfd33; Apr 12th, 2007 at 01:54 PM.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Having trouble with the delete code!

    I think I have got it Hack,

    I didnt think about opening the recordset again. I thought I could just call it with the "Fillfields" sub.

    I opened a new rs and now it appears to be working.

    Yep, its working great now!! Thanks Hack. This is what I have done:
    vb Code:
    1. Private Sub cmdDelete_Click()
    2.     If MsgBox("Deleting this record will completely remove it from the database!" & _
    3.     vbCrLf & "Are you sure you wish to delete this record?", vbQuestion + vbYesNo, "Delete Record") = vbNo Then
    4.         Exit Sub
    5.     Else
    6.         If Not (rs.BOF = True Or rs.EOF = True) Then
    7.             rs.Delete
    8.         End If
    9.     End If
    10.     Set rs = New ADODB.Recordset
    11.     rs.CursorLocation = adUseClient
    12.     strSQL = "Select * From MT Order By ID ASC"
    13.    
    14.     rs.Open strSQL, strConnection, adOpenDynamic, adLockOptimistic
    15.    
    16.     ClearBoxes
    17.     If Not (rs.BOF = True Or rs.EOF = True) Then
    18.         rs.MoveNext <----I'm not sure, but I think I dont need this since my sql takes care of the order
    19.         Fillfields
    20.     End If
    21. End Sub
    Last edited by cfd33; Apr 12th, 2007 at 02:18 PM.

  13. #13
    Lively Member
    Join Date
    Apr 2005
    Posts
    91

    Re: Having trouble with the delete code!

    Hi, Tou Could try this

    vb Code:
    1. If Not (rs.BOF = True Or rs.EOF = True) Then
    2.             rs.Delete
    3.             rs.Requery 'Add This
    4.         End If
    Fishy

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Having trouble with the delete code!

    Fishy,

    Yes I tried that early on and it didnt work.

  15. #15
    Lively Member
    Join Date
    Apr 2005
    Posts
    91

    Re: Having trouble with the delete code!

    Strange, It does the same thing and works OK for me.

    Fishy

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Having trouble with the delete code!

    Hmmm,

    I'll try it again and see what happens.

    Are you using my project?

  17. #17
    Lively Member
    Join Date
    Apr 2005
    Posts
    91

    Re: Having trouble with the delete code!

    Yes.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Having trouble with the delete code!

    Did you keep the code as is with the exception of adding rs.requery?

    Would you post what you have done. I tried it and it wil delete ok and clear the boxes, but wont repopulate.
    Last edited by cfd33; Apr 12th, 2007 at 02:32 PM.

  19. #19
    Lively Member
    Join Date
    Apr 2005
    Posts
    91

    Re: Having trouble with the delete code!

    All I added was the rs.requery and in the properties of the project
    I had to remove the Exel reference but I had to add the Active X
    Data Objects 2.8 Library

    Fishy.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Having trouble with the delete code!

    But if I remove the Excel reference, then I wont be able to send my datagrid to the excel worksheet.

  21. #21
    Lively Member
    Join Date
    Apr 2005
    Posts
    91

    Re: Having trouble with the delete code!

    Sorry, I don't have excel and it wont compile.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Having trouble with the delete code!

    Ok, thanks anyway.

    It's working with the rs.open; so I think I'll go with that for now.

  23. #23
    Lively Member
    Join Date
    Apr 2005
    Posts
    91

    Re: Having trouble with the delete code!

    Just one thing in your zip file you had a .exe of the program
    (Bit naughty should not include exe files) that works OK deletes
    the records and refreshes the form with next record. I think you
    have some corruption in your project somewhere.

    Fishy

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: Having trouble with the delete code!

    Oh yeah, I forgot to remove that exe. It was from compiling the program the first time to see if it would work ok.

    That's funny that it works fine for you. How can I fix the corruption?

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