Steve Koterski wrote:
> On Wed, 22 Sep 1999 06:01:49 -0700, Yuri <y...@tsoft.net> wrote:
> >I am trying to have some record from the database being edited on the
> >screen and user should have possibility to save it any time. But if the
> >record is new -- after hitting "Save" 2 times Query forgets field values
> >and the third time causes new record with all nulls being inserted.
> >Here is a piece of code that demonstrates the problem:
> >{ tst is table with two integer fields f1 and f2, f1 is the key }
> > Query.SQL.Text := 'select f1, f2 from tst where f1 = 10';
> > Query.Open;
> > Query.Edit;
> > if Query.RecordCount = 0 then
> > Query.FieldByName('f1').AsInteger := 10;
> > Query.FieldByName('f2').AsInteger := 20;
> > Query.Post; { first save }
> > Query.Edit; { -- }
> > Query.Post; { second save }
> > Query.Edit; { -- }
> > Query.Post; { third: here is an attempt to insert new record with
> >all nulls if initially was no record }
> > Query.Edit;
> I hope the above was just an example and your actual application does not
> use code like this. I cannot see any practical benefit in calling the Edit
> and Post methods in combination three times (!) in a routine.
> I would suggest a change to when you check the number of records returned
> by the query and what you do in response to that. Instead of calling the
> Edit method when the query returns no rows (as you were doing) to edit a
> nonexistent row, call the Insert method to add a new row.
> with Query1 do begin
> SQL.Text := 'SELECT f1, f2 FROM tst WHERE f1 = 10';
> Open;
> if (RecordCount > 0) then begin
> Edit;
> end
> else begin
> Insert;
> FieldByName('f1').AsInteger := 10;
> end;
> FieldByName('f2').AsInteger := 20;
> Post; { only one save is really needed }
> end;
> _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
> Steve Koterski "Health nuts are going to feel stupid someday,
> Felton, CA lying in hospitals dying of nothing."
> -- Redd Foxx