Results 1 to 9 of 9

Thread: [RESOLVED] VB .NET, Access, SQL, and LIKE operator

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    35

    Resolved [RESOLVED] VB .NET, Access, SQL, and LIKE operator

    Okay, as the title shows, I'm using VB .NET, Access, SQL, and the
    LIKE operator.

    I am experimenting with the binding source, dataset, and table adapter controls to get the program to behave in the same way that a data access
    class would if I had written code in a DA class.

    Okay, here is my problem. I have a sql command that isn't working.

    Code:
    SELECT        Title, Director, YearMade, Description
    FROM            MyMovies
    WHERE        (Title LIKE '%' + TitleTextBox.[text] + '%')
    The TitleTextBox.text is a text box on my form and I want to use the
    value of it at runtime to narrow the information in the database down
    to only matching requests.

    I created this query using the query builder while the program was not
    in runtime mode.

    This program is just a simple little program to store film information.
    So if someone types in "Dumb" into the textbox then
    "Dumb and Dumber" and "Dumb and Dumberer" should get returned
    to the dataset. [I'm assuming that it is where it gets returned to,
    when a query is performed on the database.]

    Anyway, if anyone has a solution, please post.

    Thanks!

  2. #2
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: VB .NET, Access, SQL, and LIKE operator

    You can do that by opening the tab data sources, drag the title field to the form, click in the little black arrow in the text box and then select add query, the you can put there you're query...

    This is one way... there are another ones...

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    35

    Re: VB .NET, Access, SQL, and LIKE operator

    That is how I added the SQL query to the project. But the SQL query isn't working. I believe it has something to do with how I added in the LIKE operator into the SQL query. I believe my sql query is incorrect. I think it is having an issue with adding "TitleTextBox.text" to the query.

    Maybe someone can help me resolve this issue.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VB .NET, Access, SQL, and LIKE operator

    SQL is SQL.... it knows nothing about your application. It knows nothing about your form. It knows nothing about your text box. You have to supply the data to it.
    1) Replace "(Title LIKE '%' + TitleTextBox.[text] + '%')" with "(Title LIKE '%' + @SearchFor + '%')"
    2) Use the AddParameter method to add the value to the parameter
    3) Run the command.

    -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??? *

  5. #5
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: VB .NET, Access, SQL, and LIKE operator

    After getting the code to work with tg's advice, just remember that if you have multiple parameters that the Access OleDb provider does not accept "named parameters." You can name your parameters as noted above but you must add them to your ParameterCollection in the correct order! They must be in the same order that they are in your SQL statement. There is a link in my signature if you want to read more.


  6. #6
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: VB .NET, Access, SQL, and LIKE operator

    One last footnote. Access you use "*" with LIKE, and in SQL Server you use "%".

    Example
    LIKE '%dumb%' ---SQL Servier
    LIKE "*dumb*" ---Access

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VB .NET, Access, SQL, and LIKE operator

    Actually... it does support them.... it just doesn't care what the names are and will use positional reference to plug the parameters in. SQL Server on the other hand, fully supports named parameters and will complain if the names don't match.

    -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??? *

  8. #8
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: VB .NET, Access, SQL, and LIKE operator

    The * with Access for wildcards is only appropriate if using DAO access methods. All other methods even for MS Access woud use the % sign as a wildcard.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    35

    Re: VB .NET, Access, SQL, and LIKE operator

    Adding parameters to the new query function was exactly what I was looking
    for. Thanks everyone!

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