Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2009

    Unanswered: Using Text Box Value as the 'Where' clause in SQL query

    I'm using VB 2008 express for a project and I am a total beginner.

    I have a small app which searches an address database and brings back a data grid of results.

    The users tpyes in all, or part of a street name into a text box, hits a search button and the records are then filtered in a data grid.

    I am writing simple sql queries which are working fine but I am strugging to indicate the where clause must be the text box text. For example, what I need to do is the following:

    SELECT * FROM tbl_addresses WHERE Street_Name Like "TextBox1.Text" - but obiously that doesn't work. I also need to ensure this works as a wildcard query.

    Can anybody help?? Please??

  2. #2
    Join Date
    Jun 2004
    Arizona, USA

    "SELECT * FROM tbl_addresses WHERE Street_Name Like '" & TextBox1.Text & "%'"

    The idea is that the string you build should contain the CONTENTS of the text box - not the textbox name...

    I'm assuming ansi standard wildcard convention as well - the %. Every database that I've ever used or heard of uses ansi standard convention for wildcards, except for Access - it uses the * character. Also, note the apostrophes which will end up around the contents of the text box. I'm assuming that the field you're searching is either a text type field, or a date/time type field. If the field is numeric, you can't use the apostrophes, and you can't use like/wildcards. (you could cast the numeric data to a string, and use the like function with that though.)
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts