Passing label.text value to another form

Posted on 2006-04-25
Last Modified: 2010-04-07
Hi Experts,

I need to be able to pass a label.text value that is derived as follows to another form.

        Incident_ID.Text = "IN-" & CStr("T588") & "-" & Month(Now) & Year(Now) & "-" & Minute(Now) & Second(Now)

Using this method, I can create a unique string of characters for each record entered.  How can I pass this unique string of characters to another form, so that when additional information is completed on the second form all the data relates properly in the database?  Your time and assistance is appreciated.

I am developing in VB2005 using SQL 2005.
Question by:NCSO
    LVL 52

    Expert Comment

    by:Carl Tawn
    Several options:

    1) Make the label public and access it directly through the form.
    2) Add a property to your form to return the value rather than making the Label public.
    3) Have a method on your second form that your first form can call, passing the value of the label.
    4) Store the value globally where it can be accessed from both form.

    Author Comment

    Which of the (4) is most preferred?  
    LVL 52

    Expert Comment

    by:Carl Tawn
    Personally I would probably go with 3).

    Basically because I assume that Form1 opens Form2. If thats the case then Form1 can call the method of Form2 immediatley after creating it. Secondly, the Label remains private to the form it belongs to, so theres no chance of any rogue element altering it. And thirdly, it prevents Form2 having to know anything about the form that created it.

    Author Comment

    Could you give me some code example for doing (3), I tried something similar to it yesterday and I was not successful.  Thanx
    LVL 52

    Assisted Solution

    by:Carl Tawn
    Haven't got time to test this, but it should be something like:

    Form 2:

        Public Sub SetLabel(ByVal pString As String)
            '// Do whatever you need to do with the value
        End Sub

    Form 1:

        '// Assume this is in an event handler

        Dim f As New Form2()
    LVL 85

    Accepted Solution

    carl_tawn has given a great answer already...

    You said:

        "I am developing in VB2005"

    This works in VB.Net 2005, but NOT in earlier versions of VB.Net.

    If you are using only ONE instance of Form2 at any given time, then you can use the old VB6 style syntax to reference the Form using JUST the Forms name.   You don't need to create an instance of it.  Just declare a Public variable in Form2 and set it from Form1 before showing it:

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Form2.someValue = "IN-" & CStr("T588") & "-" & Month(Now) & Year(Now) & "-" & Minute(Now) & Second(Now)
        End Sub

    End Class

    Public Class Form2

        Public someValue As String

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

    End Class

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    759 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

    12 Experts available now in Live!

    Get 1:1 Help Now