Click here to Skip to main content
14,298,693 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to find data from a database which is close to the value of an inserted number by the user in text-box. When a user wants to insert number in text-box, if the number not matched whit the value of any data in the database it should not display empty, the database should display a close data for inserted number by a user in text-box.

What I have tried:

<pre> Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
        If cmbtenstion.Text = "UltimateTensileStrength" Then
            Me.Table1BindingSource.Filter = "TensileStrength='" & txtTensile.Text & "'"
        ElseIf cmbDensity.Text = "Density" Then
            Me.Table1BindingSource.Filter = "Density='" & txtDensity.Text & "'"
        ElseIf cmbYield.Text = "YieldStrength" Then
            Me.Table1BindingSource.Filter = "YieldStrength='" & txtYeild.Text & "'"
        ElseIf cmbMoE.Text = "ModulusofElasticity" Then
            Me.Table1BindingSource.Filter = "ModulusofElasticity='" & txtMoE.Text & "'"
        ElseIf cmbCost.Text = "Cost" Then
            Me.Table1BindingSource.Filter = "Cost='" & txtCost.Text & "'"

        End If
    End Sub
Posted
Updated 9-Sep-19 23:53pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

There is nothing standard the provides for a "close match to a number" filter - you will have to sort the items by the absolute value of the difference between the desired number and the actual stored value.

Not simple, not at all.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Not an elegant solution, but you could do something like this:
SELECT * FROM TableXXX WHERE CAST(ColumnXXX as CHAR) LIKE '123%'

Or:
SELECT TOP 1 * FROM Customers ORDER BY ABS( CustomerId - @myvalue ) ;
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100