Solved

Sql Statement for searches

Posted on 2006-11-02
15
200 Views
Last Modified: 2010-04-23
Hello, I have the following SQL Statement, I will have about 8 different textboxes, for different search criteria, firstname, lastlame, amountpaid, etc...  I will just use 2 for the example though.  Right now, if I put in the first and lastname, it finds them.  If I only put one or the other in, it throws an error, saying that one of them was null.  I would like the user to be able to search by any of the criteria, making none of them required.

DsSearch1.Clear()
            Cmd.CommandText = "SELECT ReceiptNo, PolicyNo, pmt1, pmt2, pmt3, type1, type2, type3, total, rcvdby, firstname, lastname, typeaccount, company, checkmaker, checkno, Datercvd, notebox FROM Receiptinfo WHERE firstname='" & txtfirstname.Text & "' Or lastname='" & txtlastname.Text & "';"
            Cmd.CommandType = CommandType.Text
            Da = New OleDb.OleDbDataAdapter(Cmd)
            DGResults.DataSource = DsSearch1.Receiptinfo
            Da.Fill(DsSearch1.Receiptinfo)
0
Comment
Question by:seanrhudy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 10

Expert Comment

by:Kinger247
ID: 17857554
Why dont you create the SQL WHERE clause as

Dim strWhere as string

If txtfirstname.Text <> "" Then
0
 
LVL 10

Expert Comment

by:gangwisch
ID: 17857558
           Cmd.CommandText = "SELECT ReceiptNo, PolicyNo, pmt1, pmt2, pmt3, type1, type2, type3, total, rcvdby, firstname, lastname, typeaccount, company, checkmaker, checkno, Datercvd, notebox FROM Receiptinfo WHERE"
dim inserted as boolean =false
if firstname.textlength>0 then
Cmd.CommandText &= firstname='" & txtfirstname.Text & "'"
inserted=true
end if

if lastname.textlength>0 then
Cmd.CommandText &= iif(inserted," OR ","") & lastname='" & txtfirstname.Text & "'"
inserted=true
end if
0
 
LVL 10

Accepted Solution

by:
Kinger247 earned 250 total points
ID: 17857585
Sorry bit quick on the enter key ...

something like this ..

Dim strSQL as string = "SELECT ReceiptNo, PolicyNo, pmt1, pmt2, pmt3, type1, type2, type3, total, rcvdby, firstname, lastname, typeaccount, company, checkmaker, checkno, Datercvd, notebox FROM Receiptinfo "

Dim strWHERE as string = ""

If txtfirstname.Text <> "" Then
  if strWHERE <> "" then strWHERE &= " OR "
  strWHERE &= " firstname='" & txtfirstname.Text & "'"
End if

If txtlastname.Text <> "" Then
  if strWHERE <> "" then strWHERE &= " OR "
  strWHERE &= " lastname='" & txtlastname.Text & "'"
End if

If strWHERE <> "" Then
  strSQL = " WHERE " & strWHERE
End If
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:seanrhudy
ID: 17858200
What does the last statement do?
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17858248
sorry, should be :

 strSQL &= " WHERE " & strWHERE
0
 

Author Comment

by:seanrhudy
ID: 17858420
What does that if statement do though?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17858451
the code below checks if there is any condition in the WHERE clause and if there is, it adds the WHERE clause to the sql statement:

If strWHERE <> "" Then
  strSQL = " WHERE " & strWHERE
End If
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17858461
for example if your where clause is "lastname='Smith',

and your sql statement is "SELECT * FROM TAble1"

then  that last statement will convert your sql statement to

"SELECT * FROM Table1 WHERE lastname='Smith'
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17858511
It makes sure that only the desired columns are searched on and not all by default.
So as YZlat states you create the main SELECT SQL statement first then add on the customised WHERE clause after to create the full SQL query.
0
 

Author Comment

by:seanrhudy
ID: 17858637
ok, thank you, I dont like to just copy and paste code, I like to understand it first, makes it easier later....
0
 

Author Comment

by:seanrhudy
ID: 17863370
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim Da As OleDb.OleDbDataAdapter
        Try
            Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/backup/receiptprogram/receiptmanagement/receipt.mdb"
            Con.Open()
            Cmd.Connection = Con
        Catch ex As Exception
        End Try

        Try

            DsSearch1.Clear()
            Cmd.CommandText = "SELECT ReceiptNo, PolicyNo, pmt1, pmt2, pmt3, type1, type2, type3, total, rcvdby, firstname, lastname, typeaccount, company, checkmaker, checkno, Datercvd, notebox FROM Receiptinfo "
            Cmd.CommandType = CommandType.Text
            Da = New OleDb.OleDbDataAdapter(Cmd)
            DGResults.DataSource = DsSearch1.Receiptinfo
            Da.Fill(DsSearch1.Receiptinfo)

            Dim strSQL As String = "SELECT ReceiptNo, PolicyNo, pmt1, pmt2, pmt3, type1, type2, type3, total, rcvdby, firstname, lastname, typeaccount, company, checkmaker, checkno, Datercvd, notebox FROM Receiptinfo "

            Dim strWHERE As String = ""

            If txtfirstname.Text <> "" Then
                If strWHERE <> "" Then strWHERE &= " OR "
                strWHERE &= " firstname='" & txtfirstname.Text & "'"
            End If

            If txtlastname.Text <> "" Then
                If strWHERE <> "" Then strWHERE &= " OR "
                strWHERE &= " lastname='" & txtlastname.Text & "'"
            End If

            If strWHERE <> "" Then
                strSQL &= " WHERE " & strWHERE
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Con.Close()
            Cmd.Connection = Nothing
        End Try
    End Sub

It is bringing back a null value for the total column, when I know there is a value in there
0
 

Author Comment

by:seanrhudy
ID: 17863490
Ok, I fixed that problem, but no matter what I search for, everything row in the db is found.
0
 

Author Comment

by:seanrhudy
ID: 17863950
*correction, Every row in the database is found, and displayed.
0
 

Author Comment

by:seanrhudy
ID: 17865755
Ok its working, I changed the commandtext to "cmd.commandtext = strSQL and I put all the dataset code after the strSQL and where statements.  Thanks!!
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17865771
no problem.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question