Carrying a variable to another form

dougancil

Well-known member
Joined
Jun 29, 2010
Messages
50
Programming Experience
Beginner
I have the following form:

VB.NET:
Imports System.Data.SqlClient
Public Class Form1
    Dim ReturnValue As Object = Nothing
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connectionString As String
        Dim cnn As SqlConnection
        Dim myCommand As SqlCommand
        Dim dteReturnValue As DateTime = Nothing
        'the connection string to the SQL server'
        connectionString = "Data Source=10.2.1.41;Initial Catalog=MDR;uid=xxxxx;password=xxxxx"
        cnn = New SqlConnection(connectionString)
        cnn.Open()
        'the SQL query'
        Try
            myCommand = New SqlCommand("select payrolldate from payroll where payrolldate <= getdate ()")
            myCommand.Connection = cnn
            ReturnValue = myCommand.ExecuteScalar()
            If ReturnValue IsNot Nothing Then
                dteReturnValue = Convert.ToDateTime(ReturnValue)
                dteReturnValue = dteReturnValue.AddDays(1)
            End If
        Catch exp As SqlException
        End Try
        cnn.Close()
        Dim ButtonDialogResult As DialogResult
        ButtonDialogResult = MessageBox.Show(String.Format("The next date available to you is {0}", dteReturnValue.ToShortDateString()), "Payroll", MessageBoxButtons.YesNo)
        If ButtonDialogResult = Windows.Forms.DialogResult.Yes Then
            'pass 'Return Value' to SQL query that will be here'
            Button2.Enabled = True
            Button1.Enabled = False
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Form2.Show()
    End Sub
    
End Class

and what I need to do is to pass the value of ReturnValue into my second form, because this is a date range that I need to help present results to users. Right now form two is as follows:

VB.NET:
Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MDRDataSet2.Exceptions' table. You can move, or remove it, as needed.
        Me.ExceptionsTableAdapter.Fill(Me.MDRDataSet2.Exceptions)
        'TODO: This line of code loads data into the 'MDRDataSet1.mOpInterval' table. You can move, or remove it, as needed.
        Me.MOpIntervalTableAdapter.Fill(Me.MDRDataSet1.mOpInterval)
        'TODO: This line of code loads data into the 'MDRDataSet.Employees' table. You can move, or remove it, as needed.
        Me.EmployeesTableAdapter.Fill(Me.MDRDataSet.Employees)

    End Sub

    Private Sub FillByToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillByToolStripButton.Click
        Try
            Me.ExceptionsTableAdapter.FillBy(Me.MDRDataSet2.Exceptions)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub FillBy1ToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillBy1ToolStripButton.Click
        Try
            Me.ExceptionsTableAdapter.FillBy1(Me.MDRDataSet2.Exceptions)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub FillByToolStrip_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles FillByToolStrip.ItemClicked

    End Sub
End Class

So as you can see, its just a data set to be presented at the moment, but the return value in form one is needed to help specify data in form 2. Can anyone offer any help please.

Thank you

Doug
 
You either need to :
1) declare the variable in a seperate module or
2) declare the form where the variable is set in the second form where you wish to access it from.

first option is more tidier.
 
I thought by having the returnvalue declared as high as I did that I was declaring it in a seperate module. Is that not true? And if so, where should I declare it?
 
Doug, where u have declared it....it is still localized to that form. Lets say you have declared it in FormA then you need to add up high in FormB
Dim a as new FormA
Then whenever you refer to it in FormB refer to it as a.returnvalue
OR
the better way would be in Visual Studio to
1) Click on <Project> then <Add Module> and then <Module>
2) Then declare your variable within the module as Public Returnvalue as Integer (or whatever type you choose)

Returnvalue would now be available across you entire solution.
 
Learner,

So then how do I call that module from within either a private or public function? I'm still learning so please forgive my ignorance here.

Thanks

Doug
 
The easiest way to access a single variable from anywhere in the program is to set up the variable in "My.Settings". Then you access with "My.Settings.MyVariable = value". You can even set your program to automatically save the value when the program closes if you wish.
 
If it's just a single variable, then yes. I only use the module method if I am going to have multiple global variables.
 
At this point, I do think that it will just be the one variable, BUT just in case it's not, can you provide an example of calling a variable from a module.

Thank you

Doug
 
Sure. In the module, declare your variable as public.

Then, to access it you use: <ModuleName>.<VariableName> (example: MyModule.MyVariable = value)
 
So then this is still ok?

VB.NET:
Imports System.Data.SqlClient
Public Class Form1
    Dim ReturnValue As Object = Nothing
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connectionString As String
        Dim cnn As SqlConnection
        Dim myCommand As SqlCommand
        Dim dteReturnValue As DateTime = Nothing
        'the connection string to the SQL server'
        connectionString = "Data Source=10.2.1.41;Initial Catalog=MDR;uid=xxxxx;password=xxxxxx"
        cnn = New SqlConnection(connectionString)
        cnn.Open()
        'the SQL query'
        Try
            myCommand = New SqlCommand("select payrolldate from payroll where payrolldate <= getdate ()")
            myCommand.Connection = cnn
            ReturnValue = myCommand.ExecuteScalar()
            If ReturnValue IsNot Nothing Then
                dteReturnValue = Convert.ToDateTime(ReturnValue)
                dteReturnValue = dteReturnValue.AddDays(1)
            End If
        Catch exp As SqlException
        End Try
        cnn.Close()
        Dim ButtonDialogResult As DialogResult
        ButtonDialogResult = MessageBox.Show(String.Format("The next date available to you is {0}", dteReturnValue.ToShortDateString()), "Payroll", MessageBoxButtons.YesNo)
        If ButtonDialogResult = Windows.Forms.DialogResult.Yes Then
            'pass 'Return Value' to SQL query that will be here'
            Button2.Enabled = True
            Button1.Enabled = False
        End If
    End Sub

Then I have this for my module:

VB.NET:
Module Module1
    Public Returnvalue As Integer
End Module

(I'm not calling the module here, but I just want to make sure that where I have the variables as they are now, are ok)

Thanks

Doug
 
Sorry Doug, been tied up trying to make a VB.Net program obtain a fingerprint image from a fingerprint reader. I see that you question has been ably answered by GGunter.
 
Learner,

I think I may go the way of the module at this point, simply because I don't know what other variables I may be needing to access in form 2. I just want to make sure that the code I presented above is still good code and that adding the module to this won't cause any odd complications. I'm trying to write code as cleanly as I can.
 
Back
Top