delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
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







Startblatt.de






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 (0)


Extract field from DataSet into TStringListFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Database Others
Skill Level:
Scoring:
Last Update:
01/15/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet dataset extract export tstringlist lookup
Times Scored:
4
Visits:
5031
Uploader: Stewart Moss
Company: New Heights Software Developme
Reference: N/A
 
Question/Problem/Abstract:
To Extract a specific field from a dataset in a TStringList. Useful for
populating TlistBoxes that you dont want to be data aware etc...

Also has a handy lookup method.

Nice and quick and easy.
Answer:




{-----------------------------------------------------------------------------
Unit Name: unitDatasetExtract1

Creation Date: 04/06/02 00:39:21
Documentation Date: 04/06/02 00:39:21
Release Date: 15 Jan 2003
Version: 1.0

Author: Stewart Moss

Compiler version:
   Delphi 5 and Delphi 6 tested

Purpose:
  To Extract a specific field from a dataset in a TStringList. Useful for
  populating TlistBoxes that you dont want to be data aware etc...
  (Released originally on Delphi3000.com)

Description:
        At the moment it only support TStringLists but it will convert
        most TField.FieldType into a string

Notes:

  How to use
  ----------
   var
    DE : TDatasetExtract;
    tsl : TStringList;
    CustId : string;

   begin
     de := TDatasetExtract.create;
     tsl := TStringList.create;

     with de do
     begin
      dataset := Query1;
      FieldName := 'CustomerName';
      tsl := de.strings;
     end;              // with

     // Now we have a TStringList containing all the Customer names
     // which we can populate listboxes and other yummy stuff

     // or lets perform a lookup returning the customer ID knowing the Customer Name

     CustID := de.QuickSearch('CustomerName','Stewart Moss','CustomerID',[loCaseInsensitive]);

     de.free;
     tsl.free;


   end;



Dependancies:

History:

          Copyright 2002 by Stewart Moss
          All rights reserved.
          You must not modify this Unit Header.
-----------------------------------------------------------------------------}

unit unitDatasetExtract1;

interface

uses
classes, db{$IFDEF VER140}, variants{$ENDIF};

type
  
TDatasetExtract = class
  private
    
fDataset: TDataset;
    
fStrings: TStringList;
    
fFieldname: string;
    
fEmptyNulls: boolean;
    
fappend: string;
    
fprepend: string;

    
function FieldToString(Field: TField): string;
    
procedure ExtractStringList;

    
function Get_Strings: TStringList;
    
procedure Set_Dataset(const Value: TDataset);

  
public
    constructor
create;
    
destructor destroy; override;

    
function quickSearch(searchField,
      
searchValue, ReturnField: string; Locateoptions: TLocateOptions): string;

  
published

    property
append: string read fappend write fappend;
    
property prepend: string read fprepend write fprepend;
    
// You have the option of appending and prepending any information
    // to the begining of each record.
    // Useful for creating delimited records easily (ie you can just add the StringList
    // items together) (maybe not really that useful :P )

    
property EmptyNulls: boolean read fEmptyNulls write fEmptyNulls;
    
// Whether or not nulls must be translated into blank values,
    // or ignored

    
property FieldName: string read fFieldname write fFieldname;
    
property Dataset: TDataset read fDataset write Set_Dataset;

    
property Strings: TStringList read Get_Strings;
  
end;

implementation

{ TDatasetExtract }

constructor TDatasetExtract.create;
begin
  
FDataset := nil;
  
fstrings := TStringlist.create;
  
emptynulls := true;
end;

destructor TDatasetExtract.destroy;
begin
  
fstrings.free;
  
inherited;
end;

procedure TDatasetExtract.ExtractStringList;
var
  
field: tfield;
begin
  
fstrings.clear;
  
with fdataset do
  begin
    try
      
disablecontrols;
      
First;
      
while not eof do
      begin
        
field := FieldByName(FFieldname);
        
if not varisnull(field.asvariant) then
          
fstrings.add(Fprepend + FieldToString(Field) + Fappend) else
          if
emptyNulls then
            
fstrings.add(fprepend + fappend);
        
next;
      
end; // while
    
finally
      
first;
      
enablecontrols;
    
end; // finally
  
end; // with
end;

function TDatasetExtract.FieldToString(Field: TField): string;
begin
  
result := field.AsString;
end;

function TDatasetExtract.Get_Strings: TStringList;
begin
  if
(fdataset <> nil) and (ffieldname <> '') then
    
extractStringList;
  
Result := fStrings;
end;

function TDatasetExtract.quickSearch(searchField,
  
searchValue, ReturnField : string; Locateoptions: TLocateOptions): string;
{-----------------------------------------------------------------------------
  Procedure: TDatasetExtract.quickSearch
  Author:    StewartM
  Arguments: searchField, searchValue, ReturnField : string; Locateoptions: TLocateOptions
  Result:    string

  Searches for a field value in a specified field name and returns the specified field
  returns '' is nothing is found

  eg
    Ask for a customer id of 10 and return the customer name
-----------------------------------------------------------------------------}
begin
  with
fdataset do
  begin
    
DisableControls;
    
try
      
result := '';
      
if not locate(searchfield, searchvalue, locateoptions) then
        
exit;
      
result := fieldbyname(ReturnField).asstring;
    
finally
      
first;
      
enablecontrols;
    
end;
  
end;
end;

procedure TDatasetExtract.Set_Dataset(const Value: TDataset);
begin
  
fDataset := Value;
end;

end.








Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Shkolnik
 
   














 







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