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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DBGrid

Status
Not open for further replies.

morfasie

IS-IT--Management
Mar 28, 2004
85
ZA
Hi

I have a dbgrid and a datasource.

In my code I have a query running and I then set the query to the dataset of the Datasource.

I want to edit the dbgrid but can't seem to get it right!

here is my code:
Datamodule1.Query2.Close;
Datamodule1.Query2.SQL.Clear;
Datamodule1.Query2.SQL.Add('Select surname,firstname,dateofbirth,age,ID,premium');
Datamodule1.Query2.SQL.Add('From extendedfamily');
Datamodule1.Query2.SQL.Add('Where `Main Member ID` = '+quotedstr(Datamodule1.Query1.Fields.FieldByName('id').AsString));
Datamodule1.Query2.Open;


DataSource1.DataSet := Datamodule1.Query2;
DBGrid1.DataSource := DataSource1;
 
Are you using Cached Updates or is RequestLive set to true? From the looks of your SQL, you could potentially turn on RequestLive.

Also, rather than resetting the SQL every time you run the query, I would suggest using parameters. You'll set up the SQL in the query like this:
Code:
Select surname, firstname, dateofbirth, age, ID, premium
from extendedfamily
where 'Main Member ID' = :ID
You also need to set the DataType (ftString) and the ParamType (ptInput) of the parameter in the IDE (or you can do it in code...)

Your code to execute the query would then look like this:
Code:
Datamodule1.Query2.Close;
Datamodule1.Query2.ParamByName('ID').AsString :=  
    Datamodule1.Query1.Fields.FieldByName('id').AsString;
Datamodule1.Query2.Open;
Using parameters automatically takes care of handling the quotes and other formatting issues that otherwise you'd have to handle in your code. Also, depending on the database, any time the query is opened after the first time, it will return a result faster because the query has already been parsed or "prepared". If you change the SQL, the database will reparse the query, even if it's similar to the previous time the query was run.

-Dell


A computer only does what you actually told it to do - not what you thought you told it to do. --Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top