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
417 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 174
How to fill array with TArray.Create? 14 79
Convert Jpg /PNG To GIF 5 120
Using idhttp to login to instagram 2 76
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

896 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now