简体   繁体   中英

How to search a specific field in a database

I want the user to be able to choose from a dropdown Combobox listing some of the fields of the database, then below enter a search term and all then all the results that meet the query be displayed in the dbgrid. I'm not sure how to link the current value of the ComboBox into the sql statement. I tried using this

begin
    with ADOQuery do begin
      Close;
      SQL.Clear;
      SQL.Add ('SELECT * FROM List WHERE combobox1.text =' +   QuotedStr (Asearchterm.Text));
      Open;

And it doesn't work. The error i'm getting is "The parameter combobox1.text has no default value". Any ideas?

If you're wanting to use the combobox1 text value as part of the sql statement, you'd set up the sql string along the lines of

'SELECT * FROM List WHERE [' + combobox1.text + '] = ''' + QuotedStr(Asearchterm.Text) + ''''

is probably what you're looking for. I added the extra quotes around the QuotedStr because I'm guessing that the filter is not always going to be numeric values. This will work for numeric as well as non-numeric values.

instead of 'combobox1.text' you have to put actual column name that you are looking for. And you can use LIKE keyword and some wildcards. something like:

SELECT * FROM 'table WHERE 'column' LIKE '%YOUR_SEARCH_TEXT%';

That was for a search...if you want to find exact string then you have to use = operator instead of LIKE

More info here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM