dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Search query using search term from user input into TextBox

  1. #1
    Join Date
    Jul 2007
    Posts
    6

    Search query using search term from user input into TextBox

    Hi everyone, thank you for the help you provide on this forum, I've learned a lot so far.

    Right now I'm making a simple Access-Visual Basic 2005 .NET database. I want to make a Search Query that will search the Database for text similar to what the user inputs into TextBox1.Text on the form where the datagrid is displayed.

    I've created a query that works if:

    SELECT...
    FROM...
    WHERE mycolumn = 'oxygen'

    for example. It will retrieve and display all matches for oxygen, so the query itself works. But it does not work with the variations I have tried on:

    SELECT...
    FROM...
    WHERE mycolumn = ' "TextBox1.Text" '

    The query is:

    SELECT FirstName, LastName, City, Major, StudentID FROM Students
    WHERE FirstName LIKE ...

    I want to be able to search the column <FirstName> for the text that the user inputs in TextBox1.Text and then display the parts of the datagrid that match that.

    A blank datagrid appears. I don't think the query is able to see what TextBox1.Text contains.

    What do you recommend I do to make this work? Or what alternatives exist to making a robust search button with search terms?

    Thanks,

    Mendoza

  2. #2
    Join Date
    Jul 2007
    Location
    Minnesota
    Posts
    155
    Code:
    SELECT FirstName, LastName, City, Major, StudentID FROM Students
    WHERE FirstName LIKE ...
    Your search code should look like this.
    Code:
    "SELECT FirstName, LastName, City, Major, StudentID from students where Firstname LIKE ('%" & TextBox1.Text & "%'))"
    That should do.

  3. #3
    Join Date
    Jul 2007
    Posts
    6
    And what if I wanted to code a button to do this, instead of going through the query wizard. Would this work?

    Dim command As New SqlCommand("SELECT * FROM MyTable WHERE MyColumn LIKE @MyColumn", connection) command.Parameters.AddWithValue("@MyColumn", myTextBox.Text & "%")

    command.Parameters.AddWithValue("@MyColumn", myTextBox.Text & "%")

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Did you try it?

  5. #5
    Join Date
    Jul 2007
    Posts
    6
    Quote Originally Posted by Hack
    Did you try it?
    Dim command As New SqlCommand("SELECT * FROM MyTable WHERE MyColumn LIKE @MyColumn", connection)

    command.Parameters.AddWithValue("@MyColumn", TextBox1.Text & "%")


    For "SqlCommand"

    The error I get is:

    Type 'SqlCommand' is not defined.

  6. #6
    Join Date
    Mar 2005
    Location
    Los Angeles, Calif. AKA: Gangsta Yoda™
    Posts
    455
    Either fully qualify it like... System.Data.SqlClient.SqlCommand
    or just inport the class...

    Imports System.Data.SqlClient

    At the top of your class.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer code questions via PMs.
    Microsoft MVP 2006-2009
    Office Development FAQ (VBA, VB6, VB.NET, C#)
    Software Engineer MCP (VB6 & .NET)
    Reps & Rating PostsVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6Outlook Global Address List

  7. #7
    Join Date
    Jul 2007
    Posts
    6
    The code is now:

    Dim connection As New OleDbConnection

    ("Provider=Microsoft.Jet.OLEDB.4.0;Source=C:\MSDS\MSDS DATABASE DATA\MSDS.mdb;")

    Dim [command] As New OleDbCommand("SELECT * FROM Students WHERE FirstName LIKE ?", connection)

    [command].Parameters.AddWithValue("@FirstName", Me.TextBox1.Text & "%")

    connection.Open()

    Dim rdr As OleDbDataReader = command.ExecuteReader()

    Dim tbl As New DataTable()
    tbl.Load(rdr)
    Me.StudentsDataGridView.DataSource = tbl

    rdr.Close()
    connection.Close()


    And there is a crash due to connection.Open() stating: Could not find installable ISAM.

    I tried to fix the problem through this:

    http://support.microsoft.com/kb/209805

    But all registry references were correct and files were found.



    EDIT: Probably has something to do with the fact that I don't have Microsoft Access installed. Tho that hasn't stopped me up to this point.

Similar Threads

  1. Replies: 6
    Last Post: 07-08-2006, 10:48 PM
  2. Index Server Search Query
    By Dion in forum ASP.NET
    Replies: 3
    Last Post: 12-02-2002, 12:12 PM
  3. Database Search Tool
    By Bill Vigna in forum Database
    Replies: 5
    Last Post: 09-11-2002, 04:29 PM
  4. input format in textbox
    By Kurt Klusch in forum VB Classic
    Replies: 2
    Last Post: 11-22-2001, 11:42 PM
  5. Accept Enter Key in an user control textbox
    By Prasant in forum VB Classic
    Replies: 1
    Last Post: 10-10-2001, 10:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center