ramiromd
ramiromd

Reputation: 2029

Change database query at runtime

I'm trying to filter a TDBGrid via a TEdit, handling the TEdit onChange event. I have a TIBQuery to list all the records in the grid:

SELECT id,obra,cliente,fecha,nro_estudio,sondeo FROM proyecto;

When the user inserts characters in the TEdit, the grid must be refreshed by the Cliente field.

My code tries to do this, but when it executes the handler, the query returns an empty resultset. I'm using Firebird 2.5 and IB Components.

Relevant Code:

query := Self.qryTodos;
query.Close();
query.SQL.Clear();
query.SQL.Add('SELECT id,obra,cliente,fecha,nro_estudio,sondeo FROM proyecto WHERE cliente LIKE :Cliente');
query.ParamByName('Cliente').AsString := QuotedStr('%'+Self.busqueda.Text+'%');
query.Open();
DMConnect.Transaction.Commit();

Upvotes: 0

Views: 2180

Answers (2)

Jason
Jason

Reputation: 2572

If you're surrounding your search term in % I would use the containing clause, so you could write your query like:

query := Self.qryTodos;
query.Close();
query.SQL.Clear();
query.SQL.Add('SELECT id,obra,cliente,fecha,nro_estudio,sondeo FROM proyecto WHERE cliente CONTAINING :Cliente');
query.ParamByName('Cliente').AsString := Self.busqueda.Text;
query.Open();

and remove the Commit statement as @LightBulb stated. This will work if you want to include the % in your search term for any reason.

Upvotes: 0

Curt
Curt

Reputation: 5722

You don't need QuotedStr when you're supplying string parameters. By using it, you make it not match any more (e.g. "String" becomes '''%String%''')

Upvotes: 2

Related Questions