Solved

How do I loop through a table, create a temp table with distinct fields then run a query on that temp table?

Posted on 2008-09-30
6
422 Views
Last Modified: 2012-05-05
Using Delphi 7 with Paradox/Local SQL.
I'm using a component (SMExport) which exports from a dataset to an excel file.
My table contains a client code field and a type field by which I want to filter. There are only two types and i'm running two separate queries with a function for each type where the component generates the excel file. This gives me two files filtered by type, but I need one file per client code. I'm assuming I need to loop through the main table, create a temp table where the records only have unique client code, then run the query on the temp table...repeat...but I have no idea how. Hope this made sense.

Here's my code for the first function:

function TfrmTransfer.TransferTechs:Boolean;
begin
     with Leads_Datamodule do
     begin
          Result:=False;
          qryTransferTechLeads.Close;
          qryTransferTechLeads.SQL.Clear;
          qryTransferTechLeads.SQL.Add('SELECT *');
          qryTransferTechLeads.SQL.Add('FROM Leads');
          qryTransferTechLeads.SQL.Add('WHERE LeadType ='+QuotedStr('Technicians'));
          qryTransferTechLeads.Open;
          ExportXLS.Dataset:=qryTransferTechLeads;
          ExportXLS.FileName:=MainForm.GetDataDir+'\Leads\data\Techs\test.xls';
          ExportXLS.Execute;
     end;
end;
0
Comment
Question by:Cilician
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 13

Expert Comment

by:rfwoolf
ID: 22606402
Your question's a little bit too dense for me to wade through.  Sorry, I probably would wade through it if I knew for sure I'd be able to help. Perhaps you can go through it and expand a bit.
Here's my initial response:
Why can't you just use Queries, set the SQL, open the queries and then export the dataset to excel format.
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 22606430
Yes please reformat your question by capitalising where necessary and removing ambiguity. for example "type field" is that a field called "Type" or is that a TField?
0
 

Author Comment

by:Cilician
ID: 22606502
My table Leads contains a field (among others) called ClientCode and a field called Type by which I want to filter. There are only two types and i'm running two separate queries with a function for each type where the component generates the excel file. This gives me two files filtered by Type, but I need one file per ClientCode. I'm assuming I need to loop through the main table, create a temp table where the records only have one unique ClientCode, then run the query on the temp table, generate the excel file with the ClientCode name......
Better?

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:SteveBay
ID: 22606744
I think I get it.
 
You need to do a primary query that returns a result set of unique ClientCodes and then loop though that result set and do the secondary queries that create your Excel Files.  One file per ClientCode with the ClientCode as the name of the Excel file.  
 
Is that right?
0
 
LVL 14

Accepted Solution

by:
SteveBay earned 500 total points
ID: 22606929
Perhaps something like this?
procedure TForm1.Button1Click(Sender: TObject);
begin
     Query1.Close;
     Query1.SQL.Clear;
     Query1.SQL.Add('SELECT ClientCode FROM Leads');
     Query1.SQL.Add('GROUP BY ClientCode');
     Query1.Active := True;
     While not Query1.Eof do
          begin
          qryTransferTechLeads.Close;
          qryTransferTechLeads.SQL.Clear;
          qryTransferTechLeads.SQL.Add('SELECT *');
          qryTransferTechLeads.SQL.Add('FROM Leads');
          qryTransferTechLeads.SQL.Add('WHERE LeadType ='+QuotedStr('Technicians'));
          qryTransferTechLeads.SQL.Add(' AND ClientCode ='+QuotedStr(Query1.FieldByName('ClientCode').AsString));
          qryTransferTechLeads.Open;
          ExportXLS.Dataset:=qryTransferTechLeads;
          ExportXLS.FileName:=MainForm.GetDataDir+'\Leads\data\Techs\'+ Query1.FieldByName('ClientCode').AsString' +.xls';
          ExportXLS.Execute;
          Query1.Next;
          end;
end;

Open in new window

0
 

Author Closing Comment

by:Cilician
ID: 31501592
So much simpler and better than what I was assuming to be the way to go. Thank you very much.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

690 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