-2

How do I get all the username values from the image below and can convert them into TStringList, strings or TMemo?

enter image description here

I tried the following code but it didn't work.

with q3 do
  var txResul:stringlist
  begin
    Close;
    SQL.Clear;
    SQL.Add('SELECT*FROM user');
    Open;
    Next;
  end;
  txtResult.Add(q1.FieldByName('username').AsString);
Michael
  • 34,340
  • 9
  • 58
  • 100
  • i try this but error... with q3 do var txResul:stringlist begin Close; SQL.Clear; SQL.Add('SELECT*FROM user'); Open; Next; end; txtResult.Add(q1.FieldByName('username').AsString); – Anton Widodo Yongga Dec 25 '16 at 13:06
  • 3
    Don't use "with"! It saves minimal time and easily leads to mistakes. Btw did you mean q3 or q1? – MartynA Dec 25 '16 at 13:25
  • 1
    Anton, don't use comments to add information to your question. Fix your question by using the [edit link](http://stackoverflow.com/posts/41321306/edit) to add that information to the actual question. – Disillusioned Dec 25 '16 at 13:33
  • 1
    That's why I told you how to do it properly. ;) btw You should also pay attention to the actual error message - it provides **very important** clues. Don't tell us 'you got an error' tell the actual error message. From your code comment it looks like you tried to declare a variable in the middle of your method `with q3 do var txResul:stringlist begin`. Delphi doesn't allow that. All variables must be declared at the beginning of the method. – Disillusioned Dec 25 '16 at 13:47

1 Answers1

6

Assuming your screenshot is a dataset you call do something like this:

procedure GetUserNames(DataSet : TDataSet; StringList : TStringList);
var
  Field : TField;
begin
  Field := DataSet.FieldByName('username');
  StringList.BeginUpdate;      
  try
    DataSet.DisableControls;
    try
      DataSet.First;
      while not DataSet.Eof do begin
        StringList.Add(Field.AsString);
        DataSet.Next;
      end;
    finally
      DataSet.EnableControls;
    end; 
  finally
    StringList.EndUpdate;
  end;
end;

You might use it like this

procedure TForm1.GetUsers;
var
  TL : StringList;
begin
  TL := StringList.Create;
  try
    GetUserNames(MyTable, TL);
    Memo1.Lines.Text := TL.Text;
  finally
    TL.Free;
  end;
end;

Btw, if you changed the second parameter of GetUserNames to a TStrings, as in GetUserNames(DataSet : TDataSet; Strings : TStrings) you could pass Memo1.Lines to it directly.

kobik
  • 20,439
  • 4
  • 54
  • 115
MartynA
  • 28,815
  • 3
  • 27
  • 68
  • can i add it to memo bro? – Anton Widodo Yongga Dec 25 '16 at 13:11
  • 2
    One extra useful thing to do is to bookmark the initial position in the dataset and return to that position before enabling controls. Otherwise every time `GetUserNames` is called, the grid moves to the last row. – Disillusioned Dec 27 '16 at 12:30
  • @CraigYoung, I agree with you. bookmarking is a good idea in some cases. but as I see it, my guess is OP does not uses a TDBGrid at all b/c if he did, he would not have used a Memo. – kobik Dec 28 '16 at 10:17