Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collecting Multiple Lines From An ADO Query 1

Status
Not open for further replies.

PaidtheUmpire

Programmer
Jan 4, 2004
105
AU
I have a problem.

I currently have a system which uses a ADOQuery to do the following SQL

SELECT UserNo, UserLoc, UserName
FROM Details
WHERE UserLoc = 'Australia';

This query will have 10 (TEN) resulting lines. Is there an easy way to get all ten lines into something like an array or listbox?

So i can go through each of the UserNo and format them correctly one at a time?

Thanks. :)
 
Hi PaidTheUmpire,

here's some (untested) sample code that creates a query on the fly...(don't need to drop component on a form)

Code:
procedure DoTheQuery;
    TmpQry   : TADOQuery;
    Userno   : integer;
begin
 TmpQry:=TADOQuery.Create(Self);
 TmpQry.Connection:=DbConnection;
 TmpQry.SQL.Text:='SELECT UserNo, UserLoc, UserName'#13#10'FROM Details'#13#10'WHERE UserLoc ='+QuotedStr('Australia');
 TmpQry.Active:=True;
 if TmpQry.RecordCount > 0 then
  while not TmpQry.EOF do
   begin
    Userno:=TmpQry.FieldByName('UserNo').AsInteger;
    ... // do stuff
    TmpQry.Next;
   end; 
end;

Daddy

--------------------------------------
What You See Is What You Get
 
You say you want to format the fields from your query in some way. Presumably because you want to display the formatted fields in a data control such as a TDBGrid or TDBEdit?

There is no need to read the values into an array first.

The best way to do this is to use the field's OnGetText event handler.

A trivial example: if you wanted the UserLoc field to be formatted in upper case then your GetText handler would look something like:
Code:
procedure TForm1.ADOQuery1UserLocGetText(Sender: TField; var Text: String;
  DisplayText: Boolean);
begin
  text := UpperCase(Sender.AsString);
end;
You may need to write a SetText handler if you wish to update your table.


Andrew
Hampshire, UK
 
Thanks Daddy, (That sounds weird, my dad knows nothing!)

I'll have a go using this code.
Also by "Format" i meant do sums etc on the results. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top