-4

Currently I have a TextBox on the first form called txtuserid and I want to pass the value of this to another TextBox called USERIDTextBox on a second form.

But when I try to run my code below, nothing gets passed to the TextBox on the second form. So I'm just wondering how I can pass this value from one form to another form?

Here is my code:

Private Sub cmdlogin_Click(sender As Object, e As EventArgs) Handles cmdlogin.Click
    Try
        If cn.State = ConnectionState.Open Then
            cn.Close()

        End If
        cn.Open()
        cmd.CommandText = "select userid,state from registration where userid= " & _
            "'" & txtuserid.Text & "' and state='" & txtpw.Text & "'"

        Dim dr As OleDb.OleDbDataReader
        dr = cmd.ExecuteReader
        If (dr.HasRows) Then
            While dr.Read

                ' My Problem: 
                ' This code shows the 2nd form but the USERIDTextBox value doesn't change?
                Dim obj As New Sale
                obj.USERIDTextBox.Text = txtuserid.Text
                obj.Show()
            End While
        Else
            MsgBox("Invalid username or PW")
        End If
        cn.Close()
    Catch ex As Exception

    End Try
End Sub
6
  • Could you explain how your question and your code block are connected? What is wrong with your code?
    – gunr2171
    Commented Jul 27, 2015 at 12:49
  • Is 'Sale' the name of your other form? You should add a messagebox in your error handler in case you're missing an exception. Commented Jul 27, 2015 at 12:54
  • YES SIR SALE IS MY ANOTHER FORM WHERE I WANT USER ID OF LOGIN FORM'S TXTUSERID VALUE TO SALE FORM'S TEXT BOX USERIDTEXTBOX.
    – Ram Niwas
    Commented Jul 27, 2015 at 13:01
  • 1
    a) dont use global connections. b) dispose of db objects to avoid leakage c) use parameters, dont concat SQL d) dont use empty Catch blocks. If you have important data or logic to be shared with multiple forms, dont embed it inside one form. Use a class which can easily contain logic and data shared and used throughout the app. Commented Jul 27, 2015 at 13:01
  • 2
    @Plutonix - You forgot e) please don't type in ALL CAPS! :P Commented Jul 27, 2015 at 13:27

1 Answer 1

1

As a general rule, it's not a good idea to try accessing another object/forms controls directly. Instead, a better way to do it would be to pass the text in the 1st form's TextBox to a custom constructor on the 2nd form (the Sale one). Then the constructor on the 2nd form will be responsible for setting the value of the TextBox .

Here is an example of one way you could do this:

Sale.vb

Public Class Sale

    Dim secondFormInputText As String

    Public Sub New(inputTextFromFirstForm As String)
    InitializeComponent()

    ' Set the class variable to whatever text string was passed to this form
    secondFormInputText = inputTextFromFirstForm
    End Sub

    Private Sub Sale_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Set the textbox text using this class variable
    USERIDTextBox.Text = secondFormInputText
   End Sub

End Class

Login.vb

Private Sub cmdLoginExample_Click(sender As Object, e As EventArgs) Handles cmdLogin.Click
    Dim obj As New Sale(txtuserid.Text)
    obj.Show()
End Sub

So now instead of setting the Sale form's TextBox directly, you can pass the text on the 1st form to the constructor of the 2nd form. The constructor can then save the text it received to a class variable that the rest of the 2nd form can use.

One of the main benefits of this, is that if in the future you change your TextBox to a RichTextBox or possibly another control that might not even have a Text property, you won't have to go updating every single piece of code that tries to set the textbox value directly.

Instead you can change the TextBox to some other control, update the Sales form once with whatever changes you need to work with the new control, and none of the code on the other forms should need to be changed.

Edit:

Even though this question was specifically about how to pass a textbox value from one form to another form, you may also like to read the comments under your question. In particular, Plutonix had some very helpful advice on how you can improve your database code which might be of use to you.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.