Jump to content
Sign in to follow this  
bazzer747

Macro substitution in Locate

Recommended Posts

Hi,

I'm aware that you can use a macro substitution in a select statement when you don't know some value until runtime (like a fieldname). If there a similar function that can be used in a Locate?

 

I am developing a 'Search' on an accounts program where the user first selects a fieldname in which he looks for a value, maybe a 'Payee' fieldname or an 'Account' fieldname or a 'Deposit' fieldname. So the field on which to conduct the locate isn't known until runtime.

A usual locate might look like this:

  if fdqA.Locate('Payee', cPayee ), []) then
    ShowMessage('Located!')
  else
    ShowMessage('Failed!');

But the user might want to search on 'Account' or 'Deposit'.  

Share this post


Link to post

I created a function for this, which has a field name in the argument.

Share this post


Link to post
1 hour ago, bazzer747 said:

I'm aware that you can use a macro substitution in a select statement when you don't know some value until runtime (like a fieldname). If there a similar function that can be used in a Locate?

 

What is the actual question? That is what locate function does. You pass fieldname and search value at runtime and it searches.

Share this post


Link to post

For me, the question is quite unclear. I tried to give at least some answer.

  if fdqA.Locate (AFieldName, cPayee), []) then
    ShowMessage ('located!')
  else
    ShowMessage ('Failed!');

 

Share this post


Link to post
Guest
11 hours ago, bazzer747 said:

But the user might want to search on 'Account' or 'Deposit'.

FireDAC can works better than "LOCATE" method! Just read your HELP for "FireDAC" and "MACRO" replacement!!!

 

here your sample using "LOCATE" - basic method for any DATASET descendent!

  • my sample used in "Footman" project - BDE and Paradox file-tables!
function TForm1.fncFindItRecord: boolean;
var
  lFindOptions : TLocateOptions;
  lFieldNames  : string;
  lValuesToFind: Variant; // because "VarArrayOf()" use!!!
begin
  result := false;
  // lFindOptions := [TLocateOption.loCaseInsensitive];
  lFindOptions := [TLocateOption.loPartialKey, TLocateOption.loCaseInsensitive]; // more flexible search...
  //
  begin
    case rdgrpNationalCompetitions.ItemIndex of
      0:
        begin
          lFieldNames   := 'FieldName1';
          lValuesToFind := 'Value1 desired'; // qryCompetitionsFindItCompetitionName.AsString;
        end;
      1:
        begin
          lFieldNames   := 'FieldName1;FieldName2';
           // see VarArrayOf() function on HELP SYSTEM
          lValuesToFind := VarArrayOf(['Value1', 'Value2-string']); // values according with "Fields" definitions on table: string, integer, boolean, datatime, etc...
          // lValuesToFind := VarArrayOf(['Value1', true]); 
          // lValuesToFind := VarArrayOf(['Value1', 1000]); 
          // lValuesToFind := VarArrayOf(['Value1', 1.25]); 
        end;
    end;
    //
    if not FDQuery1.Locate(lFieldNames, lValuesToFind, lFindOptions) then
    begin
      ShowMessage('None record was founded with this information! Try it again...');
    end else begin
      // waht you want do?
      result := true;
    end;
  end;
end;

 

hug

Edited by Guest

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×