Results 1 to 3 of 3

Thread: vb.net sql query with variable

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2015
    Posts
    171

    vb.net sql query with variable

    hi, i've an access database where i store my data, an i want search all field that contain a keyword so do in my vb.net code query like this
    Access.ExecQuery("SELECT distinct field2 FROM Data WHERE (field1='" + field1Selected + "') AND (field2 contains '" + txtFindKeyWord.Text + "') ORDER BY field1")
    lstfield1.Items.Clear()
    For Each R As DataRow In Access.DBDT.Rows
    lstfield1.Items.Add(R("field1"))
    FindResult = FindResult + 1


    but not work; if i replace field2 contains '" + txtFindKeyWord.Text + "' with a costant string value ito work, but i want to chose which keyword i need to find. Anyone can help?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: vb.net sql query with variable

    Quote Originally Posted by hannibal smith View Post
    hi, i've an access database where i store my data, an i want search all field that contain a keyword so do in my vb.net code query like this
    Access.ExecQuery("SELECT distinct field2 FROM Data WHERE (field1='" + field1Selected + "') AND (field2 contains '" + txtFindKeyWord.Text + "') ORDER BY field1")
    lstfield1.Items.Clear()
    For Each R As DataRow In Access.DBDT.Rows
    lstfield1.Items.Add(R("field1"))
    FindResult = FindResult + 1


    but not work; if i replace field2 contains '" + txtFindKeyWord.Text + "' with a costant string value ito work, but i want to chose which keyword i need to find. Anyone can help?
    well first off, the SQL isn't close to being right.

    Field1 will only return if there's an exact match, and field2 ... well... what you want is a LIKE clause, and you'll need to include the wildcard characters too. And lastly you'll only get records where BOTH conditions are met. Is that what you want or do you want where either OR the other is true?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2015
    Posts
    171

    Re: vb.net sql query with variable

    Quote Originally Posted by techgnome View Post
    well first off, the SQL isn't close to being right.

    Field1 will only return if there's an exact match, and field2 ... well... what you want is a LIKE clause, and you'll need to include the wildcard characters too. And lastly you'll only get records where BOTH conditions are met. Is that what you want or do you want where either OR the other is true?

    -tg
    in my database field1 is a list of brand, and field2 are models of products; i would find all products name of a selectedbrand that contain a keyword(input with textbox) in the name.

Posting Permissions

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



Click Here to Expand Forum to Full Width