Solved

Making last record new record???

Posted on 2000-03-19
8
139 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…

789 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