Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with Code [Loop]

Posted on 2007-10-11
15
Medium Priority
?
179 Views
Last Modified: 2010-04-05
Hi all,

When I run the code below it takes 100% CPU and I have to kill the process.

Can someone help to make it run properly please?

Here is the code:

////////////////////////////////////////////////////
/// Procedure here
///////////////////////////////////////////////////
procedure TForm1.ExcludeListBoxItems;
var Exclude: TStringList;
    i: integer;
    Query: string;
begin
 Exclude := TStringList.Create();

 try
  for i := 0 to sListBox1.Count - 1 do
   Exclude.Add(QuotedStr(sListBox1.Items[i]));

  Query := Format('SELECT * FROM products where not (AccountID in (%s))', [Exclude.CommaText]);
 finally
  Exclude.Free();
 end;
end;

////////////////////////////////////////////////////////////////////////
/// Button Click Procedure here
///////////////////////////////////////////////////////////////////////
procedure TForm1.sButton5Click(Sender: TObject);
var TheField : String;
begin
  with DBAdvGrid1.DataSource.DataSet do
         begin
         First;
         DisableControls;
         try
            while not EOF do
               begin
               if DBAdvGrid1.DataSource.DataSet.FieldByName('AccountID').AsString = ADOQuery1.FieldByName('AccountID').Value then
               TheField:=(ADOQuery1.FieldByName('AccountID').AsString);
               Memo1.clear;
               //Memo1.Lines.Add(Format('SELECT * From products WHERE AccountID=''%s''',[TheField]));
               ExcludeListBoxItems;
               sListBox1.Items.Add(TheField);
               //exit; //Stop it here for now
               //...Do stuff there
   

               Next;
            end;
         finally
            EnableControls;
         end;
      end;
end;

Thanks

ST3VO
0
Comment
Question by:ST3VO
  • 8
  • 7
15 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 20055786
what is it you are trying to achieve.

please be aware that the function I gave you (which you now called "ExcludeListBoxItems") does not result anything or do anything actively.
It is just an example, in the Query variable in that procedure you'll find the query you asked for, but the procedure does not do anything itself!
0
 

Author Comment

by:ST3VO
ID: 20055901
I've modified the procedure to:

procedure TForm1.ExcludeListBoxItems;
var Exclude: TStringList;
    i: integer;
    Query: string;
begin
 Exclude := TStringList.Create();

 try
  for i := 0 to sListBox1.Count - 1 do
   Exclude.Add(QuotedStr(sListBox1.Items[i]));
   adoquery1.active := false;
   Memo1.Clear;
   Memo1.Lines.Add (Format('SELECT * FROM products where not (AccountID in (%s))', [Exclude.CommaText]));

    adoquery1.active := false;
    adoquery1.SQL.Assign(memo1.Lines);
    adoquery1.Active := true;

 finally
  Exclude.Free();
 end;
end;

So, it now executes the Query.

What I'm tring to do is more extensive which I can now make work one at the time but I need to automate
so everything is done one after the other in just one click.

Here is what it will do:

1. Connect to FTP Server and download a zip file containing a csv file.
2. Once downloaded extract the csv and import it.
3. Run the loop to separate the different contents for every account.
    3a: Some every record with accounrtid (x)
    3b: run the filtered query and display the contents on the DBGrid
    3c: Save to results back to a SMALLER CSV named AccountID.CSV with only the results of that     account.
   3d Loop next but AccountID leaving out the previous account just the next account
4. This created 1500 different csv files from 1 large csv
5. Zip all created contents
6. Upload ack to server.

END.

This is that I need to do :o)

0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20055937
> What I'm tring to do is more extensive which I can now make work one at the time

one what at a time?
How do you do this one at a time? Can you show code?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:ST3VO
ID: 20055966
Basically I call every procedure one after the other in order!

Is that a bad way to do it?
0
 

Author Comment

by:ST3VO
ID: 20056010
By the way...your SQL code is producing:

This SQL String:

SELECT * FROM products where not (AccountID in ())

I'm getting a Syntax error :o/
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20056117
if you get that query it means the listbox is empty
0
 

Author Comment

by:ST3VO
ID: 20056147
Opp...Your right....sorry a about that...I forgot that it was the first record and I need to populate it first!
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20056200
you say you call a bunch of procedures after each other.
And that is for one 'whatever'.. right?
0
 

Author Comment

by:ST3VO
ID: 20056236
one 'whatever'.. right?  <--- Could you please rephrase this? I cannot understand what you mean there. Sorry about that!
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20056249
> What I'm tring to do is more extensive which I can now make work one at the time

what did you mean with 'one at a time'. One what, one ftp site, one file on a ftp site, one database?
0
 

Author Comment

by:ST3VO
ID: 20056476
One Procedure at the time.

For example:

1. FTP Connect Procedure
2. FTP Download File Procedure
3. Extract Archieve Procedure
4. Load csv file procedure

etc...

0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20056504
so what is the problem then, just call them one by one and you're done, or do you mean something else?
0
 

Author Comment

by:ST3VO
ID: 20057054
Well, the problem is that It's so busy 100% CPU that I cannot see the things changing ...for example a new item is added to the listbox on every loop and I cannot see it happen...The DBGrid is also not refreshing with the new data.

Should I need to create some space between the events?


0
 
LVL 19

Accepted Solution

by:
MerijnB earned 2000 total points
ID: 20057109
> for example a new item is added to the listbox on every loop

"loop"? you were just calling a bunch of procedures, so now I understand you call them in a loop?

you can try to call
 Application.ProcessMessages();
each time you do a run in your loop
0
 

Author Comment

by:ST3VO
ID: 20057881
Yep...That worked :o)  

Thanks very much!

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month20 days, 16 hours left to enroll

810 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