Apr 12th, 2007, 11:39 AM
#1
Thread Starter
Hyperactive Member
Having trouble with the delete code!
Once again I'm having trouble.
I used the code from Beacon's tutorial :
vb 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
If Not (rs.BOF = True Or rs.EOF = True) Then
rs.MoveNext 'move next
If rs.EOF Then rs.MoveLast
fillfields
End If
End If
End If
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.
Apr 12th, 2007, 11:43 AM
#2
Re: Having trouble with the delete code!
On what line does it occur?
Does the record get deleted?
Apr 12th, 2007, 11:45 AM
#3
Thread Starter
Hyperactive Member
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.
Apr 12th, 2007, 11:47 AM
#4
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?
Apr 12th, 2007, 11:51 AM
#5
Thread Starter
Hyperactive Member
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
Apr 12th, 2007, 11:58 AM
#6
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.
Apr 12th, 2007, 12:54 PM
#7
Thread Starter
Hyperactive Member
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
Apr 12th, 2007, 01:02 PM
#8
Re: Having trouble with the delete code!
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....
Apr 12th, 2007, 01:21 PM
#9
Thread Starter
Hyperactive Member
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
Apr 12th, 2007, 01:25 PM
#10
Re: Having trouble with the delete code!
Originally Posted by
cfd33
The error points to the first part of the Fillfields code
What is that code?
Apr 12th, 2007, 01:34 PM
#11
Thread Starter
Hyperactive Member
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
Last edited by cfd33; Apr 12th, 2007 at 01:54 PM .
Apr 12th, 2007, 02:07 PM
#12
Thread Starter
Hyperactive Member
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:
Private Sub cmdDelete_Click()
If MsgBox("Deleting this record will completely remove it from the database!" & _
vbCrLf & "Are you sure you wish to delete this record?", vbQuestion + vbYesNo, "Delete Record") = vbNo Then
Exit Sub
Else
If Not (rs.BOF = True Or rs.EOF = True) Then
rs.Delete
End If
End If
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
strSQL = "Select * From MT Order By ID ASC"
rs.Open strSQL, strConnection, adOpenDynamic, adLockOptimistic
ClearBoxes
If Not (rs.BOF = True Or rs.EOF = True) Then
rs.MoveNext <----I'm not sure, but I think I dont need this since my sql takes care of the order
Fillfields
End If
End Sub
Last edited by cfd33; Apr 12th, 2007 at 02:18 PM .
Apr 12th, 2007, 02:17 PM
#13
Lively Member
Re: Having trouble with the delete code!
Hi, Tou Could try this
vb Code:
If Not (rs.BOF = True Or rs.EOF = True) Then
rs.Delete
rs.Requery 'Add This
End If
Fishy
Apr 12th, 2007, 02:19 PM
#14
Thread Starter
Hyperactive Member
Re: Having trouble with the delete code!
Fishy,
Yes I tried that early on and it didnt work.
Apr 12th, 2007, 02:24 PM
#15
Lively Member
Re: Having trouble with the delete code!
Strange, It does the same thing and works OK for me.
Fishy
Apr 12th, 2007, 02:26 PM
#16
Thread Starter
Hyperactive Member
Re: Having trouble with the delete code!
Hmmm,
I'll try it again and see what happens.
Are you using my project?
Apr 12th, 2007, 02:28 PM
#17
Lively Member
Re: Having trouble with the delete code!
Apr 12th, 2007, 02:28 PM
#18
Thread Starter
Hyperactive Member
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 .
Apr 12th, 2007, 02:35 PM
#19
Lively Member
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.
Apr 12th, 2007, 02:39 PM
#20
Thread Starter
Hyperactive Member
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.
Apr 12th, 2007, 02:41 PM
#21
Lively Member
Re: Having trouble with the delete code!
Sorry, I don't have excel and it wont compile.
Apr 12th, 2007, 02:45 PM
#22
Thread Starter
Hyperactive Member
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.
Apr 12th, 2007, 02:50 PM
#23
Lively Member
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
Apr 12th, 2007, 03:01 PM
#24
Thread Starter
Hyperactive Member
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
Forum Rules
Click Here to Expand Forum to Full Width