Recent

Author Topic: Searchkey Tdbf  (Read 491 times)

rcmz

  • Jr. Member
  • **
  • Posts: 84
Searchkey Tdbf
« on: September 28, 2023, 06:57:45 pm »
Hi,

Any sample code on how to use Searchkey

I have this code but I get an error saying wrong number of parameters

atUdg.Searchkey(IntToStr(ncod))

I have in the USES dbf, db dbf_common

TIA
Ramiro

Handoko

  • Hero Member
  • *****
  • Posts: 5060
  • My goal: build my own game engine using Lazarus
Re: Searchkey Tdbf
« Reply #1 on: October 01, 2023, 05:56:02 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Dbf: TDbf;
  4. begin
  5.  
  6.   // Create the dbf
  7.   Dbf := TDbf.Create(nil);
  8.   Dbf.FilePathFull := ExtractFileDir(ParamStr(0));
  9.   Dbf.TableName := 'Sample.dbf';
  10.   Dbf.FieldDefs.Add('FRUIT', ftString, 10, True);
  11.   Dbf.FieldDefs.Add('NOTE', ftString, 30);
  12.   Dbf.CreateTable;
  13.   Dbf.Open;
  14.   Dbf.Append;
  15.   Dbf.FieldByName('FRUIT').AsString := 'Banana';
  16.   Dbf.FieldByName('NOTE').AsString := 'Monkeys like it.';
  17.   Dbf.Post;
  18.   Dbf.Append;
  19.   Dbf.FieldByName('FRUIT').AsString := 'Orange';
  20.   Dbf.FieldByName('NOTE').AsString := 'Good source of vit C.';
  21.   Dbf.Post;
  22.   Dbf.Append;
  23.   Dbf.FieldByName('FRUIT').AsString := 'Apple';
  24.   Dbf.FieldByName('NOTE').AsString := 'Snow White ate it.';
  25.   Dbf.Post;
  26.   Dbf.Append;
  27.   Dbf.FieldByName('FRUIT').AsString := 'Durian';
  28.   Dbf.FieldByName('NOTE').AsString := 'An exotic fruit.';
  29.   Dbf.Post;
  30.   Dbf.AddIndex('idxFRUIT', 'FRUIT', [ixPrimary]);
  31.   Dbf.RegenerateIndexes;
  32.   Dbf.Close;
  33.   Dbf.Free;
  34.  
  35.   // Search the dbf
  36.   Dbf := TDbf.Create(nil);
  37.   Dbf.FilePathFull := ExtractFileDir(ParamStr(0));
  38.   Dbf.TableName := 'Sample.dbf';
  39.   Dbf.IndexName := 'idxFRUIT';
  40.   Dbf.Open;
  41.   if Dbf.SearchKey('Orange', stEqual, True) then
  42.     ShowMessage(Dbf.FieldByName('NOTE').AsString);
  43.   Dbf.Close;
  44.   Dbf.Free;
  45.  
  46. end;

SearchKey has 2 parameters:
function SearchKey( Key: Variant; SearchType: TSearchKeyType ): Boolean;

The documentation says:
Quote
This function assumes you selected a particular index, using the IndexName property.

Also:
Quote
  • stEqual searches exactly Key. Returns false if no key matches.
  • stGreaterEqual searches exactly Key or, if not found, the record which key is greater. Returns false if end of file is found.
  • stGreater searches the first record which key is greater than specified Key. Returns false if end of file is found.

Quote
When false is returned as result, the cursor is not moved.

rcmz

  • Jr. Member
  • **
  • Posts: 84
Re: Searchkey Tdbf
« Reply #2 on: October 03, 2023, 06:04:09 am »
Thx!!!

In the documentation ther is no info on what parameters are needed in the search.

TIA
Ramiro

 

TinyPortal © 2005-2018