Delphi Using Locate Function in Data Source in Memory Table

I need to check given value is already exists in the memory table, so i have tried following method.

function TForm4.findValueExists(id: Integer): Boolean;
var
  state: Boolean;
begin
  state := DBGrid1.DataSource.DataSet.Locate('code', id, []);

end;

here is the Search button procedure

procedure TForm4.butSearchClick(Sender: TObject);
var
  id: Integer;
  Name: String;
  Sell: Double;

  Qty: Integer;
  Amount: Double;
  OldQty: Integer;
  RecordExist: Boolean;
begin

  if txtprocode.Text <> '' then

  begin

    FDQuery1.Params.ParamByName('ID').Value := txtprocode.Text;
    if FDQuery1.Active then
      FDQuery1.Close;
    FDQuery1.Open();

    try
      FDQuery1.First;
      if not FDQuery1.Eof then
      begin
        id := FDQuery1.FieldByName('Id').AsInteger;
        Name := FDQuery1.FieldByName('name').AsString;
        Sell := FDQuery1.FieldByName('selling').AsFloat;
        Qty := 1;
        Amount := Sell * Qty;

        if Form4.findValueExists(id) then
        begin
          FDMemTable1.Edit;
          OldQty := DBGrid1.Fields[3].Value;

          FDMemTable1.FieldByName('Qty').AsInteger := (OldQty + 1);
          FDMemTable1.FieldByName('amount').AsFloat := (Sell * (OldQty + 1));
          FDMemTable1.Post;

        end
        else
        begin
          FDMemTable1.InsertRecord([id, Name, Sell, Qty, Amount]);
        end;

      end;
    finally

    end;

  end;
end;

unfortunately this method always gives me result as 'false'. but physically i can found matched result for given id. here is UI

Guys i`m new to Delphi language.

8

1 Answer

You're not returning a value from your FindValueExists function, so you have absolutely no way of knowing if it locates the record or not. If you'd turn on compiler hints and warnings, the compiler would have pointed that fact out to you. (It would have informed you function findValueExists might not return a value, and also that Value assigned to 'state' is never used.)

Change your findValueExists so that it actually returns the result of the Locate call.

function TForm4.findValueExists(id: Integer): Boolean;
begin
  Result := DBGrid1.DataSource.DataSet.Locate('code', id, []);
end;
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest