Calling a sub on one form from another

compguru910

Well-known member
Joined
Nov 25, 2009
Messages
47
Programming Experience
3-5
Ok, so heres my issue. Im loading a form to retrieve customer information from one form, then, when I collect the data, im trying to pass an ID back to the form that originally made the call. So, what I did is I made it so that the second form calls a sub on the first form, but for some reason it wont work. I put breakpoints in to check the variables, and they are getting data, but I can set the textbox on the first form with the data that is in the sub. Heres the code that loads the second form

VB.NET:
    Private Sub btnCustSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustSearch.Click
        Dim strLastName As String
        Dim strFirstName As String
        strLastName = txtLastName.Text
        strFirstName = txtFirstName.Text
        Using fCustomerLookup As New frmCustBrowser
            fCustomerLookup.strLastName = strLastName
            fCustomerLookup.strFirstName = strFirstName
            fCustomerLookup.ShowDialog()
        End Using

    End Sub

Here is the code that returns the id to the first form
VB.NET:
    Private Sub btnSelectCustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectCustomer.Click

        MsgBox(dgvCustomer.Item(dgvCustomer.CurrentRow.Index, 0).Value)
        frmAddRo.intCustId = dgvCustomer.Item(dgvCustomer.CurrentRow.Index, 0).Value
        frmAddRo.RetrieveCustomer(frmAddRo.intCustId.ToString)
        Me.Dispose()
    End Sub

And here is the sub that is supposed to return a name to the textbox.

VB.NET:
    Public Sub RetrieveCustomer(ByVal id As String)
        Dim strQuery As String
        Dim data As New DataTable
        Dim strFirstName As String
        strQuery = "SELECT * FROM customer WHERE cust_id = '" & id & "'"
        data = dbReturnTable(strQuery)
        strFirstName = data.Rows(0).Item(1)

        txtFirstName.Text = "3"

    End Sub

Thanks in advance for the help
 
Firstly, turn Option Strict On.

Try it the other way round. Move the intCustId property onto the 2nd form. Instead of calling Dispose, call Close.

2nd form:-

VB.NET:
Private _intCustID As Int32 = -99
Public Property intCustID() As Int32
    Get
        Return _intCustID
    End Get
    Set(ByVal value As Int32)
        _intCustID = value
    End Set
End Property


Private Sub btnSelectCustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectCustomer.Click
    _intCustId = Convert.ToInt32 (dgvCustomer.Item(dgvCustomer.CurrentRow.Index, 0).Value)
    Messagebox.Show (_IntCustId.ToString)
    Me.Close()
End Sub

Now change the calling code on your 1st form to :-

VB.NET:
Private ReturnedID as Int32

Private Sub btnCustSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustSearch.Click
    Dim strLastName As String
    Dim strFirstName As String
    strLastName = txtLastName.Text
    strFirstName = txtFirstName.Text
    Using fCustomerLookup As New frmCustBrowser
        fCustomerLookup.strLastName = strLastName
        fCustomerLookup.strFirstName = strFirstName
        fCustomerLookup.ShowDialog()
[B]        ReturnedID = fCustomerLookup.intCustId
        If ReturnedID >= 0 then
            RetrieveCustomer (ReturnedID)
        End If[/B]
    End Using
End Sub
 
Works great, thank you. I figured I would have to make it so that new instance of form2 would return something, I just didnt know how to do it. Thanks alot
 
Back
Top