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

TQuery help?

Status
Not open for further replies.

cold25

Programmer
Feb 11, 2002
41
US
I'm trying to do a TQuery that just executes an SQL statement that the user enters into a TEdit box called edit1.
The SQL statement I'm entering into the TEdit box is as follows: SELECT * FROM "021103.dbf" WHERE EXS = "t"
(021103.dbf is the table displayed by the grid, which I'm querying, and EXS is a valid column heading in the table.)
After I enter this SQL statement and execute it, nothing happens to the DBGrid at all. It doesn't clear, it doesn't
post any new results of any kind, it doesn't change at all from how it previously was (listing the entire table.)
The code for my TQuery is as follows:

Procedure TForm1.TQuery1;
begin
try
// activate the TDatabase component
Database1.Connected := True;
// assign user input as the data value to select

SQLstmt := edit1.Text;
//***********************************************
//Put in the statements to launch the SQLstmt at this point.
Query1.Close; Query1.SQL.Clear;
Query1.SQL.Add(SQLstmt);
Query1.Prepare;

Query1.Open;
//***********************************************

finally

end;
end;


Could anyone please tell me if something is wrong with the SQL statement I'm entering, or if I'm missing a statement
that is required to launch the SQLstmt? It seems that however I change my SQL statement around, nothing works.
Any help would be greatly appreciated, since I have never succeeded in getting a TQuery to return a set of
records to me yet, even an incorrect set.


cold25
 
If you're executing a SELECT statement then you will need to close / open (set Active = False / Active = True) the TQuery to redisplay the result-set. (Assuming that you have a correctly hooked in DataSource and DBGrid).
If you're looking at running an INSERT, UPDATE or DELETE command then you will need to execute the query rather than close / open it.
In this case you should use the syntax : Query1.Prepare; and then Query1.ExecSQL; (as in Execute SQL).
I'd personally favour a TMemo rather than TEdit as you can more easily see what you're doing and can load the lines in using the syntax Query1.SQL.Text := Memo1.Lines; (I think off the top of my head - or something very similar).
I hope that this sets you in the right direction.

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top