473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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("Addres s").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.
Jul 21 '05 #1
4 38775
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


"Lucky" <tr***********@yahoo.com> wrote in message
news:2a**************************@posting.google.c om...
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("Addres s").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.

Jul 21 '05 #2
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!

"Ryan Cooper" <in**@ryancooper.com> wrote in message news:<qu******************@typhoon.sonic.net>...
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


Jul 21 '05 #3
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.
"Ryan Cooper" <in**@ryancooper.com> wrote in message news:<qu******************@typhoon.sonic.net>...
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


Jul 21 '05 #4
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
"Lucky" <tr***********@yahoo.com> wrote in message
news:2a**************************@posting.google.c om...
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.
"Ryan Cooper" <in**@ryancooper.com> wrote in message news:<qu******************@typhoon.sonic.net>...
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


Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Job Lot | last post by:
How can I filter/search records in DataGridView after filling it with data? The BindingNavigator provides default functionality for navigating, adding, deleting and saving records. Is there...
5
by: Chand | last post by:
Private Sub tbDate_AfterUpdate() Dim cnn As New ADODB.Connection Dim rst As New ADODB.Recordset rst.CursorLocation = adUseClient Set cnn = CurrentProject.Connection rst.Open "SELECT * FROM...
1
by: Miguel Dias Moura | last post by:
Hello, What I know: To send a Value in the URL and filter the results in order to display only the database records which FIELD_A = Value. What I need to do: I have a page with an Input...
3
by: Animal | last post by:
Hey, I've got the following problem: I'm trying to create a small tool that must be able to search thrue a database using a search-criteria, that has to be entered by the user in a TextBox. The...
9
by: Peter | last post by:
Hello£¬everyone, My program will collect a testing machine's data ,save the data and deal with the data everyday. I want to use vb.net to create database, add and delete tables or modify the...
8
by: menmysql | last post by:
i am not bale to solve this problem since two weeks i am trying to access records from mysql database using jsp. inside this jsp program i wrote all my JDBC code. it is working very nicely and...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
2
Cowbie
by: Cowbie | last post by:
Hello again everyone. I sorted out my problem with tables by re-jigging the code around a little bit. However, I still have one question for now. I want to be able to search my database using a php...
0
by: mrsoft100 | last post by:
Dear Members I have written a program whose intention is to track fee payment by students in a school. the program is intended to track as many students as possible. My program works very well and...
12
lifeisgreat20009
by: lifeisgreat20009 | last post by:
I am a newbie to Struts and JSP...I have been working on the code below for 5 hours now..I googled a lot but couldn't get much help so finally I am here.. Hoping of getting my problem solved. Please...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.