Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete records within DBGrid

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I have a DBGrid on my form that shows the following information:

Date Type Time In Time Out Total Time
04/01/2004 ORIENT 8:30 am 10:30 am 2.0
04/03/2004 DLYPNL 9:30 am 5:00 pm 7.5
04/06/2005 DLYPNL 2:00 pm 4:30 pm 2.5


The total time field is a calculated field. There is another field in the query that is not shown in the grid which is the JURNUM (Part of the PK along w/ the date and the type).

Occasionally the users need to delete a specific time record and I'm having problems navigating to the proper record in the recordset to delete. I can delete records, but not the one selected. Can anyone tell me how I move to the record that was selected in the grid so I can delete it?

Thanks!

Leslie
 
Not sure I understand where this is going wrong? Your using a query and a DBGrid so the selected record in the DBGrid should be the active record in the query as the DBGrid is just a live, visual representation of whats in the query.

You should just be able to call Query1.Delete; in your code to delete the currently selected record without needing to worry about where the cursor is. Or are you trying to delete several selected records at once?

[blue]A good friend will come and bail you out of jail, but a true friend will be sitting next to you saying "Damn that was fun!"[/blue]
 
That's what I thought, but it's not working that way, it's always deleting the first record!

leslie
 
In that case, I would suggest using the RecNo function as the easiest wasy of being sure your are deleting the selected record. You can the the current record number and store it as an integer;

MyInteger := Query1.RecNo;

Then, when you want to delete the selected record, just move the query to that record. I cant remember off the top of my head but I think there is a MoveBy function, so :-

Query1.First;
Query1.MoveBy(MyInteger);
Query1.Delete;

Bear in mind that you would need to disable the Querys Controls first otherwise the user will see the data in the grid jumping about all over the place.

Query1.DisableControls;
Query1.First;
Query1.MoveBy(MyInteger);
Query1.Delete;
Query1.EnableControls;

Hope this helps

[blue]A good friend will come and bail you out of jail, but a true friend will be sitting next to you saying "Damn that was fun!"[/blue]
 
Just remembered, MoveBy will move the current poistion by the number given (obviously) so you would want to subtract one from the record number.

i.e RecNo = 2 but first record = 1, so MoveBy(2) would move the active record to number 3 and so on.

[blue]A good friend will come and bail you out of jail, but a true friend will be sitting next to you saying "Damn that was fun!"[/blue]
 
Ok, for some reason, it's working today! However, the DBGrid isn't redrawn correctly.

Say in the example above, I delete the middle record (4/3), unless I exit the form and open it again, the DBGrid shows:

Date Type Time In Time Out Total Time
04/03/2004 DLYPNL 9:30 am 5:00 pm 7.5
04/06/2005 DLYPNL 2:00 pm 4:30 pm 2.5

when I reopen the form, the correct entry has been deleted:

Date Type Time In Time Out Total Time
04/01/2004 ORIENT 8:30 am 10:30 am 2.0
04/06/2005 DLYPNL 2:00 pm 4:30 pm 2.5

How can I redraw the grid after the deletion so it shows the correct information?

les




 
never mind, got it by refreshing the query!!

thanks!

les
 
EricDraven said:
In that case, I would suggest using the RecNo function as the easiest wasy of being sure your are deleting the selected record.

I may be wrong, but IIRC, RecNo doesn't work with all types of databases, only with those that actually have record numbers - usually desktop databases like Paradox.

-Dell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top