Solved

Making last record new record???

Posted on 2000-03-19
8
140 Views
Last Modified: 2010-04-04
Hi all,

After saving a record to a table, I want to make the values saved the defaults of a new (yet to be posted) record and have those values displayed in the data-aware components?

Any ideas?

Thanx, fats
0
Comment
Question by:ffelaar
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2633082
hi ffelaar,

from my paq, a bit modified



                   a sample (table1 = Workingtable, table2 = get last values)

                         procedure TForm1.Button1Click(Sender: TObject);
                         var i : Integer;
                         begin
                           table2.last;
                           table1.Append;
                           for i := 0 to table2.FieldCount - 1 do
                             table1.Fields[i].Assign(table2.Fields[i]);
                           //table1.post; //do not post yet, maybe the user want to modify
                         end;
remark table1 and table2 can pointed to the same table or to two tables with same structure,

meikl
0
 

Expert Comment

by:binho
ID: 2633094
ffelaar:
I use to do that by 2 ways:
1. If the number of columns is not that big (about 10), I use Delphi variables (var) to keep the values BEFORE posting the first record. Then, after I call the Post method and the Insert (to insert the new record), I set the new record fields using the values stored in the variables.

2. In the AfterInsert of the TTable or TQuery, use a auxiliary TQuery to search for the last posted record (if you have a Primary key created with a sequence, use the SQL:
SELECT * FROM TABLE_NAME
WHERE KEY_FIELD IN
(SELECT MAX(KEY_FIELD
 FROM TABLE_NAME)

After getting the last record, set the new fields by
Query1.FieldByName('FIELD1').AsType := QueryAux.FieldByName('FIELD1).AsType;
Query1.FieldByName('FIELD2').AsType := QueryAux.FieldByName('FIELD2).AsType;

and so on...

Fabio
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2633179
to kretzschmar.
Sorry all for disturbing, but it's one way to access Meikl.

Meikl, something wrong with your e-mail, I get a message:
-------------
"The message that you sent was undeliverable to the following:
        meikl@abcdef.de (access denied)"
-------------
where abcdef = your host. Do I need to try agan it later or may be you have another e-mail?

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 2633317
to igor,
didn't know, where this come from (abcdef)

my eMail is
meikl@spektracom.de

meikl
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2633329
to meikl,
I get it again:
----
The message that you sent was undeliverable to the following:
        meikl@spektracom.de (access denied)
-----

Ok, i will consult with me sysadmin, it look like we get troubles.

By Igor.
0
 
LVL 2

Accepted Solution

by:
kubeerja earned 75 total points
ID: 2633477

-Before post save the values of the last record Inserted.
-On new record read the field values.

try this code :

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, DBCtrls, Db, StdCtrls, Mask, DBTables;

type
  TForm1 = class(TForm)
    Table1: TTable;
    Label1: TLabel;
    DBEdit1: TDBEdit;
    DataSource1: TDataSource;
    Label2: TLabel;
    DBEdit2: TDBEdit;
    Label3: TLabel;
    DBEdit3: TDBEdit;
    Label4: TLabel;
    DBEdit4: TDBEdit;
    Label5: TLabel;
    DBEdit5: TDBEdit;
    Label6: TLabel;
    DBEdit6: TDBEdit;
    DBNavigator1: TDBNavigator;
    procedure Table1NewRecord(DataSet: TDataSet);
    procedure Table1BeforePost(DataSet: TDataSet);
  private
    { Private declarations }
    LastRec:Variant ;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Table1NewRecord(DataSet: TDataSet);
var
 I:Integer ;
begin
  if VarIsArray(LastRec) then
    For I:=0 to DataSet.FieldCount-1 do
    begin
      DataSet.Fields[I].Value := LastRec[I] ;
    end;
end;

procedure TForm1.Table1BeforePost(DataSet: TDataSet);
var
 I:Integer ;
begin
  if DataSet.State = dsInsert then
  begin
   if not VarIsArray(LastRec) then
    LastRec :=VarArrayCreate([0,DataSet.FieldCount], varVariant);
   For I:=0 to DataSet.FieldCount-1 do
   LastRec[I] :=  DataSet.Fields[I].Value;
  end ;
end;

end.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2633998
Listening
0
 

Author Comment

by:ffelaar
ID: 2683335
Well what can I say, exactly what I needed. I am sorry I took so long to respond ... I was kind of down and out wiht the flu ... but hey, better late than never hey

Thank you, thank you once again ...

fats
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tembedded WB animatid gifs not animated on some pcs 2 86
CheckListBox usage 3 82
How to load 2 images in same column in Delphi 2 68
Firemonkey BASS_Init into a thread 17 57
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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 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