Can you guys help me with this little thing.

On a a form, i have a textbox1 and add button. If i click the add button, it will check if the textbox has a data and i am already done with that. Now my problem is, if it has a data, how will i search on my database and check if that data on the textbox is already in the database?

6 Years
Discussion Span
Last Post by Reverend Jim

You connect to the database.

You query the database for the record containing that data.

If the number of records returned is zero then it isn't in the database.

For a more detailed answer we need more detailed information.


Hey thank you for the reply. That is what i want to know, how do i query the database/check if textbox1 value is in database.


There are different ways, all of which use the same query. One way is by using ADO. That looks like

    Private Sub btnADO_Click(sender As System.Object, e As System.EventArgs) Handles btnADO.Click

        Dim con As New ADODB.Connection
        Dim rec As New ADODB.Recordset

        con.Open("Driver={SQL Server};Server=.\SQLEXPRESS;Database=mydb;Trusted_Connection=yes;")
        rec.Open("SELECT * FROM Users WHERE UserName = '" & txtUser.Text & "'", _
                con, CursorTypeEnum.adOpenStatic)

        If rec.EOF Then
            MsgBox("user " & txtUser.Text & " not in database")
            MsgBox("user " & txtUser.Text & " has userid " & rec("UserID").Value)
        End If


    End Sub

The steps are

  1. create a connection to the database
  2. open the connection to the database
  3. query the database
  4. do something with the returned records
  5. close the recordset
  6. close the connection to the database

Edited by Reverend Jim

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.