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
418 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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