How to make a search box in an application

Hi.

I have a database that has a table named "Main", this table has 4 elements "ID, Blue Number, Year, Description" .

What i need is how can i make a form that has only a search box to search using any given blue number, and once the blue number is entered into the text box the program will search the database for all other elemnts to that blue number.

Anyone can help plz



Answer this question

How to make a search box in an application

  • tviel

    I forgot to mention the error I am getting.

    This is where the debugger highlights:

    MyReader = MyCommand.ExecuteReader

    This is the error stated

    OleDbException was unhandled

    No value given for one or more required parameters.

    Thanks

  • Bjorn EP Backlund

    This is how i tried to do it :

    1: Made a form with a button (Button1), a textbox (TexBox1) & a listbox (ListBox1).
    2: Went to Data Sources tab and selected my database and then selected the table i want to use (Table1) and the dataset name is memsDataSet.
    3: Inserted the following code into Button1 :

            Do
                Dim MyCommand As OleDb.OleDbCommand
                Dim MyParam As OleDb.OleDbParameter
                Dim MyConnection As OleDb.OleDbConnection
                Dim MyReader As OleDb.OleDbDataReader

                MyConnection.Open()

                MyParam = Me.TextBox1.Text

                MyCommand.Parameters.Add(MyParam)

                MyCommand.CommandText = "Select [Blue] From Table1 Where [Blue] = "

                MyReader = MyCommand.ExecuteReader

                While MyReader.Read
                    Me.ListBox1.Items.Add(MyReader(0))
                End While

                MyConnection.Close()
            Loop

     

    4: The error i have is " Value of type 'String' cannot be converted to 'System.Data.OleDb.OleDbParameter' "


    So, what's wrong


  • Donaghy

    Thanks DMan1 for replying.

    But, if you can can you explain more

    My form has : textbox1, button1 & listbox1

    My database has one a table called Table1 and 3 items ID, Blue & Name

    Dataset name = memsDataSet.

    How can i implement the code you gave me

    Sorry i'm a novice


  • GDigrego

    I've tried to use the code you gave me, but i got errors. Can u help plz
  • Anthony M

    MyCommand.Connection = MyConnection



  • Raihan Iqbal

    Well, if you posted the code you are using and the error you are getting we just might be able to help

  • fbiots

    Here I am...

    A couple of problems...First get rid of the 'Do loop' and second...

    Set your connection string:

    Dim MyConnection As New OleDb.OleDbConnection("

    Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;")
    instantiate your command and parameter objects with the 'New' keyword:

    Dim MyCommand As New OleDb.OleDbCommand
    Dim MyParam As New OleDb.OleDbParameter

    & Then

    the parameter value is = to the text :

    MyParam.Value = Me.TextBox1.Text



  • Julian V

    DMan1 , Where are you
  • A1Programmer

    http://msdn2.microsoft.com/en-us/vbasic/ms789075.aspx#data

    Data Access Samples

    The Data Access samples show how to use the classes in the System.Data and related namespaces. In addition, how to use the new features of SQL Server 2005. Samples include:

    • Asynchronous Queries
    • Attaching a database with your application
    • Creating and using User Defined Types with SQL Server 2005
    • DataReader vs. DataSet comparision
    • DataSet and DataTable Enhancements
    • Performing Batch Updates and Data Paging
    • Performing Bulk Updates
    • Reading and Writing Images from a Database
    • Using Factory Classes
    • Using Managed Stored Procedures and User Defined Functions with SQL Server 2005
    • Using Multiple Active Result Sets with SQL Server 2005
    • Using Notifications with SQL Server 2005
    • Using the XML data type with SQL Server 2005
    • XPath and XSLT Transformations Enhancements

    Download the Visual Basic version of the Data Access samples.

    Learning ADO.NET:

    http://msdn2.microsoft.com/en-us/data/aa937699.aspx

  • DDressel

    anyone can help please
  • Zac Boyles

    Hi, I have been trying to make a similar search happen, but I can't seem to get it to work. I am trying to make an addressbook for my wife. She wants to be able to do a search on a number of fields such as birthday month to get all the people she needs to send cards to etc. Or to find a phone number. I have written the code to clear all the textboxes and do a search on each to determine which field was enterwed to search for. This was then assigned to a variable. The column name is also assigned to a variable based on the previous one. Using these variables I can't seem to get the code written above to work. Any Ideas Here is my code for the search button. PS Brand new to all programming and trying to self teach > not going to fast haha

    Dim Search As String

    Dim Column As String

    Dim MyCommand As New OleDb.OleDbCommand

    Dim MyParam As New OleDb.OleDbParameter

    Dim MyConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\gistemp\VBDotNet\Addressbook.mdb")

    Dim MyReader As OleDb.OleDbDataReader

    If FirstNameTextBox.Text <> "" Then

    Search = FirstNameTextBox.Text

    Column = "Firstname"

    ElseIf SurnameTextBox.Text <> "" Then

    Search = SurnameTextBox.Text

    Column = "Surname"

    ElseIf AddressTextBox.Text <> "" Then

    Search = AddressTextBox.Text

    Column = "Address"

    ElseIf CityTextBox.Text <> "" Then

    Search = CityTextBox.Text

    Column = "City"

    ElseIf PostalTextBox.Text <> "" Then

    Search = PostalTextBox.Text

    Column = "Postal"

    ElseIf PhoneTextBox.Text <> "" Then

    Search = PhoneTextBox.Text

    Column = "Phone"

    ElseIf MonthComboBox.Text <> "" Then

    Search = MonthComboBox.Text

    Column = "Month"

    End If

    ListBox1.Visible = True

    MyConnection.Open()

    MyParam.Value = Search

    MyCommand.Parameters.Add(MyParam)

    MyCommand.CommandText = "Select [Column] from tblContacts Where [Column] = [Search]"

    MyCommand.Connection = MyConnection

    MyReader = MyCommand.ExecuteReader

    While MyReader.Read

    Me.ListBox1.Items.Add(MyReader(0))

    End While

    MyConnection.Close()


  • yeos_lee

    Thanks DMan1, now it works !

    Just wanted to say sorry for the annoying posts asking very stupid questions. But you know i'm new to VB, and i hope i learn it some day.

    C YA


  • tayoga

    Use the ado.net connection, command, parameter, and sql string to retrieve the data

    The exact syntax will vary depending upon the db and controls used....

    pseudo:

    Dim MyCommand as OledbCommand

    Dim MyParam as OledbParameter

    MyParameter = Me.Textbox1.Text

    MyCOmmand.Parameters.Add(MyParam)

    MyCommand.CommandText = "Select [Blue Number] From Main Where [Blue Number] = "

    MyConnection.Open

    MyReader = MyCOmmand.ExecuteReader

    While MyReader.Read

    Me.ListBox1.Items.Add(MyReader(0))

    Loop

    MyConnection.CLose



  • ExtinctPencil

    Hi DMan1, thanks for replying.
    Well, i still got some error. After modifing the code as below :


    Dim MyCommand As New OleDb.OleDbCommand
    Dim MyParam As New OleDb.OleDbParameter
    Dim MyConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mems.mdb")
    Dim MyReader As OleDb.OleDbDataReader

    MyConnection.Open()

    MyParam.Value = Me.TextBox1.Text

    MyCommand.Parameters.Add(MyParam)

    MyCommand.CommandText = "Select [Blue] From Table1 Where [Blue] = "

    MyReader = MyCommand.ExecuteReader

    While MyReader.Read
    Me.ListBox1.Items.Add(MyReader(0))
    End While

    MyConnection.Close()


    I excuted the program and got a message box with a yellow arrow hilighting the line " MyReader = MyCommand.ExecuteReader ".
    The error inside the box is " InvalidOperationException was unhandeled. ExcuteReader : Connection property has not been initialized ".

    So, what do you suggest


  • How to make a search box in an application