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'));
zicheAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wim ten BrinkSelf-employed developerCommented:
No. Now back to work, lazy coder! ;-)
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.