Locate for two fields

Asked

Viewed 1,981 times

1

How to do a survey with Locate with two or more fields simultaneously?

  • What are the drawbacks of doing this?

2 answers

3

The Locate what I’m used to doing is in this format:

DataSetX.Locate('CODIGO;DATAVENCTO;COD_PRODUTO',VarArrayOf([QOperacoesCODIGO.AsInteger
                                                           ,QOperacoesDATAVENCIMENTO.AsDateTime
                                                           ,QOperacoesPRODUTO.AsInteger]), [])

The disadvantage of Locate exists only when you own a DataSet that was not loaded with FetchAll, because, when running it it may be necessary that the Dataset recharges all data from the Table, increasing memory consumption.

A good alternative would be to use a ClientDataSet that has data compression property, that way you would load this client with the data of the dataset and destroy it, leaving only the client with the data ready for search.

In a Client would use the FindKey operation is the same, but much faster.

ClientDataSetX.FindKey([QOperacoesCODIGO.AsInteger
                        ,QOperacoesDATAVENCIMENTO.AsDateTime
                        ,QOperacoesPRODUTO.AsInteger])

Remember that for this you need to set an Index for the Client to work correctly, in the case of the example cited the Index would be by the fields CODE, DATE AND PRODUCT.

  • Note that in Locate you should not separate the String fields by ','. The constant KeyFields is just a string.

  • Okay. Well done @Junior Moreira.

2


LOCATE - Declaration:

function Locate(const KeyFields: string; const KeyValues: Variant; Options: TLocateOptions): Boolean; virtual;

This method allows the exact search of a record, for fields that are not part of the current index of the table represented by the component, and receives as parameters:

  • A string containing the names of the fields by which the search (separated by semicolons).
  • A string containing the values to be searched in the fields by which search will be done (separated by semicolon).
  • A set of options, which may contain the following elements: Locaseinsensitive - if this element is included, upper and lower case letters will be treated equally; LoPartialKey - indicates that the search will be approximated.

This is an example of how to make the method Locate with two fields I found in docwiki.embarcadero.com/Using_locate.

CustTable.IndexFieldNames := 'ID'; 
if CustTable.Locate('ID1';'ID1', VarArrayOf([´String1´,´String2´]), [])then
  ShowMessage ('O cliente com ID = String1 e ID = String2 encontrado')
else 
  ShowMessage ('O cliente não encontrado') ;

In the docwiki.embarcadero.com has all description of the method and also found another method that searches with the fields that is the Extended Locating take a look at the method LocateEx accepts field values or expressions.

  • Field Value:

    if not CustTable.LocateEx('COMPANY', 'AMCO', [lxoCaseInsensitive]) then
      ShowMessage('The customer from AMCO company is not found')
    else
      ShowMessage('Order is not found'); 
    
  • Expression:

    if FDQuery1.LocateEx('Price >= 1000 and Price <= 2000', []) then
      ShowMessage('Order is found')
    else
      ShowMessage('Order is not found');
    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.