Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
427 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
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

618 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