delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback



Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi








Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


How to get field values of a dataset as comma text ?Format this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
DB-General
Skill Level:
Scoring:
Last Update:
01/04/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet database fields commatext dynamic query
Times Scored:
4
Visits:
5357
Uploader: Sunish Issac
Company: sunish.net
Reference: N/A
 
Question/Problem/Abstract:
How to get field values of a dataset as comma text ?
Getting the unique field values (strings of course) as comma text can be a big advantage in populating any TStrings descendant. The following functions implement it with respect to a table and also on TBDEDataset.
Answer:



Getting the unique field values (strings of course) as comma text can be a big advantage if you want to fill in a List box or CheckedListBox or for that matter a PickList of DBGrid.

Here are two functions that will let you get the field values as CommaText.The first one gets it from a table given the databasename ,tablename and field name. The second function retrieves it from a TBDEDataSet given the dataset  and field name. The components used in the functions are created at runtime so you don't require a component to be added to the form per se, but the respective units should be added in the uses clause.

The idea is to use a query to get just the required field values. A for loop is used to concatenate the values with a comma in between. The use of DISTINCT in the SQL ensures that there are no repeated entries.
The second function, which works with a dataset, uses a BatchMove component to move the data to a table and then does the function of creating a commatext string.

The Commatext can be assigned to any TStrings descendant making stuff like ChecklistBox.Items.CommaText := GetCommaTextFromdb(table.DatabaseName, 'fieldName', 'Tablename'); possible.

function GetCommaTextFromdb(const Dbname, dbField, Tablename: string): string;
var
  i: integer;
  QryTemp: TQuery;
  sFieldname: string;
begin
  Result := '';
  QryTemp := TQuery.Create(nil);
  with QryTemp do
  begin
    DatabaseName := Dbname;
    SQL.Clear;
    SQL.Add('SELECT DISTINCT ' + dbField + ' FROM ' + Tablename);
    Active := True;
    First;
    for i := 0 to QryTemp.RecordCount - 1 do
    begin
      sFieldname := FieldByName(dbField).AsString;
      if (sFieldname <> '') then
      begin
        Result := Result + '"' + (sFieldname) + '"';
        if i <> (QryTemp.RecordCount - 1) then Result := Result + ',';
      Next;
    end;
    Active := False;
  end;
  QryTemp.Free;
end;


function GetCommaTextFromDataSet(Dataset: TBDEDataSet; dbField: string): string;
var
  i: integer;
  QryTemp: TQuery;
  sFieldname: string;
  BatchMove: TBatchMove;
  TempOutTable: TTable;
begin
  Result := '';
  QryTemp := TQuery.Create(nil);
  BatchMove := TBatchMove.Create(nil);
  TempOutTable := TTable.Create(nil);
  TempOutTable.TableName := 'TempOutTable';

  if Dataset is TQuery then QryTemp.DatabaseName := TQuery(Dataset).DatabaseName
  else QryTemp.DatabaseName := TTable(Dataset).DatabaseName;

  TempOutTable.DatabaseName := QryTemp.DatabaseName;

  with BatchMove do
  begin
    Mappings.Clear;
    Source := Dataset;
    Destination := TempOutTable;
    Mode := batCopy;
    Execute;
  end;

  with QryTemp do
  begin
    SQL.Clear;
    SQL.Add('SELECT DISTINCT ' + dbField + ' FROM TempOutTable');
    Active := True;
    First;

    for i := 0 to QryTemp.RecordCount - 1 do
    begin
      sFieldname := FieldByName(dbField).AsString;
      if (sFieldname <> '') then
      begin
        Result := Result + '"' + (sFieldname) + '"';
        if i <> (QryTemp.RecordCount - 1) then Result := Result + ',';
      end;
      Next;
    end;
    Active := False;
  end;
  TempOutTable.DeleteTable;
  QryTemp.Free;
  BatchMove.Free;
  TempOutTable.Free;
end;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
A small improvement...
    Carlos Alberto Longen (Jan 5 2002 12:44AM)

if you put this two lines of codes, the GetCommaTextFromdb and GetCommaTextFromDataSet functions will get a better performance:

Before Active := true, put DisableControls; and after Active := false put EnableControls;
Respond

RE: A small improvement...
Sunish Issac (Jan 5 2002 6:26AM)

Thanks, Iam updating the code as your suggestion.
Respond

RE: A small improvement...
Sunish Issac (Jan 5 2002 6:32AM)

You can still call the enable and disable controls before calling this function.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


   


  Community Ad of
D. Wischnewski
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)