سؤال

I'm using Delphi7, MS Vista and Devart's dbExpress drivers (version 4.70). I drop a TSQLConnection, a TSQLTable (tabA), a TDataSetProvider, a TClientDataSet(cdsA), a DataSource and a DBGrid.

I made all the settings via graphical design tool. Everything works fine, as I open cdsA, I can see all the data in the Grid. Here is my code:

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 and fieldB exists in table and are defined in cdsA.Fields too. When I execute this code the Locate instruction generate exception EVariantInvalidArgError ... Invalid argument. I'm wondering what's wrong. TIA.

Francesco

هل كانت مفيدة؟

المحلول

Your code is wrong. :)

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;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top