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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why is EOF ignored in this code? 3

Status
Not open for further replies.

delphiman

Programmer
Joined
Dec 13, 2001
Messages
422
Location
ZA

Can somone please throw some light on this?

The ShowMessage proves that the routine
progresses through the TQuery - but then stops on
the last record. Ignoring the EOF. Why?

procedure TfrmThisForm.btn1Click(Sender: TObject);
var
ABookmark : TBookmark;
begin
with dmThisModule.qryGroc do
begin
dmThisModule.qryGroc.Disablecontrols;
try
ABookMark := dmThisModule.qryGroc.GetBookmark;
dmThisModule.qryGroc.First;
while not dmThisModule.qryGroc.Eof do
begin

ShowMessage('qryGrocItem = ' + (dmThisModule.qryGrocItem.value));

dmThisModule.qryGroc.Next;
end;
finally
dmThisModule.qryGroc.GotoBookmark(ABookMark);
dmThisModule.qryGroc.FreeBookMark(ABookMark) ;
dmThisModule.qryGroc.enablecontrols;
end;
end;
end;
 
The only thing wrong that I can see is that you are referencing
Code:
 qryGrocitem
in the ShowMessage while you are using
Code:
 qryGroc
in the loop.
 
>The only thing wrong that I can see is that you are >referencing qryGrocitem in the ShowMessage while you are >using qryGroc in the loop.

In fact that is not wrong.

I am looping on qryGroc and displaying the progress (record-by-record) by showing the contents of qryGrocitem. Which is how I am able to tell that the entire qry is being scanned - coming to a stop at the end. Without coming out of the loop.

My problem. :-)


 
hi

You don't need the dmThisModule.qryGroc prefixes as you've go the With. The getBookmark should be outside the TRY aswell.


procedure TfrmThisForm.btn1Click(Sender: TObject);
var
ABookmark : TBookmark;
begin
with dmThisModule.qryGroc do
begin
Disablecontrols;
ABookMark := GetBookmark;
try
First;
while not Eof do
begin
ShowMessage('qryGrocItem = ' + (dmThisModule.qryGrocItem.value));

Next;
end;
finally
GotoBookmark(ABookMark);
FreeBookMark(ABookMark) ;
enablecontrols;
end;
end;
end;

hope that helps
lou

 
Can you show us the SQL used in the qryGrocItem query ?

Have you used the debugger to single step through the code ?

What makes you think that the program has stopped on the last record ?

Have you got multiple records in the table with the same value of qryGrocItem ?

Andrew
 
weez (Programmer)

I have used that code with a TTable and Paradox for years and it works perfectly. I can't understand why it wont work with a TQuery.


towerbase (Programmer)

>Can you show us the SQL used in the qryGrocItem query ?

select *
from groc t1, selstore t2
where (store_no = t2.store_no) and (area_no = t2.area_no)
order by item

>Have you used the debugger to single step through the code ?

It doesn't show what's happening. That's why I use the ShowMessage.

>What makes you think that the program has stopped on the >last record ?

Because I know what's in the Table (only 5 records in it) and ShowMessage shows me each in turn - and then stops (repeats) on the last one. If I take Show Message out I end up in an endless loop. Which I have to stop with the TaskMaster. (W2K)

>Have you got multiple records in the table with the same >value of qryGrocItem ?

It will happen. But isn't the case now.



 
...coming to a stop at the end. Without coming out of the loop....

Which is it? does the program stop or does it continue to loop? I don't see how it can do both at the same time.

...where (store_no = t2.store_no) and (area_no = t2.area_no)...

should probably be:
where (t1.store_no = t2.store_no) and (t1.area_no = t2.area_no)

I'm surprised it can work at all without having store_no and area_no qualified wherever referenced.

 
I don't understand how a data value is placed into qryGrocItem by the Query.

(Perhaps I've been coding queries properly in the past)

Where is qryGrocItem defined in your program ?

Andrew
 
>Which is it? does the program stop or does it continue to >loop?

Program continues to loop. Routine stops progression through the table - having reached EOF - but then loops. EOF being of no effect. ShowMessage continuing to show the content of qryGrocItem in the last record.

>should probably be:
where (t1.store_no = t2.store_no) and (t1.area_no = t2.area_no)

>I'm surprised it can work at all

So am I! Yet it does otherwise. Of course I have now fixed that (Thanks!) but it has not solved the problem.
 
As Spock would say, Fascinating...
What happens if you code it this way?
[blue]
Code:
  while not dmThisModule.qryGroc.Eof do
    begin
      ShowMessage('qryGrocItem = ' + (dmThisModule.qryGrocItem.value));
      dmThisModule.qryGroc.Next;
    end;
[/color]

Delphi sometimes does funny things with the with..do construct.

 
Is this infuriating? Or is it infuriating? It is wasting so much time!!

If I place a break-point at B below I find that Delphi simply oscillates between Á and B.

Meanwhile from the ShowMessage I have established that Delphi is in fact scanning all the records in the Query. Because I know what each (of 5) records contains.

But for some reason EOF (or Eof) in line A is being ignored. Where I came it. :-(

with dmThisModule.qryGroc do
begin
dmThisModule.qryGroc.Disablecontrols;
try
GrocBookMark := ThisModule.qryGroc.GetBookmark
dmThisModule.qryGroc.First;
while not Eof do .... A
begin
// ShowMessage (Wha wha wha);
dmThisModule.qryGroc.Next; ...B
end;

?? Anyone ?? :-)
 
I forgot to mention that I now also have the following preceding the above code. But it makes no difference.


with dmThisModule.qryGroc do
begin
SQL.Clear;
SQL.Add ('SELECT *');
SQL.Add ('FROM GROC');
Active := True;
end;
with dmThisModule.qryGroc do
begin
dmThisModule.qryGroc.Disablecontrols;
 
I don't see you opening the query or calling execute. Also make sure the property QueryAllRecords is set to true.
 
I think it would help if you could cut and paste the entire function from your source code without doing any editing. There is a nagging doubt that you are omitting some vital bits of evidence!

Also it would help if you could use the [ code ] and [ /code ] formatting tools.

Andrew
 
FIXED!!

I really appreciate the interest and input from everyone - and this great website. And so having found the problem I feel it only fair to share it with everyone.

It was actually somewhat elusive as it has nothing to do with EOF and the code which I have above. All of which work perfectly.

Even with my omitting the t as pointed out by zathras. An eye-opener being that it actually works like that!!

My problem was the result of some obscure garbage in UponScroll of qryGROC which (for reasons which escape me - having put it there ages ago) I was doing an Edit followed by a Post.

I discovered it upon completely replacing qryGROC - and ran the routine without any EventHandlers. And it worked perfectly. I then replaced them one by one and the moment I replaced UponScroll bingo!!.

At which point I also realised that I didn't need it.

Thanks again everyone!
 
Suggestion: don't use design-time components if they have no design-time use.

For instance:
Code:
  with TQuery.Create(nil) do
  try
     DatabaseName := 'Fred';
     SQL := 'SELECT * FROM PLANTS';
     while not EOF do
     begin
      /// do something
         Next;
     end;
  finally
     Free;
  end;

This prevents any wierd design-time stuff from affecting the process.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top