The code from the other article can be used like this:
cmd = New OleDbCommand("select top 1 * from table1 order by abs(value - @searchvalue), value", conn)
cmd.Parameters.Add(New OleDbParameter("@searchvalue", TextBox1.Text))
rd = cmd.ExecuteReader
What's happening here...
...The
@searchvalue
is a
Parameter to the Command.
...In the SQL you're ordering the data to be returned based on the smallest difference between
value
and
@searchvalue
in ascending order and selecting only the first record - i.e. the smallest difference or closest number to
@searchvalue
.
...If there are two records equidistant from
@searchvalue
then the row with the highest value in
value
will be returned. If you want it to be the lowest then remove the
, value
from the order by.
This
could have been written something like this ...
cmd = New OleDbCommand("select top 1 * from table1 order by abs(value - " & TextBox1.Text & ", value", conn)
rd = cmd.ExecuteReader
But it is always better to use Parameters than string concatenation (there are plenty of explanations of why out in google-land)
Note - this solution only works if
value
is a Number column (any Field Size) as per your question title, and there may be typing errors in the text above.