views:

561

answers:

3

Hi,

I would like to change the behaviour of the insert button on the standard DBNavigator bar, from a dataset insert to append.

I could trap the button click in the BeforeAction event, do the append, etc; and then in the OnClick event abort the original insert, but this seems a bit of a hack. Any better ideas? I'm using D6 (500,000 kms on the clock, and still going strong...).

Thanks for any advice

Regards,

PhilW.

+6  A: 

You could derive your own class from TDBNavigator and override BtnClick method. Or, for a quick and dirty fix, you could change the insert button's click handler at runtime, e.g.:

type
  THackDBNavigator = class(TDBNavigator);

procedure TForm1.DBNavigatorInsertClick(Sender: TObject);
var
  DBNavigator: TDBNavigator;
begin
  DBNavigator := ((Sender as TControl).Parent as TDBNavigator);
  if Assigned(DBNavigator.DataSource) and (DBNavigator.DataSource.State <> dsInactive) then
  begin
    if Assigned(DBNavigator.BeforeAction) then
      DBNavigator.BeforeAction(DBNavigator, nbInsert);

    DBNavigator.DataSource.DataSet.Append;

    if Assigned(DBNavigator.OnClick) then
      DBNavigator.OnClick(DBNavigator, nbInsert);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  THackDBNavigator(DBNavigator1).Buttons[nbInsert].OnClick := DBNavigatorInsertClick;
end;
TOndrej
A: 

There is no difference in most databases between insert and append. Doing an actual physical insert would mean actually moving all data, starting with the place the new row would be inserted, down the size of one row, and then writing that new row in the newly open spot. This would be very slow because of all of the disk activity.

Databases instead do an append, which writes the data to the end of the physical file, and the index order controls the way the row appears to be positioned in the correct place in the file.

So for most intents and purposes, you're probably already getting an append instead of an insert, regardless of which method you use or what the button on the DBNavigator says. It's the index that makes it appear otherwise.

You can check that for validity by creating a database without an index, and try doing both an insert and an append a few times, examining the data carefully after every operation.

Ken White
If you use a grid component to insert/append your data, there's definitely a difference between the two, at least from a UI standpoint.
onnodb
But that's because it's not actually inserting into the database, but the grid instead. It's still just an append to the DB physically.
Ken White
A: 

@TOndrej: Great! I hadn't appreciated this technique. Thanks!

@Ken White: I understand your point, but visually to my users it makes a difference - the DBNavigator controls a DBGrid where, in the majority of cases, there is plenty of unused rows in the grid. It appears to be more consistent to have new records appear at the bottom of the grid rather then just above where ever the current record is at that moment. But thanks for your answer.

Regards, PhilW.

PhilW
Phil, you didn't mention anything about a DBGrid in your question. In that case, TOndrej's answer *is* better because of the appearance issue.
Ken White