I'm trying to build a dynamic query where the user can select the field as part of the search.
My question is can I do this by using ParamByName?
Example :
MyQuery1.SQL := 'select * from mytable where :field = :value;'
MyQuery1.ParamByName('field').AsString := 'fieldname';
MyQuery1.ParamByName('value').AsString := 'myvalue';
MyQuery1.Execute;
The test didn't return any errors but it didn't return any results either when I reference MyQuery1.RecordCount , so I'm guessing it doesn't necessarily work this way, but if it does, what am I doing wrong? Or is there an easier way to do it? If so, could an example be provided?
TMyQuery and ParamByName
Re: TMyQuery and ParamByName
Try
MyQuery1.SQL := 'select * from mytable where &field = :value;
MyQuery1.MacroByName('field').AsString := 'fieldname';
MyQuery1.ParamByName('value').AsString := 'myvalue';
MyQuery1.Execute;
MyQuery1.SQL := 'select * from mytable where &field = :value;
MyQuery1.MacroByName('field').AsString := 'fieldname';
MyQuery1.ParamByName('value').AsString := 'myvalue';
MyQuery1.Execute;
Re: TMyQuery and ParamByName
As hkarmeh wrote correctly to solve the issue, you can use Macros. For example:alphawiz1 wrote: ↑Sun 14 Feb 2021 18:26 I'm trying to build a dynamic query where the user can select the field as part of the search.
My question is can I do this by using ParamByName?
Example :
MyQuery1.SQL.Text := 'select * from mytable where :field = :value;'
MyQuery1.ParamByName('field').AsString := 'fieldname';
MyQuery1.ParamByName('value').AsString := 'myvalue';
MyQuery1.Execute;
The test didn't return any errors but it didn't return any results either when I reference MyQuery1.RecordCount , so I'm guessing it doesn't necessarily work this way, but if it does, what am I doing wrong? Or is there an easier way to do it? If so, could an example be provided?
Code: Select all
MyQuery1.SQL := 'select * from mytable where &field = :value;'
MyQuery1.MacroByName('field').Value := 'fieldname'; //If you set macro with the AsString property, it will be quoted.
MyQuery1.ParamByName('value').AsString := 'myvalue';
MyQuery1.Execute;