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
416 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

19 Experts available now in Live!

Get 1:1 Help Now