How to use LIKE keyword in to MS ACCESS DB

I know this is very simple and I have done it in other databases, but for some crazy reason it doesnt work
when i try to use it in VB to access data in MS Access.

Ok this is the scenario I have 1 text box which i want the user to retrieve last names of other users in the
database, but just incase some one doesnt know how to spell correctly a person last name I would like to use
a Wild card Key like the keyword 'LIKE' to retrieve anything that resembles what the user put in the text box.

this is my sample code:


Dim connString As String = " Provider = Microsoft.Jet.OLEDB.4.0; Data Source = X:\members.mdb;"
        Dim myConnection As OleDbConnection = New OleDbConnection ' Stores a connection
        Dim insertCmd As OleDbCommand = New OleDbCommand     'Stores object of OLEDB Command

        Dim LastName = txtLastName.Text.Trim

        'Opening the DB Connection
        myConnection.ConnectionString = connString

        ' Now to create a select command to search Customer ID in the DB
        'Trying to put a search command with the user input from the text box

        With insertCmd

            .Connection = myConnection
            .CommandText = "Select LastName  From UserTable " & _  *****''''Here is my problem!!!
                            " Where LastName Like %@LastName "

            .CommandType = CommandType.Text

            .Parameters.Add(New OleDbParameter("@LastName", OleDbType.Char, 25))
            .Parameters("@LastName").Value = LastName

      End With




            'To bind the Dataset to the DataGrid :)
            grdResults.DataSource = DataSet

            'Tell the DataGrid which table in the Dataset to use
            grdResults.DataMember = DataSet.Tables(0).TableName

            'Setting the Alternating Colors property to the Grid
            grdResults.AlternatingBackColor = Color.WhiteSmoke

            'Set the GridLineStyle Property :)
            grdResults.GridLineStyle = DataGridLineStyle.None

            'Set the SelectionBackColor and the Selection ForeColor Properties
            grdResults.SelectionBackColor = Color.LightGray
            grdResults.SelectionForeColor = Color.Black


        Catch OleDbExceptionErr As OleDbException
        Catch InvalidOperationExceptionErr As InvalidOperationException

        End Try

        'Cleaning up the Mess
        insertCmd = Nothing
        Adapter = Nothing
        DataSet = Nothing

        myConnection = Nothing


Keep in mind that everything else works just fine.
Why!?!..well because I changed the SQL code from

.CommandText = "Select LastName  From UserTable " & _  
                            " Where LastName=@LastName "

This is good, but this then means the user has to be specific to a person last name if not then it doesnt retrieve, which of course makes sense based on the SQL statement.

//////////////** Also to keep ahead of the game//////
I have tried a few other ways and it still doesnt work

            .CommandText = "Select LastName  From UserTable " & _  
                            " Where LastName Like ' %@LastName'  "

And tried it this way:

  .CommandText = "Select LastName  From UserTable " & _  
                            " Where LastName Like ' %@LastName% ' "

tried it like this :

  .CommandText = "Select LastName  From UserTable " & _  
                            " Where LastName Like %@LastName  "


So please tell me where Am i going wrong LOL

Thankyou in advance for helping a noob out !!! :)

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this

            .Connection = myConnection
            .CommandText = "Select LastName  From UserTable " & _  *****''''Here is my problem!!!
                            " Where LastName Like @LastName "

            .CommandType = CommandType.Text

            .Parameters.Add(New OleDbParameter("@LastName", OleDbType.Char, 25))
          .Parameters("@LastName").Value = "%" & LastName
try this:

        With insertCmd

            .Connection = myConnection
            .CommandText = "Select LastName  From UserTable " & _  
                            " Where LastName Like @LastName "

            .CommandType = CommandType.Text

            .Parameters.Add(New OleDbParameter("@LastName", OleDbType.Char, 25))
          .Parameters("@LastName").Value = "%" & LastName & "%"

     End With

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
masterat03Author Commented:
Thankyou G_johnson for the help it worked out like a charm.

I was wondering if there is a good book out there that contains SQL code to use in
Because im always getting trouble with this part. Even though I know a decent amount of SQL
I always get my hands tied when it comes to this part.

don't know, don't use books.  LOL
just research, trial and error, looking at things closely, etc.
and EE a lot
masterat03Author Commented:
LOL yeah so far the same with myself as well. Usually search Google and now I'm starting to get heavily addicted to EE LOL
as always thankyou for your help.

Take care :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.