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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting all ADOTable Records

Status
Not open for further replies.

MLNorton

Programmer
Nov 15, 2009
134
US
I create a ADOTable which may be from 1 to over 25 records. I After use I need to delete all records. I have the following procedure:
procedure TForm_Main.Button_CleanClick(Sender: TObject);
begin
ADOTable1.First;
With ADOTable1 do
begin
while NOT EOF do
begin
Delete;
next;
end;
end;
end;

Only a few records are deleted and I get the following error message:

Key column information is insufficient. Too many rows are affected by update.

What is my problem and how can I delete all records?
 
first of all, don't use ADOTable.
Try to learn SQL and use TADOQuery;

deleting all rows from a table is one SQL statement:

Code:
DELETE FROM yourtablename

where yourtablename is the name of your table
wrapping this up with TADOQuery:

Code:
procedure TForm_Main.CleanTable(TableName : string);

var Qry : TADOQuery;

begin
 Qry := TADOQuery.Create(nil);
 // adapt this line with the name of your actual TADOConnection component
 Qry.Connection := Form_main.DbConnection;
 try
  Qry.SQL.Add(Format('DELETE FROM %s', [TableName]));
  Qry.ExecSQl;
 finally
  FreeAndNil(Qry);
 end;
end;

// now use it!
procedure TForm_Main.Button_CleanClick(Sender: Object);
begin
 // nice clean self explaining code in your Click event
 CleanTable('orders'); 
end;

A second issue could be that your table is referenced by other tables through foreign keys. If the above sample doesn't work, you need to provide the full db layout.

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top