473,568 Members | 2,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 oleDbDataAdapte r 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.Chec ked = True Then
OleAddressAdapt er.SelectComman d.Parameters("A ddress").Value
= txtValue.Text
DsPrimarySite1. Clear() 'this is my dataset
OleAddressAdapt er.Fill(DsPrima rySite1)
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 38798
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 '%" & strAddressSearc h & "%'"

This will return ALL records that contain the strAddressSearc h 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 '" & strAddressSearc h & "%'"
or ending with:
"SELECT * FROM Customers WHERE Address LIKE '%" & strAddressSearc h & "'"
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.goo gle.com...
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 oleDbDataAdapte r 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.Chec ked = True Then
OleAddressAdapt er.SelectComman d.Parameters("A ddress").Value
= txtValue.Text
DsPrimarySite1. Clear() 'this is my dataset
OleAddressAdapt er.Fill(DsPrima rySite1)
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**@ryancoope r.com> wrote in message news:<qu******* ***********@typ hoon.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 '%" & strAddressSearc h & "%'"

This will return ALL records that contain the strAddressSearc h 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 '" & strAddressSearc h & "%'"
or ending with:
"SELECT * FROM Customers WHERE Address LIKE '%" & strAddressSearc h & "'"
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
oleDbDataAdapte r to connect to my Access database.
"Ryan Cooper" <in**@ryancoope r.com> wrote in message news:<qu******* ***********@typ hoon.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 '%" & strAddressSearc h & "%'"

This will return ALL records that contain the strAddressSearc h 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 '" & strAddressSearc h & "%'"
or ending with:
"SELECT * FROM Customers WHERE Address LIKE '%" & strAddressSearc h & "'"
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.goo gle.com...
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
oleDbDataAdapte r to connect to my Access database.
"Ryan Cooper" <in**@ryancoope r.com> wrote in message news:<qu******* ***********@typ hoon.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 '%" & strAddressSearc h & "%'"
This will return ALL records that contain the strAddressSearc h 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 '" & strAddressSearc h & "%'" or ending with:
"SELECT * FROM Customers WHERE Address LIKE '%" & strAddressSearc h & "'" 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
6744
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 anything available for filtering/searching records? Is there any Filter Bar available in DataGridView control similar Component One True DBGrid control? ...
5
5804
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 Table1 ORDER BY StudentID ASC", cnn, adOpenStatic, adLockReadOnly, adCmdText
1
1901
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 Text Box and a Search Button. Each record of my database has 5 fields:
3
1172
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 results of the search are displayed in a small datagrid. My problem is that I've no idea how to create the search functionality with VB.NET. The...
9
8273
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 records in the database. Is it possible to create a SQL Server database using vb.net? I know I can use vb.net and ADOX to create a Access database....
8
4629
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 displaying records. now i wrote all the JDBC code in .java and i am accessing that code in jsp file. but this time i am getting only exceptions not...
1
7519
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 having the sql statement dynamically built according to the input provided by the user. I have used the method described here hundreds of times it is...
2
1911
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 page. So, I want to search for something between two dates (fromDate) and (toDate). So far, I have the user enter in the details into a form on one...
0
2953
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 it is linked to a database (Access 2003). I can move from one document to the next or previouse, I can exit, enter new records and even delet. the...
12
77955
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 give me some idea where I am going wrong ?? I just want to retrieve data from my emp_mstr table and display it using my JSP file... The table...
0
7693
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.