-1

hi ive got an SQL with the following code,

SELECT SUM (Travel_Cost)
FROM Travel
WHERE Job_ID=:mcode

in order to allow the user to enter a value from an edit component which is passed to the SQL i have got the following code aswell,

procedure TfrmExpenses.Button3Click(Sender: TObject);      
begin          
  ADOQuery1.active:=false;      
  ADOQuery1.Parameters('mcode').AsString:=Edit1.Text;      
  ADOQuery1.active:=true;      
end;

I keep on getting the error message

"missing operator or semicolon"

when i try to compile, any suggestions?

LU RD
  • 34,438
  • 5
  • 88
  • 296
Jeowkes
  • 501
  • 7
  • 20
  • Is there a specific line where you get that? This code looks okay, but maybe you got some more. The compiler should tell you the exact line. – GolezTrol Mar 24 '12 at 22:16
  • 3
    The `Parameters` property is of type `TParameters` and not 'indexed'. Hence, you cannot 'pass' a string to it as an 'index' or 'parameter'. I have never worked with database controls, so I have no idea what this is actually about, but from a syntax point of view, for instance, `ADOQuery1.Parameters.ParamValues['mcode'] := 'Test';` and `ADOQuery1.Parameters.ParamByName('mcode').Value := 'text';` compiles. – Andreas Rejbrand Mar 24 '12 at 22:19
  • Cont with what Andreas posted, try using ParamByName instead of Parameters. Also, make sure that the parameter input type 'mcode' matches the Job_ID field type.. if both are integers, set the parameter input type to integer, and convert the edit1.text to integer during your assignment. – John Easley Mar 24 '12 at 22:21
  • 1
    yeh It fixed when i changed the line `ADOQuery1.Parameters('mcode').AsString:=Edit1.Text;` to `ADOQuery1.Parameters.ParamByName('mcode').Value:=Edit1.Text;` thanks – Jeowkes Mar 24 '12 at 22:22
  • 1
    i'm afraid it did for me, not sure why. – Jeowkes Mar 24 '12 at 22:35
  • @David: I tried the code, and it does in fact give the 'missing operator or semicolon' error at `Parameters` in Delphi 2009. – Andreas Rejbrand Mar 25 '12 at 08:32
  • @AndreasRejbrand Mea culpa. Clearly I got hung up on the semi-colon. – David Heffernan Mar 25 '12 at 10:26

1 Answers1

1

While the TParameters class does have a default property, it accepts an Integer, not a string.

If you know the index of the parameter you can use:

var I: Integer;
....
ADOQuery1.Parameters[I].AsString := Edit1.Text;

Otherwise you need to use:

ADOQuery1.Parameters.ParamByName('mcode').AsString := Edit1.Text;

Note: You'll want to avoid using any of the *ByName functions (ParamByName, FieldByName, etc) in a tight loop if you can because each call performs a linear search on the underlying collection until it finds a match. Not really relevant in this particular case but worth taking into consideration.

Kenneth Cochran
  • 11,954
  • 3
  • 52
  • 117