Solved

Making last record new record???

Posted on 2000-03-19
8
137 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, DigitalPersona Fingerprint reader and MySQL 6 252
Process Javascript errors with Delphi TWebBrowser 1 128
Working with hours 3 54
control image tags in a string ? 12 127
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now