Board index » delphi » Adding parameters to a TQuery component?

Adding parameters to a TQuery component?

Please help me.
I've been trying this for some time now, and I can't get it to work (yet).

I am trying to add a sql statement to te TQuery component at run-time.
No problem there.

I would however like to be able to use parameters as if I was using the
component design-time.
I tried the help files, but didn;t get much further.
Can anybody tell me how to make this work.......  please?

(if this is the wrong group please let me know.)

procedure SetQParam(qValua : TQurey, lcbTest : TLookupComboBox);
// The parameters are not actually in my program.
// They are a Query and a LookupComboBox on the same form that calls this
procedure.
var
  Test : TStrings;
  Teller, Q1Field : Integer;
  Param: TParam;
  Error : Boolean;
begin
  // Assuming this is done run-time, by a user
  Test := TStringList.Create;
  Test.Add('select name, weight, area');
  Test.Add('from animals');
  Test.Add('where name like :HotCode');
  qValue1.SQL.AddStrings(Test);
  qValue1.FieldList.Update;

  //  try to make the parameter work
    try
    Param := TParam.Create(qValue1.Params, ptInput);
    Param.Name := 'HotCode';
    Param.DataType := ftString;
    Param.AsString := 'a';
    qValue1.Params[0] := Param;

//  If there is no parameter found I get an erroressage here.
//  That should not be surprising, since this is where the query is first
launched.
    qValue1.Open;
    qValue1.Close;
    Error := False;
  // but fail missirably
  except
    // the message that would be generated, if the following is marked as
comment, is 'Could not find object.'
//    MessageDlg('During parameter.', mtError, [mbOk], 0);
//    Error := True;
  end;  // try-except

  //  so it will not get here, if the parameter goes wrong
  if not Error then
  begin
    qValue1.Open;
    lcbTest.ListSource := dsqValue1;
    lcbTest.ListField := qValue1.FieldList.Fields[1].FieldName;
    lcbTest.KeyField := qValue1.FieldList.Fields[0].FieldName;

    Q1Field := qValue1.Fields.Count;
    try
      Test.Clear;
      qValue1.FindFirst;
      for Teller := 0 to Q1Field - 1 do
        Test.Add(qValue1.FieldList.Fields[Teller].FieldName);
    except
      MessageDlg('After parameter.', mtError, [mbOk], 0);
    end;  // try-except
    lcbTest.ListField := qValue1.FieldList.Fields[0].FieldName;
  end;  // if not Error then
end;

Thank you all,

--

Greetings,

the GateKeeper

 

Re:Adding parameters to a TQuery component?


Quote
"the GateKeeper" <gatekee...@chip.tmfweb.nl> wrote in message

news:utUdV1YhAHA.219@net005s.hetnet.nl...

Quote
> Please help me.
> I've been trying this for some time now, and I can't get it to work (yet).

> I am trying to add a sql statement to te TQuery component at run-time.
> No problem there.

> I would however like to be able to use parameters as if I was using the
> component design-time.
> I tried the help files, but didn;t get much further.
> Can anybody tell me how to make this work.......  please?

> (if this is the wrong group please let me know.)

> procedure SetQParam(qValua : TQurey, lcbTest : TLookupComboBox);
> // The parameters are not actually in my program.
> // They are a Query and a LookupComboBox on the same form that calls this
> procedure.
> var
>   Test : TStrings;
>   Teller, Q1Field : Integer;
>   Param: TParam;
>   Error : Boolean;
> begin
>   // Assuming this is done run-time, by a user
>   Test := TStringList.Create;
>   Test.Add('select name, weight, area');
>   Test.Add('from animals');
>   Test.Add('where name like :HotCode');
>   qValue1.SQL.AddStrings(Test);
>   qValue1.FieldList.Update;

>   //  try to make the parameter work
>     try
>     Param := TParam.Create(qValue1.Params, ptInput);
>     Param.Name := 'HotCode';
>     Param.DataType := ftString;
>     Param.AsString := 'a';
>     qValue1.Params[0] := Param;

> //  If there is no parameter found I get an erroressage here.
> //  That should not be surprising, since this is where the query is first
> launched.
>     qValue1.Open;
>     qValue1.Close;
>     Error := False;
>   // but fail missirably
>   except
>     // the message that would be generated, if the following is marked as
> comment, is 'Could not find object.'
> //    MessageDlg('During parameter.', mtError, [mbOk], 0);
> //    Error := True;
>   end;  // try-except

>   //  so it will not get here, if the parameter goes wrong
>   if not Error then
>   begin
>     qValue1.Open;
>     lcbTest.ListSource := dsqValue1;
>     lcbTest.ListField := qValue1.FieldList.Fields[1].FieldName;
>     lcbTest.KeyField := qValue1.FieldList.Fields[0].FieldName;

>     Q1Field := qValue1.Fields.Count;
>     try
>       Test.Clear;
>       qValue1.FindFirst;
>       for Teller := 0 to Q1Field - 1 do
>         Test.Add(qValue1.FieldList.Fields[Teller].FieldName);
>     except
>       MessageDlg('After parameter.', mtError, [mbOk], 0);
>     end;  // try-except
>     lcbTest.ListField := qValue1.FieldList.Fields[0].FieldName;
>   end;  // if not Error then
> end;

> Thank you all,

> --

> Greetings,

> the GateKeeper

  You have few lines more than you nead.

  When you put SQL into query object, the query object makes parameters, you
don't nead to do this by hand. My sample:

Procedure RunParameteredQuery;
Begin
  With Query1, SQL do
  Begin
    Clear;
    Add('SELECT * FROM Table1 WHERE UPPER(Name) LIKE :SomeParameter');
    ParamByName('SomeParameter').AsString := '%START%'
  End;

  Try
    Query1.Open;
    //Do something
  Finally
    Query1.Close;
  End;
End;

This will work on standard query object. On the ADO Query object, things are
little different

Tip:  Like clause - Verify that field that you are searching is the String
field (CHAR; VarCHAR type)

Dusko Vuksanovic
w...@gmx.net

Re:Adding parameters to a TQuery component?


Quote
"Dusko Vuksanovic" <w...@gmx.net> wrote in message

news:94m6i6$7gh$1@sunce.iskon.hr...

Quote

> "the GateKeeper" <gatekee...@chip.tmfweb.nl> wrote in message
> news:utUdV1YhAHA.219@net005s.hetnet.nl...
> > Please help me.
> > I've been trying this for some time now, and I can't get it to work
(yet).

<><cut><>
<></cut><>

Quote

>   You have few lines more than you nead.

>   When you put SQL into query object, the query object makes parameters,
you
> don't nead to do this by hand. My sample:

<><cut><>
<></cut><>

Quote

> Dusko Vuksanovic
> w...@gmx.net

Thank you (and Robert Kaplan too) for helping me.
I thought I tried this, and it didn't seem to work (at first).

Now I tried again and it works.
Thank you for your help.

--

Greetings,

the GateKeeper

Other Threads