Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I know if I'm on the last record in a TDataSet ? 2

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
How can I establish whether or not I'm sitting on the last record in a TDataSet (TTable or TQuery).
I'm looking at stepping through the entries in a result set (using a 'First' then 'while not EOF' idea) and doing a particular task with the last record - how do I know when I've hit this last record ?

I'm assuming it's something simple that I'm missing. :(

Any help would be appreciated.
Steve
 
If table1.EOF = True then

Failing that, you could add a Counter to your loop like this

var
Count, X : Integer
begin
X := 0;
Count := Table1.RecordCount;
while not Table1.EOF do
begin

{ Your code }

X := X + 1;
Table1.Next;
if X = Count then
begin
//On last record
end;
end;
end;

if EOF = True is probably the easiest way to go though

Arte Et Labore
 
It's looking likely that I'm having to resort to the 'RecordCount' idea.
I can't for the life of me get the 'EOF' idea to work.
I've got code along the lines of :

Table1.First;
while not Table1.EOF do
begin
if (Table1.EOF = True) then
begin
Showmessage('Put Code in here');
end;
Table1.Next;
end; // while not Table1.EOF do

Running this code - never actually outputs the message.
:(

Steve
 
Put the call to next above the "if Table1.EOF = True then" code. EOF doesnt recognise when you are on the last record of a file, it tells you when you have actually hit the end of the file. Because of this, you need to call another Next when on the last record of a table.

Arte Et Labore
 
I'm going to guess that the reason that your code never executes the message:

Table1.First;
while not Table1.EOF do
begin
if (Table1.EOF = True) then
begin
Showmessage('Put Code in here');
end;
Table1.Next;
end; // while not Table1.EOF do

Is becasue once it hits EOF (while not Table1.EOF do) it doesnt go back inside the begin / end statement.

try

Table1.First;
repeat
if (Table1.EOF = True) then
begin
Showmessage('Put Code in here');
end;
Table1.Next;
until Table1.EOF

OR
(although this would not be the best way to code that loop)

Table1.First;
while not Table1.EOF do
begin
Table1.Next;
if (Table1.EOF = True) then
begin
Showmessage('Put Code in here');
end;
end; // while not Table1.EOF do


Ill bet you hit the message then...
 
Thanks for the feedback guys - I'm now up and running as required.
Steve
 
Why don't you use TDataSet.Last which moves you to the last record?

Leslie
landrews@metrocourt.state.nm.us

There are 10 types of people in the world -
those who understand binary
and
those who don't!
 
if Table1.RecNo=Table1.RecordCount then
ShowMessage('I am in the lastest line!');
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top