Mega Code Archive

 
Categories / Delphi / ADO Database
 

Extract field from DataSet into TStringList

Title: Extract field from DataSet into TStringList Question: 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.