1

Delphi7MS VistaおよびDevart's dbExpressドライバー(バージョン4.70)を使用しています。TSQLConnectiona 、a TSQLTabletabA)、a TDataSetProvider、a TClientDataSetcdsA) 、a DataSource、 aをドロップしDBGridます。

グラフィックデザインツールですべての設定を行いました。開くと、すべてが正常にcdsA機能し、グリッド内のすべてのデータを確認できます。これが私のコードです:

procedure TForm1.Button1Click(Sender: TObject);
var
  fields, values: string;
begin
  cdsA.Close;
  cdsA.Open;
  fields := 'fielda;fieldb';
  values := Edit1.Text+';'+Edit2.Text;
  cdsA.SetKey;
  cdsA.Locate(fields, values, [loCaseInsensitive]);
end;

fieldAテーブルにfieldB存在し、で定義されていcdsA.Fieldsます。このコードを実行すると、Locate命令は例外を生成しますEVariantInvalidArgError ... Invalid argument。何が悪いのかしら。TIA。

フランチェスコ

4

1 に答える 1

5

あなたのコードは間違っています。:)

procedure TForm1.Button1Click(Sender: TObject);
var
  fields, values: string;
begin
  // Closing the CDS and opening it every time is foolish. Just
  // open it if it's not already open.
  if not cdsA.Active then
    cdsA.Open;

  // List of column names. Since column (field) names are always
  // strings, can just use semicolon-separated values
  fields := 'fielda;fieldb'; 

  // Values for columns. Since these could be any type, you can't
  // simply use semicolon-separated strings. You have to pass an
  // array of Variants. The easiest way is to just create it and
  // populate it, and let reference counting release it when it's
  // out of scope
  values := VarArrayOf([Edit1.Text, Edit2.Text]);  

  // No call to SetKey here. SetKey is used with FindKey, not Locate
  cdsA.Locate(fields, values, [loCaseInsensitive]);
end;
于 2011-11-27T17:06:25.563 に答える