I've been working on building a back end on an access database that allows users to search records using a web search box. I've been able to successfully set up simple search query's but have run into trouble where multiple criteria with "AND" statements (OR statements would return too much to be useful). My problem is this, when all five text boxes are filled out the query returns records no problem, but if even one is left blank i get nothing what so ever. Is there any thing I can do in either SQL or VB to either ignore null values entirely or convert them to wildcard values?
9 2655
The SQL code is: - SELECT [Full Title] AS Full_Title, [Author List] AS Author_List, [Title Call Number] AS Title_Call_Number, Series, Location
-
FROM Library
-
WHERE ([Full Title] LIKE '%' + ? + '%') AND ([Author List] LIKE '%' + ? + '%') AND ([Title Call Number] LIKE '%' + ? + '%') AND (Location LIKE '%' + ? + '%') AND ([Search Terms] LIKE '%' + ? + '%') ORDER BY [Full Title]
Frinavale 9,735
Recognized Expert Moderator Expert
Could you use an OR instead of an AND in your SQL query to get what you're looking for?
If not, I suggest that you build it according to what the user has entered: -
Dim sqlQuery as String = " SELECT [Full Title] AS Full_Title, [Author List] AS Author_List, [Title Call Number] AS Title_Call_Number, Series, Location " _
-
" FROM Library "_
-
" WHERE "
-
-
Dim whereStatement As String
-
If String.IsNullOrEmpty(txt_Title) = False Then
-
whereStatement = "([Full Title] LIKE '%' + ? + '%')"
-
End If
-
-
If String.IsNullOrEmpty(txt_AuthorList) = False Then
-
whereStatement = " AND ([Author List] LIKE '%' + ? + '%')"
-
End If
-
-
'....
The second approach is messy so I suggest you check out the first suggestion first :)
-Frinny
@Frinavale
Lol Sorry the first suggestion was the code I am currently using and it doesn't display null values. I'll try this approach though. Messy is ok It has to be easy on the user end not necessarily for me :P. I could use an OR statement but that risks information overload on the user end.
Frinavale 9,735
Recognized Expert Moderator Expert
Perhaps ask the Access Experts how to create a sql query that best suits your needs before pursuing the messy path :)
NeoPa 32,556
Recognized Expert Moderator MVP
Unless you have the ANSI-92 option set in Access you would use '*' instead of '%'.
String concatenation is normally done with & rather than + (although + should work in most cases. See Using "&" and "+" in WHERE Clause).
I'm not sure what the ? means or does in your code, but that would need to be replaced by the value (or Null) of whatever the operator selected.
Otherwise, it seems you have the fundamental concept right already.
Welcome to Bytes!
I apologize there seems to be miscommunication. The Access Database is already up and functional. Where I am having trouble is with Asp.net. In Access for a wildcard query if a field is left blank then acess automatically ignores that field and pulls up the rest of the results based on the the others(i.e. if i have four fields for search criteria and leave one blank, Access will ignore that one and search the other three.) whereas if a box is left blank in asp.net it will look for a null value in that field and since that field is not blank in the database that record will be omitted despite the contents of the other three fields. I'm looking to simulate what is going on in Access.
In an idea inspred by Frinavale I am currently trying to get the "SelectCommand=" to change depending on what textboxes actually have a string by using a VB event.
Here's what I have so far: -
Public Class SearchResults2
-
Inherits System.Web.UI.Page
-
Dim sqlQuery As String
-
Dim whereStatement As String
-
-
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
-
End Sub
-
-
Private Sub SearchResults2_PreLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreLoad
-
Library.SelectCommand = " SELECT [Full Title] AS Full_Title, [Author List] AS Author_List, [Title Call Number] AS Title_Call_Number, [Series] AS Series, [Location] AS Location"
-
Library.SelectCommand &= "FROM Library "
-
Library.SelectCommand &= "WHERE "
-
If Not String.IsNullOrWhiteSpace(TSearch.Text) Then
-
Library.SelectCommand &= "([Full Title] LIKE '%' + ? + '%')"
-
ElseIf String.IsNullOrWhiteSpace(TSearch.Text) Then
-
Library.SelectCommand &= "([Full Title] Like '%')"
-
-
End If
-
-
If Not String.IsNullOrWhiteSpace(AUSearch.Text) Then
-
Library.SelectCommand &= " AND ([Author List] LIKE '%' + ? + '%')"
-
ElseIf String.IsNullOrWhiteSpace(AUSearch.Text) Then
-
Library.SelectCommand &= " AND ([Author List] Like '%')"
-
-
End If
-
-
If Not String.IsNullOrWhiteSpace(CNSearch.Text) Then
-
Library.SelectCommand &= " AND ([Title Call Number] LIKE '%' + ? + '%')"
-
ElseIf String.IsNullOrWhiteSpace(CNSearch.Text) Then
-
Library.SelectCommand &= " AND ([Title Call Number] LIKE '%')"
-
-
End If
-
-
If Not String.IsNullOrWhiteSpace(LSearch.Text) Then
-
Library.SelectCommand &= " AND ([Title Call Number] LIKE '%' + ? + '%')"
-
ElseIf String.IsNullOrWhiteSpace(LSearch.Text) Then
-
Library.SelectCommand &= " AND ([Title Call Number] LIKE '%')"
-
-
End If
-
If Not String.IsNullOrWhiteSpace(KWSearch.Text) Then
-
Library.SelectCommand &= " AND ([Search Terms] LIKE '%' + ? + '%')"
-
ElseIf String.IsNullOrWhiteSpace(KWSearch.Text) Then
-
Library.SelectCommand &= " AND ([Search Terms] LIKE '%')"
-
End If
-
End Sub
-
-
Private Sub SearchResults2_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
-
TSearch.Text = Request.QueryString("Title").ToString
-
AUSearch.Text = Request.QueryString("Author").ToString
-
CNSearch.Text = Request.QueryString("CallNumber").ToString
-
LSearch.Text = Request.QueryString("Location").ToString
-
KWSearch.Text = Request.QueryString("Key_Words").ToString
-
-
-
-
End Sub
-
@dascott20
Keep In mind, that I was hired on as a student in net admin, not programming or web development. However this is my tasking at work so I can't hand it off. So please leave clear suggestions (Small words and color pictures appreciated :P).
Frinavale 9,735
Recognized Expert Moderator Expert
What you have should work (are you having problems with it?) but I recommend that you create your query using a StringBuilder instead of concatenating everything into the Library.SelectCommand. The reason I recommend this is because it takes less resources to use a StringBuilder than to concatenate strings together (strings are immutable and cannot be changed so when you add 2 together you are creating a new string...whereas a StringBuilder is better for performance).
Please note that this is not really necessary to accomplish your goal but will speed up your application slightly.
-Frinny
This is to let everyone know that I have solved this issue, the answer posted by Frinavale below was very helpful in that process. If you are reading this to solve a simisare problem, below is a solid setup, with two important factors to remember.
1)Declare All your variables.
2)Keep in mind what order you are coding youre commands.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: manning_news |
last post by:
I've got the following query in SQL 2000:
select a.SSN, a.MonthName, a.IMClinicDay,
b.IMClinicDay as SecondDay
from tblResidentRotations a
inner join view7 b
on a.SSN = b.SSN
where...
|
by: Martin R |
last post by:
Hi,
How to find first not null value in column whitout chacking whole table
(if there is a not null value then show me it and stop searching, the
table is quite big)?
thx,
Martin
*** Sent...
|
by: darrylforma |
last post by:
What I need is an SQL query that joins three tables. TableA and TableB are joined on Column1 which is common to both. Rows are filtered by Column2 in TableA equalling a specific value...
|
by: RvGrah |
last post by:
I have a TabelAdapter that has an int column that allows null (no
lectures please, I have my reasons). When I use intellisense to fill in
the parameters in the InserQuery method, it shows the...
|
by: djdarpan |
last post by:
Hi,
I am new with MySql and PHP.
I have a problem while I am checking NULL value in database.
I need
If there is empty field in my table row return message "Somthing "and if there is...
| |
by: mwanstall |
last post by:
Hi All,
I have started tearing my hair out over this problem!
I am pulling some data from a table and passing it as variables into a function in Access. One of the variables I'm passing through...
|
by: billa856 |
last post by:
Hi,
I am trying to insert Null value in column(ShipDate) in my table.That column(ShipDate)'s type id date/time and format is short date.
I am using "" to insert Null in that column(ShipDate)...
|
by: wizardry |
last post by:
hello -
i'm trying to insert a blob into my table, it will insert but the string that i insert when i query the inserted data returns null with 0 bytes in the column.
I have other tables set...
|
by: prabirchoudhury |
last post by:
CRITERIA;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+...
|
by: qwedster |
last post by:
Folk!
How to programattically check if null value exists in database table (using stored procedure)?
I know it's possble in the Query Analyzer (see last SQL query batch statements)?
But how...
|
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,...
| |
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: 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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |