Pietro,

My guess is that you will have to create an SQL statement that calculates the 
error of the user-supplied value versus the values in your table and finds the 
closest (lowest error) record value. Something like this:

SELECT id, therecordvalue, abs(therecordvalue - usersuppliedvalue) as error
FROM yourtablename
order by error
limit 1

The query will return the ID of the record with the value closest to the one 
the user asked for. It will work even if the user enters a ridiculous 
usersuppliedvalue. It will always return just one record in the dataset - the 
closest one.

Once you have the ID, use the Locate function to jump your grid's dataset to 
the correct record ID.

Now, all that being said, I have never used ADO - I use ZeosLib and MySQL, so 
the above statement has only been tested on MySQL. It does work on MySQL.

If your implementation of SQL does not support the LIMIT statement, then just 
grab the first record from the dataset returned by the query. That will be the 
closest one.

Good luck!

Kevin G. McCoy


--- In advanced_delphi@yahoogroups.com, pietro <pietro.bo...@...> wrote:
>
> Hello,
> 
> First of all, many thanks for your previous (and precious) help !
> 
> Need to solve this problem:
> 
> I have a DBGrid connected to ADOQuery  (Delphi-7).  One field contain 
> number between 0.000 and 30000.000
> User can input a number in this range, than the cursor should move to 
> the exact or nearest value (record).
> It works perfectly with LOCATE, but only when input data are exactly the 
> same on the record value.
> 
> Is there any simple solution to move cursor to the nearest value ?
> 
> 
> In the mean time, I implemented a simple routine with  a dataset scan ( 
> while not eof) and storing in a bookmark the exact record number or  
> last previous / first after record number
> with a simple if a >= b then  set the bookmark, exit the scan routine, 
> then move the dbgrid cursor to the bookmark.
> It's works, but it's not a very beautiful implementation..............

Reply via email to