Passing label.text value to another form

Posted on 2006-04-25
Medium Priority
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
  • 3
  • 2
LVL 52

Expert Comment

by:Carl Tawn
ID: 16533827
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

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

Expert Comment

by:Carl Tawn
ID: 16534012
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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 16534143
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
Carl Tawn earned 600 total points
ID: 16534244
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 86

Accepted Solution

Mike Tomlinson earned 1000 total points
ID: 16534584
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) 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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

862 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