Search code examples
vbavariablestransfer

How do I pass Form1.TextBox1.Text to Form2.TextBox2.Text using Visual Basic (correctly)?


This is a continuation of a question asked two years ago in this thread: VB6 equivalent of string.IsNullOrEmpty (I think.) A programmer recommended I use the String.IsNullorEmpty method, which I used this thread for to convert to Visual Basic, but I still couldn't get it to work.

The specifics of my question are here, including all current code: http://www.daniweb.com/software-development/visual-basic-4-5-6/threads/473930/passing-data-between-forms-in-vba

Here is the gist of it, copied directly from the second link: So I'm trying to make a link between TextBox1.Text on Form1 to TextBox2.Text on Form 2. What I currently have is a line of code underneath my TextBox2_Change code reading: TextBox2 = Form1.TextBox1.Text

This ALMOST does what I want it to do. The only problem is that it is requiring me to input any character in to the TextBox2 when Form2 pops up before it displays.

I'm trying to get that problem solved and then I'm eventually going to try to get it to chop off part of the file name until just the project file name displays.......but that's a whole different game I'll be playing. One step at a time.

Does anyone have any suggestions?


Solution

  • Explanation

    You should write the code under the Form2_Load event.

    If you write the code under Textbox2_TextChanged event, the code will be executed only when you type or delete something in Textbox2 (That is the same as Text being changed).

    Code and Example

    Private Sub Form2_Load () Handles Mybase.Load
            Textbox2.Text = Form1.Textbox1.Text
    End Sub
    

    Hope it works perfectly!