BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

table.last does not point to last record in table

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (Desktop)
View previous topic :: View next topic  
Author Message
Georg Beyer Clausen
Guest





PostPosted: Thu Apr 01, 2004 10:59 am    Post subject: table.last does not point to last record in table Reply with quote



D3Pro, Paradox 7
After an insert I want to get the value of fields[0] of last record.

I tried the following:
detailTab.Last;
newGlosID := detailTab.Fields[0].AsInteger;

However 'newGlosID' does not return the value of the last inserted record.

How do I get the value of the last inserted record?

(code fragments below)


TIA,

Georg





----------------


for i := 0 to dbgrid2.SelectedRows.Count -1 do
begin
with query1 do
begin

DetailTab.GotoBookmark(TBookmark(DBGrid2.selectedRows[i]));
if active then Close;
SQL.Clear;
SQL.Add(' Insert into "'+ordbogTab+'" (lektionstitel_ID,
fremmedsprog, modersmål, anden_betydning, ordklasse, køn, kommentarer,
eksempel_fremmedsprog, eksempel_modersmål)');
SQL.Add(' VALUES(:lektionstitel_ID, :fremmedsprog,
:modersmål,:anden_betydning, :ordklasse, :køn, :kommentarer,
:eksempel_fremmedsprog, :eksempel_modersmål) ');
Params[0].AsInteger := Til_LekID;
Params[1].AsString := detailTab.Fields[2].AsString;
Params[2].AsString := detailTab.Fields[3].AsString;
Params[3].AsString := detailTab.Fields[4].AsString;
Params[4].AsString := detailTab.Fields[5].AsString;
Params[5].AsString := detailTab.Fields[6].AsString;
Params[6].AsMemo := detailTab.Fields[7].AsString;
Params[7].AsString := detailTab.Fields[8].AsString;
Params[8].AsString := detailTab.Fields[9].AsString;
if not Prepared then Prepare;
ExecSql;
end;


detailTab.Last;
newGlosID := detailTab.Fields[0].AsInteger;

with query1 do
begin
if active then Close;
SQL.Clear;
SQL.Add('Insert into "'+mmTab+'" (Glose_ID, pic001,
pic002, pic003, sound001, sound002, sound003, film001, film002, film003)');
SQL.Add('VALUES(:Glose_ID, :pic001, :pic002, :pic003,
:sound001, :sound002, :sound003, :film001, :film002, :film003)');
Params[0].AsInteger := newGlosID;
Params[1].AsString := multimediaTab.Fields[1].AsString;
Params[2].AsString := multimediaTab.Fields[2].AsString;
Params[3].AsString := multimediaTab.Fields[3].AsString;
Params[4].AsString := multimediaTab.Fields[4].AsString;
Params[5].AsString := multimediaTab.Fields[5].AsString;
Params[6].AsString := multimediaTab.Fields[6].AsString;
Params[7].AsString := multimediaTab.Fields[7].AsString;
Params[8].AsString := multimediaTab.Fields[8].AsString;
Params[9].AsString := multimediaTab.Fields[9].AsString;
if not Prepared then Prepare;
ExecSql;
end;

end;
end;
end;



Back to top
John Herbster
Guest





PostPosted: Thu Apr 01, 2004 12:56 pm    Post subject: Re: table.last does not point to last record in table Reply with quote




"Georg Beyer Clausen" <develop (AT) gbcdesign (DOT) dk> wrote
Quote:
After an insert I want to get the value of fields[0] of last record.
I tried the following:
detailTab.Last;
newGlosID := detailTab.Fields[0].AsInteger;

Georg,
Does your dataset have an index for insertion order or time?
If so you need to use it; because otherwise the concept of
"last" is not well defined..
Regards, JohnH



Back to top
Bill Todd (TeamB)
Guest





PostPosted: Thu Apr 01, 2004 6:51 pm    Post subject: Re: table.last does not point to last record in table Reply with quote



When you insert a row into a Paradox table the new row should be the
current row after you post.

--
Bill (TeamB)
(TeamB cannot respond to questions received via email)
Back to top
Georg Beyer Clausen
Guest





PostPosted: Thu Apr 01, 2004 8:39 pm    Post subject: Re: table.last does not point to last record in table Reply with quote

"John Herbster" <herb-sci1_AT_sbcglobal.net> skrev i en meddelelse
news:406c1180 (AT) newsgroups (DOT) borland.com...
Quote:

"Georg Beyer Clausen" <develop (AT) gbcdesign (DOT) dk> wrote
After an insert I want to get the value of fields[0] of last record.
I tried the following:
detailTab.Last;
newGlosID := detailTab.Fields[0].AsInteger;

Georg,
Does your dataset have an index for insertion order or time?
If so you need to use it; because otherwise the concept of
"last" is not well defined..
Regards, JohnH

Hi John,
it is a primary key field, so I believe it is indexed?
How do I use the index to move to last record?

regards,
Georg



Back to top
Georg Beyer Clausen
Guest





PostPosted: Thu Apr 01, 2004 8:49 pm    Post subject: Re: table.last does not point to last record in table Reply with quote


"Bill Todd (TeamB)" <no (AT) no (DOT) com> skrev i en meddelelse
news:05po605ic7fknlg01rmknl6ob32gql1g8u (AT) 4ax (DOT) com...
Quote:
When you insert a row into a Paradox table the new row should be the
current row after you post.

--
Bill (TeamB)
(TeamB cannot respond to questions received via email)

hi Bill,
newGlosID := detailTab.Fields[0].AsInteger; returns the value of the first
record in the loop
(DetailTab.GotoBookmark(TBookmark(DBGrid2.selectedRows[i]));)

Maybe the insert is not yet commited?
How do I make a commit in my code?

regards,
Georg



Back to top
John Herbster
Guest





PostPosted: Thu Apr 01, 2004 9:19 pm    Post subject: Re: table.last does not point to last record in table Reply with quote


Georg, Bill Todd is the expert. I see that he is trying to help.
I would follow his advice. Regards, JohnH
Back to top
Georg Beyer Clausen
Guest





PostPosted: Thu Apr 01, 2004 10:17 pm    Post subject: Re: table.last does not point to last record in table Reply with quote

Quote:
newGlosID := detailTab.Fields[0].AsInteger; returns the value of the
first
record in the loop
(DetailTab.GotoBookmark(TBookmark(DBGrid2.selectedRows[i]))Wink

correction:

the loop is:
for i := to dbgrid2.SelectedRows.Count -1 do

Georg



Back to top
Bill Todd (TeamB)
Guest





PostPosted: Fri Apr 02, 2004 1:29 pm    Post subject: Re: table.last does not point to last record in table Reply with quote

On Fri, 2 Apr 2004 00:17:31 +0200, "Georg Beyer Clausen"
<develop (AT) gbcdesign (DOT) dk> wrote:

Quote:
for i := to dbgrid2.SelectedRows.Count -1 do

That statement will not compile. Show us the actual code so we can see
what is happening.

--
Bill (TeamB)
(TeamB cannot respond to questions received via email)

Back to top
Georg Beyer Clausen
Guest





PostPosted: Fri Apr 02, 2004 1:36 pm    Post subject: Att: Bill Todd Re: table.last does not point to last reco Reply with quote


"Bill Todd (TeamB)" <no (AT) no (DOT) com> skrev i en meddelelse
news:05po605ic7fknlg01rmknl6ob32gql1g8u (AT) 4ax (DOT) com...
Quote:
When you insert a row into a Paradox table the new row should be the
current row after you post.

--
Bill (TeamB)
(TeamB cannot respond to questions received via email)

Bill,
newGlosID := detailTab.Fields[0].AsInteger; returns the value of the
first record in the loop

Maybe the insert is not yet commited?
How do I make a commit?

regards,
Georg



Back to top
Georg Beyer Clausen
Guest





PostPosted: Fri Apr 02, 2004 1:44 pm    Post subject: Att.: Bill Todd Re: table.last does not point to last re Reply with quote

Quote:
That statement will not compile. Show us the actual code so we can see
what is happening.

--
Bill (TeamB)
(TeamB cannot respond to questions received via email)

Bill,
below the hole procedure

regards,
Georg

procedure TCopyForm.BitBtn4Click(Sender: TObject);

var
Fra_LekID, Til_LekID : Integer;
i : Integer;
antal_glos : Integer;
CopyIt : boolean;
newGlosID, ct : Integer;

begin
OvcDbSearchEdit1.text := '';
CopyIt := false;
Fra_LekID := table1.FieldByName('Lektionstitel_ID').AsInteger;
Til_LekID := Table3.FieldByName('Lektionstitel_ID').AsInteger;

// copy marked words
If Radiogroup1.ItemIndex = 0 then
begin
if dbgrid2.SelectedRows.Count = 0 then

Application.MessageBox('Ingen gloser markeret!'+chr(13)+
' Brug musen+Ctrl eller'+chr(13)+
' Shift+PilNed', 'Kopiering', 16)
else
begin
CopyIt := true;
Animate1.Visible := true;
Animate1.Active := true;
for i := 0 to dbgrid2.SelectedRows.Count -1 do
begin
with query1 do
begin

DetailTab.GotoBookmark(TBookmark(DBGrid2.selectedRows[i]));
if active then Close;
SQL.Clear;
SQL.Add(' Insert into "'+ordbogTab+'" (lektionstitel_ID,
fremmedsprog, modersmål, anden_betydning, ordklasse, køn, kommentarer,
eksempel_fremmedsprog, eksempel_modersmål)');
SQL.Add(' VALUES(:lektionstitel_ID, :fremmedsprog,
:modersmål,:anden_betydning, :ordklasse, :køn, :kommentarer,
:eksempel_fremmedsprog, :eksempel_modersmål) ');
Params[0].AsInteger := Til_LekID;
Params[1].AsString := detailTab.Fields[2].AsString; //
fremmedsprog
Params[2].AsString := detailTab.Fields[3].AsString; //
modersmål
Params[3].AsString := detailTab.Fields[4].AsString;
Params[4].AsString := detailTab.Fields[5].AsString;
Params[5].AsString := detailTab.Fields[6].AsString;
Params[6].AsMemo := detailTab.Fields[7].AsString;
Params[7].AsString := detailTab.Fields[8].AsString;
Params[8].AsString := detailTab.Fields[9].AsString;
if not Prepared then Prepare;
ExecSql;
end;

newGlosID := detailTab.Fields[0].AsInteger; // **** should
return value of last inserted record, but returns value of the first in the
loop ***

with query1 do
begin
if active then Close;
SQL.Clear;
SQL.Add('Insert into "'+mmTab+'" (Glose_ID, pic001,
pic002, pic003, sound001, sound002, sound003, film001, film002, film003)');
SQL.Add('VALUES(:Glose_ID, :pic001, :pic002, :pic003,
:sound001, :sound002, :sound003, :film001, :film002, :film003)');
Params[0].AsInteger := newGlosID;
Params[1].AsString := multimediaTab.Fields[1].AsString;
Params[2].AsString := multimediaTab.Fields[2].AsString;
Params[3].AsString := multimediaTab.Fields[3].AsString;
Params[4].AsString := multimediaTab.Fields[4].AsString;
Params[5].AsString := multimediaTab.Fields[5].AsString;
Params[6].AsString := multimediaTab.Fields[6].AsString;
Params[7].AsString := multimediaTab.Fields[7].AsString;
Params[8].AsString := multimediaTab.Fields[8].AsString;
Params[9].AsString := multimediaTab.Fields[9].AsString;
if not Prepared then Prepare;
ExecSql;
end;

end;
end;
end;


// kopier lektion
If Radiogroup1.ItemIndex = 1 then
begin
if dbgrid1.SelectedRows.Count = 0 then
Application.MessageBox('Ingen lektion markeret!'+chr(13)+
' Klik venstre museknap'+chr(13)+
' for at vælge kilde- og mållektion',
'Kopiering', 16)
else
begin
CopyIt := true;

TempTable.Active := false;
TempTable.emptyTable;
TempTable.Active := true;
Animate1.Visible := true;
Animate1.Active := true;

with query2 do
begin
If active then Close;
Sql.Clear; // kopier rows fra kilde til temptabel
Sql.Add(' Insert into "'+TMPtab+'" ');
Sql.Add(' Select * from "'+ordbogTab+'" where
LektionsTitel_ID = CAST(:Fra_LekID AS Integer) ');
ParamByName('Fra_LekID').AsInteger := Fra_LekID;
if not Prepared then Prepare;
ExecSql;
end;

with Query1 do // opdater lekid
begin
close;
SQL.Clear;
SQL.Add(' Update "'+TMPtab+'" SET LektionsTitel_ID =
:Til_LekID');
ParamByName('Til_LekID').AsInteger := Til_LekID;
Prepare;
ExecSQL;
end;


with query2 do
begin
If active then Close;
Sql.Clear; // kopier rows fra temptabel til kilde
Sql.Add(' Insert into "'+ordbogTab+'" ');
Sql.Add(' Select * from "'+TMPtab+'" ');
if not Prepared then Prepare;
ExecSql;
end;
end;
TempTable.Active := false;
TempTable.emptyTable;
TempTable.Active := true;
end;

// opdatér antal gloser i titletab
if CopyIt = true then
begin
with Query1 do
begin
close;
sql.clear;
sql.add('Select count(*) as antal_gloser '+
' from "'+ordbogTab+'" '+
' where lektionstitel_ID = :lektions_ID');
ParamByName('lektions_ID').AsInteger := Til_LekID;
Open;
end;

antal_glos := Query1.FieldByName('antal_gloser').AsInteger;

with Query1 do
begin
close;
SQL.Clear;
SQL.Add(' Update "'+titleTab+'" SET Antal_Gloser = :antal_glos');
SQL.Add(' where LektionsTitel_ID = :lektions_ID');
ParamByName('antal_glos').AsInteger := antal_glos;
ParamByName('lektions_ID').AsInteger := Til_LekID;
Prepare;
ExecSQL;
end;
end;


Animate1.Visible := false;
Animate1.Active := false;

DetailTab.Active := false;
DetailTab.TableName := ordbogTab;
DetailTab.Active := true;
DetailTab.refresh;
Table1.Refresh;
Table3.Refresh;
multimediaTab.Refresh;
DBGrid1.Refresh;
end;



Back to top
Bill Todd (TeamB)
Guest





PostPosted: Fri Apr 02, 2004 5:49 pm    Post subject: Re: Att.: Bill Todd Re: table.last does not point to las Reply with quote

Since you are inserting the record using a SQL INSERT and viewing the
data with a TTable you will not see the record inserted by the SQL
statement. The new record will be placed in the table in primary key
order. To find it with a TTable you will have to call TTable.Refresh
then search for the record using FindKey or Locate.

What you are doing is very inefficient. It would be faster and easier
to insert the new record into the TTable.

--
Bill (TeamB)
(TeamB cannot respond to questions received via email)
Back to top
Georg Beyer Clausen
Guest





PostPosted: Fri Apr 02, 2004 9:40 pm    Post subject: Re: Att.: Bill Todd Re: table.last does not point to las Reply with quote

Quote:
What you are doing is very inefficient. It would be faster and easier
to insert the new record into the TTable.

Bill,
thansk for helping.
Could you give some hints to insert the new record into the TTable?

TIA,
Georg



Back to top
Bill Todd (TeamB)
Guest





PostPosted: Fri Apr 02, 2004 10:25 pm    Post subject: Re: Att.: Bill Todd Re: table.last does not point to las Reply with quote

Table1.Insert;
Table1.FieldByName('SomeField').AsString := 'ABC';
Table1.Post;

After you call Post the new record will be the current record.

--
Bill (TeamB)
(TeamB cannot respond to questions received via email)
Back to top
Georg Beyer Clausen
Guest





PostPosted: Fri Apr 02, 2004 11:57 pm    Post subject: Re: Att.: Bill Todd Re: table.last does not point to las Reply with quote

"Bill Todd (TeamB)" <no (AT) no (DOT) com> skrev i en meddelelse
news:60qr60lmv6b4n2esrrdoja85vg69rhia8o (AT) 4ax (DOT) com...
Quote:
Table1.Insert;
Table1.FieldByName('SomeField').AsString := 'ABC';
Table1.Post;

After you call Post the new record will be the current record.

--
Bill (TeamB)
(TeamB cannot respond to questions received via email)

thanks, I will try this,

regards,
Georg



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (Desktop) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.