Recent

Author Topic: [SOLVED] Lazarus | locate on numeric field  (Read 877 times)

micoudic

  • New Member
  • *
  • Posts: 30
[SOLVED] Lazarus | locate on numeric field
« on: September 23, 2023, 05:47:29 pm »
Hi,
I am doing incremental searches from a DBGRID. This works perfectly, but for string fields only.
I'm trying to do the same on numeric fields. I defined them in currency, because it is the only format that allows me to enter the decimal place. This almost works, except that from the 3rd digit entered, nothing changes. It doesn't crash, but it's like I can't grasp anything anymore.

I enter the search criteria from an Edit on the Form (named "Edt_Recherche_Num")

Code: Pascal  [Select][+][-]
  1. procedure TForm_01.Edt_Recherche_NumChange(Sender: TObject);
  2. var
  3.    WKleSearchN: string;
  4. begin
  5.     if (Edt_Recherche_Num.Text = '') or (Edt_Recherche_Num.Text = '0') then Abort;
  6.    WKleSearchN := Edt_Recherche_Num.Text;
  7.    Datamodule1.DB_Annonces.Locate(WFieldName,WKleSearchN,[loPartialKey])
  8. end;

I also tried with this instruction
Code: Pascal  [Select][+][-]
  1. WKleSearchN := StrToCurr(Edt_Recherche_Num.Text);

But without change.
By changing the field type to Integer, I have not more success.
Of course, my fields are indexed

I can't find any info for a locate with a digital key

Thanks

Mint 20
Lazarus 2.0.3
dBase7 files
« Last Edit: September 29, 2023, 09:08:11 am by micoudic »

jamie

  • Hero Member
  • *****
  • Posts: 5852
Re: Lazarus | locate on numeric field
« Reply #1 on: September 23, 2023, 06:02:07 pm »
I am not an expert in Db-coding but I see a couple of issues.

First off, you need to use "EXIT" instead of Abort, because about works with a TRY statement at some point before this code.

It's to raise an exception early.

 Also, you should be converting the string data from edit to a currency for test.

 if StrToCurr(TheEditString) = 0.0 then.....


etc

The only true wisdom is knowing you know nothing

paweld

  • Hero Member
  • *****
  • Posts: 851
Re: Lazarus | locate on numeric field
« Reply #2 on: September 23, 2023, 07:20:58 pm »
Locate is a function and returns True if successful.   
Sample in attachment
Best regards / Pozdrawiam
paweld

micoudic

  • New Member
  • *
  • Posts: 30
Re: Lazarus | locate on numeric field
« Reply #3 on: September 25, 2023, 10:08:09 am »
Small details:
I use 2 distinct procedures for my incremental searches
1- a procedure for searching on String fields (there are specific additional treatments in this procedure which do not need to be in searches on numeric fields)
2- in procedure for searching numeric fields.
You will notice that I defined a variable "WKleSearchN" inside this procedure. This variable is normally defined at the very beginning of the unit. But as I had to make numerous (unsuccessful) attempts, I found more practical to redefine this variable in the search procedure, which avoids me, each time, going back to the very beginning of the unit to modify the type of this variable.
This is why, in the code cited above, this variable is of type String. This is the last test that I carried out, but obviously, I tested all kinds of combinations, without perfect results.

Concerning the research, in the current state, I noticed this:
if the column I want to search for contains
5.00
52.15
68.87
525.18
525.23
525.32
638.55
....

Entries        Result
5               OK out of 5.00
52             OK out of 52.18
525           Remains on 52.18
525.          Rest on 52.18
525.3        Rest on 52.18
525.32      OK

525.32 : The cursor only moves if, and only if, the exact amount is found.
This is not what I expect from my research, but to be able to get as close as possible to the amount I am looking for, not knowing in advance the exact amount, to the nearest cent.


To Jamie:
Abort suits me perfectly. I don't need to do a search if the input edit is blank or zeros. This is the case when I go back, or when I reset the research edit. But maybe I'm missing something?

a paweld
I downloaded your program, but cannot test it.
when trying to compile, I get the following messages

Code: Pascal  [Select][+][-]
  1. Compilation du projet - Cible : project1 : Code de sortie 1 - Erreurs : 1
  2. Unit1.pas(8,22) Fatal: Impossible de trouver Forms utilisé par Unit1. Vérifier que le paquet LCLBase est dans les dépendances.


TryStrToFloat: I didn't know this function. I will test with this function.
I tested with the StrToFloat function, without entirely satisfactory results.

Zvoni

  • Hero Member
  • *****
  • Posts: 2036
Re: Lazarus | locate on numeric field
« Reply #4 on: September 25, 2023, 10:31:15 am »
The second argument of Locate is a Variant
https://www.freepascal.org/docs-html/fcl/db/tdataset.locate.html

Since you're passing your variable WKleSearchN, which IS of Type String, why do you expect it to work on Floating points?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

paweld

  • Hero Member
  • *****
  • Posts: 851
Re: Lazarus | locate on numeric field
« Reply #5 on: September 25, 2023, 01:02:06 pm »
Quote from: micoudic
when trying to compile, I get the following messages
What version of Lazarus do you have?   
Try adding the LCLBase package to your project requirements - it should solve the problem. (Menu: Project > Project Inspector > Add > New Requirement)   
   
Quote from: micoudic
but to be able to get as close as possible to the amount I am looking for, not knowing in advance the exact amount, to the nearest cent.
For numeric fields Locate searches by exact value. The loPartialKey option only works for strings.
To make it work the way you want it to, you'll need to convert the column to a string (an easier option, but only if the column in DBGrid is ReadOnly) or write your own version of the Locate function.
Best regards / Pozdrawiam
paweld

micoudic

  • New Member
  • *
  • Posts: 30
Re: Lazarus | locate on numeric field
« Reply #6 on: September 27, 2023, 09:21:46 am »
To Zvoni
As indicated previously, I use 2 distinct procedures for searches on string fields and numeric fields.
For numeric searches, I tried all possible numeric types, without satisfactory results, and I ended up trying (at random and without conviction) the numeric field type. That's when I decided to ask for help on the forum. But rest assured, I am well aware that for a search on a digital field, you need a digital key.

to paweld

Many thanks for this valuable information. I have searched everywhere for this type of information. Oddly enough, there is a plethora of information and examples on the net, but it's always on string keys. I even dissected the Delphi7 documentation that I have, but Delphi has specialized functions (notably gotonearest), and as a result, almost nothing on the locate function.
Thanks again, I think I found the solution. To be finalized and tested thoroughly.

Zvoni

  • Hero Member
  • *****
  • Posts: 2036
Re: Lazarus | locate on numeric field
« Reply #7 on: September 27, 2023, 09:34:56 am »
To Zvoni
As indicated previously, I use 2 distinct procedures for searches on string fields and numeric fields.
For numeric searches, I tried all possible numeric types, without satisfactory results, and I ended up trying (at random and without conviction) the numeric field type. That's when I decided to ask for help on the forum. But rest assured, I am well aware that for a search on a digital field, you need a digital key.

to paweld

Many thanks for this valuable information. I have searched everywhere for this type of information. Oddly enough, there is a plethora of information and examples on the net, but it's always on string keys. I even dissected the Delphi7 documentation that I have, but Delphi has specialized functions (notably gotonearest), and as a result, almost nothing on the locate function.
Thanks again, I think I found the solution. To be finalized and tested thoroughly.
What about Filter? https://www.freepascal.org/docs-html/fcl/db/tdataset.filter.html
something along the Lines
Code: Pascal  [Select][+][-]
  1. MyDataSet.Filter:='NumericFieldInDBGrid/DataSet>='+MyNumericValue;
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

micoudic

  • New Member
  • *
  • Posts: 30
Re: Lazarus | locate on numeric field
« Reply #8 on: September 27, 2023, 04:51:04 pm »
What about Filter? https://www.freepascal.org/docs-html/fcl/db/tdataset.filter.html
something along the Lines
Code: Pascal  [Select][+][-]
  1. MyDataSet.Filter:='NumericFieldInDBGrid/DataSet>='+MyNumericValue;

Thank you, I will keep this information carefully.
In my case, my file is already filtered, sometimes with a second filter which combines with the first. Three filters combined, on a dbf file, why not, but add a filter, search, undo the filter, I think it will become a little "heavy".
In addition, I do not know in advance the exact amount of my research. This is precisely the purpose of this research: to find if a given amount exists in the file.

 

TinyPortal © 2005-2018