Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to get button click to search multiple fields in vb.net

Posted on 2016-08-30
11
Medium Priority
?
76 Views
Last Modified: 2016-08-30
I have 2 search criteria fields, with one search button. If Criteria 1(SearchFlightCode) is entered, then the button click need to search the database. If Criteria 2 (SearchFlightName) is entered, then the same button click need to search the same database based on Criteria 2.

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
         SearchFlightCode(tbFlightCode.Text)
         SearchFlightName(tbFlightName.Text)
         Me.Cursor = System.Windows.Forms.Cursors.WaitCursor 
        btnSearch.Enabled = False
        dgvFlightEnquiry.DataSource = Nothing

        Load_Data(ds)

        Me.Cursor = System.Windows.Forms.Cursors.Arrow
        btnSearch.Enabled = True

    End Sub

Open in new window

0
Comment
Question by:fadiel ras
  • 6
  • 5
11 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41775858
OK.  What is the question?
0
 

Author Comment

by:fadiel ras
ID: 41775859
Hi, how do I get the single button click to handle multiple search criteria being entered?
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41775878
By doing what you are currently doing.  

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click

         SearchFlightCode(tbFlightCode.Text)     'Search on the code ??

         SearchFlightName(tbFlightName.Text)   'Search on the name  ??


         Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        btnSearch.Enabled = False
        dgvFlightEnquiry.DataSource = Nothing

        Load_Data(ds)

        Me.Cursor = System.Windows.Forms.Cursors.Arrow
        btnSearch.Enabled = True

    End Sub
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41775879
You might want to post what the two SearchFlightXXXX functions do.
0
 

Author Comment

by:fadiel ras
ID: 41775883
thanks Andy.
so the search works 100% on "SearchFlightName(tbFlightName.Text)", but not on the other, yet my code dealing with the Sql part is basically identical (meaning, I've copied and pasted it in VB, simply changed the Sql Parameter).
Could there be anything else driving this behavior?
0
 

Author Comment

by:fadiel ras
ID: 41775889
The 2 Functions for my search

Private Sub SearchFlightName (FlightName As String)
        Try
            Dim strSql As String = ""

            ClearParm()

            AddParm("@FlightName", FlightName, DbType.String)

            strSql = " SELECT  FlightCode , FlightName , Active "
            strSql = strSql & " ,'N' RowModified "
            strSql = strSql & " From tblFlights "
            strSql = strSql & " where FlightName = @FlightName "
            ds = SelectDataSet(strSql)
        Catch ex As Exception
            MsgBox("Error finding Data", MsgBoxStyle.Exclamation, "Load Error - Test Data")
        End Try
    End Sub


    Private Sub SearchFlightCode (FlightCode As String)
        Try
            Dim strSql As String = ""

            ClearParm()

            AddParm("@FlightCode", FlightCode, DbType.String)

            strSql = " SELECT  FlightCode , FlightName , Active "
            strSql = strSql & " ,'N' RowModified "
            strSql = strSql & " From tblFlights "
            strSql = strSql & " where FlightCode = @FlightCode "
            ds = SelectDataSet(strSql)
        Catch ex As Exception
            MsgBox("Error finding Data", MsgBoxStyle.Exclamation, "Load Error - Test Data")
        End Try
    End Sub
End Class

Open in new window

0
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 41775890
As the SearchFlightName is the second call of the two are you just throwing away the results of the first search?

You might need a function rather like:
SearchFlight(tbFlightCode.Text, tbFlightName.Text)

and inside that function code like (air code to show the logic)
if tbFlightCode.Text = "" then
  search on name only
else if tbFlightName.Text = "" then
  search on code only
else
  search on both name and code
end if
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41775894
the two search functions.  Yes,  you are throwing the first search results away.  See my previous comment for what you should be doing.
0
 

Author Comment

by:fadiel ras
ID: 41775909
thanks, trying it and revert asap
0
 

Author Comment

by:fadiel ras
ID: 41775929
Thank you so much Andy. It worked!

The amended "Button Click" code as suggested by Andy

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click

        SearchEnquiry(tbRPHFlightCode.Text, tbRPHFlightName.Text)

        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        btnSearch.Enabled = False

        dgvFlightEnquiry.DataSource = Nothing
        Load_Data(ds)

        Me.Cursor = System.Windows.Forms.Cursors.Arrow
        btnSearch.Enabled = True
    End Sub

Open in new window


The Amended code to include the IF , THEN

    Private Sub SearchEnquiry(FlightCode As String, FlightName As String)
        Try
            Dim strSql As String = ""
            ClearParm()

            AddParm("@FlightCode", FlightCode, DbType.String)
            AddParm("@FlightName", FlightName, DbType.String)
            If tbRPHFlightName.Text = "" Then
                strSql = " SELECT  FlightCode , FlightName , Active "
                strSql = strSql & " ,'N' RowModified "
                strSql = strSql & " From tblFlightCodes "
                strSql = strSql & " where FlightCode = @FlightCode "
                ds = SelectDataSet(strSql)
            End If
            If tbRPHFlightCode.Text = "" Then
                strSql = " SELECT  FlightCode , FlightName , Active "
                strSql = strSql & " ,'N' RowModified "
                strSql = strSql & " From tblFlightCodes "
                strSql = strSql & " where FlightName = @FlightName "
                ds = SelectDataSet(strSql)
            End If
        Catch ex As Exception
            MsgBox("Error finding Data", MsgBoxStyle.Exclamation, "Load Error - Test Data")
        End Try
    End Sub

Open in new window

0
 

Author Closing Comment

by:fadiel ras
ID: 41775930
Thanks Andy. you certainly saved me hours, as I already wasted a few prior to asking for assistance.
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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

879 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