0

Im running a couple of queries (well attempting to) using Lazarus objects rather than programatically, I have my DB Connections and Transactions setup and interacting with my Queries and am reading and initial query into a TDBGrid. Now what I want to do is be able to click on a grid cell and select an ID value to use in a query.

I have a TSQLQuery object setup and linked to a source, I entered this into the SQL property:

SELECT * FROM tbl_accounts
WHERE tbl_accounts.ClientID = :AccID

However I can't work out how to pass my parameter into the object... My procedure looks like this:

procedure TtcheckHome.accResultsCellClick(Column: TColumn);
  begin
  selected := listAccounts.Fields[0].AsString;
  // accSelect.SQL.Text := 'SELECT * FROM tbl_accounts WHERE tbl_accounts.ClientID = 2';
  accSelect.Params.ParamByName('AccID').AsString := selected;
  textEdit.Text := accSelect.FieldByName('AccountNumber').AsString;
end;

This always returns with accSelect : Field name AccountNumber not found, if I remove any references to Params however and hardcode an ID into the query it works perfectly. This leads me to believe that my method for using bound parameters is wrong! What have I missed?

4

1 回答 1

0

使用参数值刷新查询:

accSelect.Close;
accSelect.Params.ParamByName('AccID').AsString := selected;
accSelect.Open;

如果您不想不断关闭并重新打开相同的查询,我该怎么办:

if accSelect.Params.ParamByName('AccID').AsString <> selected then
begin
  accSelect.Close;
  accSelect.Params.ParamByName('AccID').AsString := selected;
end;
if not accSelect.Active then
  accSelect.Open;
于 2013-07-24T12:46:30.880 回答