?
Solved

ADO/Access & Delphi - Deleting selected record in DBGrid?

Posted on 2009-04-17
15
Medium Priority
?
1,571 Views
Last Modified: 2013-11-23
Hi there, new question... Having trouble deleting a record from a table when selected in a DBGrid.

Code I'm using is:  
  StockQuery.Close;
  ADOStock.Delete;
  StockQuery.Open;

And it runs on an onclick event. What happens is it deletes either the record at the BoF, or the EoF if a new record has just been added. Seems to me that clicking a record in the DBGrid doesn't make it the "current" record so to speak. Any way around this?

0
Comment
Question by:TTRMW
  • 8
  • 7
15 Comments
 
LVL 18

Expert Comment

by:Johnjces
ID: 24168297
 StockQuery.Open;
  ADOStock.Delete;
  StockQuery.Close;  // If you have to close it.

John
0
 

Author Comment

by:TTRMW
ID: 24168327
Okiedoke, tried this. Same effect (deletes the EoF record), but also closes the dataset (obv) and displays no data in the dbgrid until it is reopened.
0
 

Author Comment

by:TTRMW
ID: 24168385
BoF record, even.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:TTRMW
ID: 24168439
Ah okay, removing the .close part solves the closing issue, in retrospect that's painfully obvious. Still having trouble with deleting a selected record as opposed to just the BoF record.
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 24168448
OPen the dataset.

Click on the record you want to delete in the dbGrid

The ADOStock.Delete.

0
 

Author Comment

by:TTRMW
ID: 24168459
Yep, fails hard. Just deletes the BoF record
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 24168471
Well... I don't know. Makes no sense!

John
0
 

Author Comment

by:TTRMW
ID: 24168495
Haha, awesome.

I'm thinking that selecting a record in the DBGrid doesn't make it the current record. Could this be because the DBGrid is populated by the query, not the ADOTable component? If so is there a work around or perhaps a manual way to set the current record when the selected record on the DBGrid is changed? Maybe an oncellchange event or similar?
0
 
LVL 18

Accepted Solution

by:
Johnjces earned 1000 total points
ID: 24168535
Well.. it has to point to whatever dataset is contained within it!

If the grid is pointing to your Query, then by deleting a record from another dataset, yes bad things will happen!

With your Query... you delete.

ADOQuery1.Open
ADOQuery1.Delete

John
0
 

Author Comment

by:TTRMW
ID: 24168572
Haha, that was really simple in retrospect, works now, thanks lots.

One last part (I can create another question if that's more acceptable!)

If using a non-data aware editbox to display the information contained in a record, what event would I want to use so that it updates the editbox when the current record is changed?

Cheers man :D
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 24168628
Hmmm..

I would use AfterScroll.

At least I do to refresh data and do new queries and etc.

John
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 24168763
Please let me know if all this is working for you!

I 'think' it all should!

By the way the AfterScroll means that the action you have in there will happen after any record is shown in the dataset.

John

0
 

Author Comment

by:TTRMW
ID: 24168778
Thus far all going well. How would I implement AfterScroll? It's not in the events editor for the DBGrid and I'm generally pretty useless!
0
 
LVL 18

Assisted Solution

by:Johnjces
Johnjces earned 1000 total points
ID: 24168814
It will be in the query events and the table events. You gotta use the right one, table or query depending upon where your results set is coming from.
0
 

Author Comment

by:TTRMW
ID: 24168835
Oh you legend, that works great now!

Thanks very much man!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question