Pantheo
Pantheo

Reputation: 129

How to use two forms to search in one and display the records to the other using Visual Basic 2010, SQL Server 2008

I have 2 forms in my visual basic 2010 project. Both are connected with an SQL 2008 Server where i keep a db with customers records(name,surname,address, etc).

What i want to do is to give certain criteria in the first form, lets say name,and hit a search button. The program should search the db if the specified name exists.If it exists i want to show the results in the second form. By results i mean the name , surname, address,etc. If it does not exist i want to prompt the user to insert data into the db.

The problem is that i know how to do this work in a single form but not how to make the two forms "communicate" with each other (meaning that one does the search and the other the projection-adding part).

Any help is appreciated

Edit

I am sorry guys but i cannot figured it out. I tried setting the filters but all i get is errors. I will post whatever i have written so far in case someone can help me out. So here it is. As i stated earlier i know how to search for the data in a single form(lets call it DisplayForm). As Olivier mentioned i have textboxes for the records, a combo box with the search criteria(name , surname,id) and a Search button that perfoms the search and populates the text boxes. The code for the search button is this:

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

    Dim intPosition As Integer


   'Combo box code
    Select Case cboField.SelectedIndex
        Case 0 
            ObjDataView.Sort = "surname"
        Case 1 
            ObjDataView.Sort = "id"
    End Select


    If cboField.SelectedIndex = 0 Then

        intPosition = ObjDataView.Find(txtSearch.Text)
    ElseIf cboField.SelectedIndex = 1 Then

        intPosition = ObjDataView.Find(CType(txtSearch.Text, Integer))
    End If


    If intPosition = -1 Then
        MessageBox.Show("No customer found", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
    Else
        objCurrencyManager.Position = intPosition

    End If
End Sub

I want this procedure be done in a second form (lets call it FormSearch) and populate the textboxes in form DisplayForm. I know that it is easy enough for someone that knows VB but i am tottally a newbie and desperatly need help. Thanks a lot for any advice

Upvotes: 1

Views: 7422

Answers (1)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112392

In your display form add this code

Public Sub SetFilter(name as String)
   ' Set the filter here
End Sub

In your filter form do this

DirectCast(Application.OpenForms(0), DisplayForm).SetFilter(NameTextBox.Text)

Here I assume that DisplayForm is your main form and that it's name is DisplayForm. You would call this in a button click event hanlder, for instance.


EDIT in response to comment:

You said that you know how to do it on a single form. I do not know how you do it, but probably you have textboxes for the name, the address, etc. that you are looking for. Then probably you press a button called btnSearch. By pressing this button you trigger

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch_Click.Click
     ' The search logic goes here
End Sub

What I am telling you is to replace btnSearch_Click by Public Sub SetFilter(name as String) that is public. You would move those search fields and the button to the second form and from there call SetFilter of the first form in order to communicate with it and tell it to search a customer. In my example, I pass only a name, but I left it up to you to pass more parameters as required.

Assuming that your customer form is called "DisplayForm", you could access it like this

Dim cust As DisplayForm
cust = DirectCast(Application.OpenForms("DisplayForm"), DisplayForm)
cust.SetFilter(NameTextBox.Text)

My answer was only about the communication between the two forms, assuming that you already managed the other part.


Searching for "VB.NET Tutorial: Working With Data" on YouTube results in six videos on this subject. Video #3 in particular shows how to use filters.


EDIT #2. Here is a little more help:

I do not know how you are opening the two forms. Since you need a reference to the display form in the search form, it is easier to open the search form as the main form at application startup. At Form_Load in the search form, you then open the display form by code

Code in the search form:

' Reference to the display form
Private _displayForm As DisplayForm

' Create the display form and open it
Private Sub SearchForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    _displayForm = New DisplayForm()
    _displayForm.Show()
End Sub

' Get the search parameters and tell the display form to set the filter
Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
    Dim field As String, search As Object

    Select Case cboField.SelectedIndex
        Case 0
            field = "surname"
            search = txtSearch.Text
        Case 1
            Dim n As Integer

            field = "id"
            If Integer.TryParse(txtSearch.Text, n) Then
                search = n
            Else
                MessageBox.Show("Please enter a number for id search", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
                Return
            End IF
    End Select
    _displayForm.SetFilter(field, search)
End Sub

Code in the display form that actually performs sorting and searching:

Public Sub SetFilter(ByVal field As String, ByVal search As Object)
    Dim intPosition As Integer

    ObjDataView.Sort = field
    intPosition = ObjDataView.Find(search)
    If intPosition = -1 Then
        MessageBox.Show("No customer found", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
    Else
        objCurrencyManager.Position = intPosition
    End If
End Sub

Upvotes: 2

Related Questions