Dmitry  Sokolov
Dmitry Sokolov

Reputation: 1383

Delphi 2007 Adoquery Parameters not working

   ADOQuerySelect.Close;
    ADOQuerySelect.SQL.Add(' AND (дата_заказа between #'+dat+'# and #'+da+'#)');
    if ComboBox6.Text <> '' then
    begin
    ADOQuerySelect.Parameters.ParamByName('Name').Value := ComboBox6.Text ;
      ADOQuerySelect.SQL.Add(' AND (Наименование = :Name)');
    end;
ADOQuerySelect.Open;

I use Delphi 2007, MS Access. And i dont now how work with parameters. On this code, i have error: parameter Name not found. I tried many other variants of code, but they all not working. I add parameter Name via GUI with datatype ftstring;

Upvotes: 1

Views: 652

Answers (2)

In the object inspector ADOQuerySelect should have 'Name' among list of parameters.

You can also use the following code to create the parameter 'Name':

with ADOQuerySelect.Parameters.AddParameter do
begin
  Name := 'Name';
  DataType := ftString;
end;

Upvotes: 1

Ken White
Ken White

Reputation: 125620

Changing SQL in your code clears the existing parameters, so there's no parameter called 'Name' at the time you try to set its value. You then create the Name parameter, but it's too late.

Change the order of your statements:

ADOQuerySelect.Close;
ADOQuerySelect.SQL.Add(' AND (дата_заказа between #'+dat+'# and #'+da+'#)');
if ComboBox6.Text <> '' then
begin
  ADOQuerySelect.SQL.Add(' AND (Наименование = :Name)');
  ADOQuerySelect.Parameters.ParamByName('Name').Value := ComboBox6.Text ;
end;
ADOQuerySelect.Open;

You should be using parameters for all your replacements, BTW. Let the database driver handle conversions and date formats and quoting values and all of that for you.

Upvotes: 3

Related Questions