How to Search for records in a Database using VB.net

L

Lucky

I have a vb.net windows app that connects to an access database. The
database has 1 table. One of the columns is called "Address". A user
enters any address into a textbox (txtValue) then clicks the Find
button (cmdFind) and a datagrid displays all the rows with this
address. The problem is that it will only find the the address if the
user types it exactly as it's entered the the DB. For example: the
address column has 123 Anystreet Rd. If the user types 123 Anystreet
then clicks Find, no rows are returned. But if the user types 123
Anystreet Rd, all rows are displayed containing that address. I don't
want that because the user won't know exactly how it's stored in the
DB. I used an oleDbDataAdapter and Query builder for my select
statement which is: SELECT *
FROM [Primary Site List]
WHERE (Address LIKE ?)

My cmdFind_click event code is as follows (I'm using option buttons):

If optAddress.Checked = True Then
OleAddressAdapter.SelectCommand.Parameters("Address").Value
= txtValue.Text
DsPrimarySite1.Clear() 'this is my dataset
OleAddressAdapter.Fill(DsPrimarySite1)
End If

How can I get it to return rows containing any portion of the address
entered by the user?

I am a newbie and this is my first app, so please be detailed in your
help. I greatly appreciate any assistance you can provide.
 
R

Ryan Cooper

Your question really is not a .NET question but a SQL question. Anyways, if
you are using the LIKE feature in a sql statement, you have to use a special
"wildcard" character (%) to make the system look for records that don't
match exactly. Heres an example:

"SELECT * FROM Customers WHERE Address LIKE '%" & strAddressSearch & "%'"

This will return ALL records that contain the strAddressSearch variable
anywhere in the Address field.

Alternatively, you can place the % on only one side of the variable to
search for records with the address
starting with:
"SELECT * FROM Customers WHERE Address LIKE '" & strAddressSearch & "%'"
or ending with:
"SELECT * FROM Customers WHERE Address LIKE '%" & strAddressSearch & "'"
the search string.

Sorry, I'm not going to re-write all your code for you, but this is what you
need in your SQL statement to make the LIKE feature work. Not sure that you
can do this with the query builder, you may have to code this by hand.
You'll learn more doing it that way anyways.

HTH
Ryan
 
L

Lucky

Thanks for the hint Ryan. Going to try it out tomorrow. I really don't
think I asked anyone to re-write all my code for me though. Heck I
only posted 5 lines of code plus the select statement so folks would
know where I'm coming from. My database is Access 2000 and I'm coding
in vb.net, but I guess the SQL query gave you the impression I was
asking a SQL question. I know it gets confusing, believe me. After
being stuck on this the past 4 days I'm ready to try anything!
 
L

Lucky

Unfortunatly that solution doesn't work. Query builder wouldn't accept
that.
Does anyone else have any ideas? More info is that I'm using an
oleDbDataAdapter to connect to my Access database.
 
R

Ron Allen

Lucky,
QueryBuilder doesn't like this because 'straight' Access uses a
different syntax for wildcards. Use * vs % in query builder. You will need
to use the other syntax through ADO.NET though.

Ron Allen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top