• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 611
  • Last Modified:

Field data to TStrings

Is it possible to copy all the items in a particular field of a dataset into a TStrings variable, without loading them manually using a loop?

Something like...

>  TStrings1 := Dataset1.fielditems('FIELD1');

Instead of...

>  For i := 1 to Dataset1.Recordcount do
      TSrings1.add(Dataset1.fieldByName('FIELD1'));
0
ziche
Asked:
ziche
  • 5
2 Solutions
 
kretzschmarCommented:
??
i would say no

because you will one field of all records add into a stringlist
-> you have to navigate through the dataset

even if i'm on a wrong track with above and you meant
to add all fields of one record into the stringlist
-> alsi in this case you have to iterate through the fieldlist

anyway don't be a lazy coder

meikl ;-)
0
 
kretzschmarCommented:
btw to load one field into a stringlist:

dataset.open;
while not dataset.eof do
begin
  AList.Add(Dataset.FieldByName('FieldNameHere').AsString);
  dataset.next;
end;
dataset.close;

dataset can be any derived component from TDataset

meikl ;-)
0
 
Wim ten BrinkSelf-employed developerCommented:
No. Now back to work, lazy coder! ;-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kretzschmarCommented:
just to ask, why a c-grade?
0
 
zicheAuthor Commented:
Well, if you check my question, I asked for a 1 line solution to my problem, and I specifically sad I didn't want to use a loop, this is exactly what you gave me sighting that a 1 line solution was not possible.
0
 
kretzschmarCommented:
even a no, not possible is an answer

but delphi is expandable

just make a global procedure like

procedure fillMyStringList(const AList : TStrings; const ADataset : TDataset; AFieldName : String);
var
  dsWasOpen : Boolean;
  bm : TBookmark;
  s : String;
begin
  if not assigned(Alist) then
    raise Exception.Create('No valid List given!');
  if not assigned(ADataset) then
    raise Exception.Create('No valid DataSet Given');
  if ADataset.Active then
  begin
    dsWasOpen := true;  
    bm := ADataset.GetBookmark;
  end
  else
  begin
    dsWasOpen := false;  
    try
      ADataset.open;
    except
      rasie Exception.Create('Error opening Dataset');
    end;
  end;
  ADataset.DisableControls;
  ADataset.First;
  try
    try  //check FieldName
      s := Dataset.FieldByName(AFieldName).AsString;
    except
      raise exception.Create('FieldName or Content invalid');
    end;
    try
      while not ADataset.eof do
      begin
        AList.Add(ADataset.FieldByName(AFieldName).AsString);
        ADataset.next;
      end;
    except
       raise Exception.Create('Problem during iterating Dataset');
    end;
  finally
    if dsWasOpen then
    begin
       ADataset.GotoBookmark(bm);
       ADataSet.freeBookmark(bm);
    end
    else
      if  ADataset.Active then
        ADataset.Close;  
    ADataset.EnableControls;
end;

now you can use a oneliner like

fillmylist(listbox1.items,myquery,'MyFieldName');

just from head

meikl ;-)
0
 
kretzschmarCommented:
puu, i do not agree with the c-grade,
you have to explain this
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now