Passing string parameter from Form1 to Form2

Posted on 2006-04-18
Last Modified: 2012-08-14
I found this piece of code on the forum that apparently worked for someone.  But it's confusing to me.
I want to pass a string from say Form1 to Form2.

Another option is to create a Class and store the variable there.  I have done that for an application I created and it works well.  For example:

Imports System

Public Class Global (whatever you want to call it)

Private Shared EmpID as String

Public Shared Property EmpIDVal as String
       Return EmpID
   End Get
   Set(ByVal Value As String)
       EmpID = Value
   End Set
End Property

To use in a form, send the value to this class by saying global.EmpIDVal = txtEmpID.Text.

In order to get the value in a form, use the following code:
Public Function ID() As String
        Return Global.EmpIDVal
    End Function

Hope this helps :-)

The generated code from my Form2 looks like this:

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

Are you able to use the 'Dim frm AS New Form2(text_value)' and modify the Form2_Load() above to accept this
text_value.  Or do you have to balloon across the Atlantic to do this....using code similar to the code I pasted
at the top.  This is a function that will be necessary for me in about (20) forms.  If it means setting up a Class
or something else that can be used globally..I'm all for it.  Just the same I've never created a separate Class
nor do I know where it would go.

My Module1.vb contains public variables as follows:

Imports System.Data
Imports System.Data.SqlClient

Module Module1
     Public this and that
End Module

If it can be added here...where do I add it.
Otherwise where and how would I add a Class to be used globally through the application and then how
would I suscessfully use it?

Thanks in advance!
Question by:TSFLLC

    Author Comment

    I did get the create new Class code above to work.

    However, what is more resourceful...or should I say efficient?

    Create the class I included or just create a Global variable in Module1 and access it throughout the program?
    LVL 85

    Accepted Solution

    It seems like you want to pass the string from Form1 to Form2 when you create Form2.

    If so, then...

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim data As String = "data to pass to Form2"
            Dim f2 As New Form2(data)
        End Sub

    End Class

    Public Class Form2

        Private data As String

        Public Sub New(ByVal dataIn As String)
   = dataIn
        End Sub

        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            MessageBox.Show(, "Data Passed In", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Sub

    End Class
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    What is more efficient?

    That really depends on what you're doing...

    Will you have more than one instance of Form2 that will require a different value to be passed to each?
    LVL 10

    Expert Comment

    I can think of 2 quick ways you could do it
    1. Using your Module1 to store the value as a global variable

    Module Module1
      Public EmpID As String
    End Module

    In Form 1
    Module1.EmpID = "1234"
    ' then Open Form 2

    In Form 2
    Dim EmpID As String = Module1.EmpID

    2. Create an overrload in your form2 to accept the value as an argument

    In Form2
    Private _EmpID As String
    Public Sub New(EmpID As String)
      _EmpId = EmpID
    End Sub

    _EmpId now stores the value

    In Form1
    Dim frm2 as New Form2("1234")

    note: If you plan on using the 2nd approach - it'll add value to create a base form that includes this overload. Then Form2 will inherit from this form as follows

    Class myBaseForm
      inherits Form
      Private _PassedValue As String
      Public Sub New (Value As String)
        _PassedValue = Value
      End Sub
    End Class

    Then in the form
    Class Form2
      inherits myBaseForm

      Public Sub Click(..., ...) handles cmd.Click
      End Sub
    End Class

    LVL 10

    Expert Comment

    oops - I didn't realize y'all figured it out already - I guess I had it open too long without refreshing...LOL!!!
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    It's no biggie bchoor...I do it often as well.  =)

    Author Comment

    Mike(Idle Mind),

    I used your code and it works well.  Enjoy the points...

    I'm assuming that I would need to add the 'Public Sub New()' code in each subsequent form
    that requires the value to be passed?

    What's more efficient...more instances of Form2 that require different values?

    Yes, the value will be different for each form.  It's a unique record id for each table I store.
    That's how I determine the record to display in all of my data entry forms.
    I include this record id as the 1st column with width = 0 from a datagrid that contains table


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video discusses moving either the default database or any database to a new volume.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now