Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to copy records to the same table Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
copy records to the same table 28-Aug-02
Category
BDE
Language
Delphi 2.x
Views
136
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I need to copy a record in a dBase table to the same table and just change a value 
or two. I know that I can copy the hard way read all the fields into a record then 
write it back out.

Answer:

Solve 1:

1   var
2     SourceQueryFieldName: string;
3   begin
4     QueryDestination.Open;
5     QuerySource.Open;
6     QueryDestination.Insert;
7     for FieldLoop := 0 to QuerySource.FieldCount - 1 do
8     begin
9       SourceQueryFieldName := DataBaseQuerySource.Fields[FieldLoop].FieldName;
10      try
11        QueryDestination[SourceQueryFieldName] := QuerySource[SourceQueryFieldName];
12      except
13        {Field not Found}
14      end;
15    end;
16    QueryDestination.Post;
17    QueryDestination.Close;
18    QuerySource.Close;
19  end;


Solve 2:

I actually prefer code that reads each field and writes it to the new record like 
this:

20  procedure CopyRecord(tbl: TTable);
21  var
22    I: Integer;
23    tblTmp: TTable;
24  begin
25    blTmp := TTable.Create(nil);
26    try
27      tblTmp.DatabaseName := tbl.DatabaseName;
28      tblTmp.TableName := tbl.TableName;
29      tblTmp.Open;
30      ttblTmp.GotoCursor(Src);
31      tbl.Insert;
32      try
33        for I := 0 to T.FieldCount - 1 do
34          tbl.Fields[I].Assign(tblTmp.Fields[I]);
35      except
36        tbl.Cancel;
37        raise;
38      end;
39    finally
40      tblTmp.Free;
41    end;
42  end;
43  
44  //But you can also do it like this:
45  
46  procedure CopyRecord(const FromTable: TTable);
47  begin
48    dbiInsertRecord(FromTable.Handle, dbiNoLock, FromTable.ActiveBuffer);
49  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC