×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to use one variable created in form1 in another form (form2)

How to use one variable created in form1 in another form (form2)

How to use one variable created in form1 in another form (form2)

(OP)

    I have been making some basic programs in Visual Basic. One of the problems I have and I havent solved is when I create a variable (example variable "a") in my first form (form1), then when I create another form. If I want to use the value calculated in form1 for the "a" variable in my new form2 to calculate another variable "b".  How can I pass one varible from one form to another?

RE: How to use one variable created in form1 in another form (form2)

Declare the variable as global in your main module. Then, you can set the value in Form1 and use the value in Form2.

'Main Module

Public VarA As Single

Sub Main()

    Form1.Show 1

    Form2.Show 1

End Sub

'Form 1
Private Sub cmdOK_Click()
    'Get the value for A
    VarA = CSng(Text1.Text)
End Sub

'Form 2
Private Sub Form_Load()
    'Calculate value B and show it
    VarB = Sqr(VarA)
    Text1.Text = Format(VarB,"##0.0##)
End Sub

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: How to use one variable created in form1 in another form (form2)

(OP)
Do I have to show both forms at the begining?. Can I show the form2 after getting value a?
Sub Main()

    Form1.Show 1

    Form2.Show 1

End Sub

RE: How to use one variable created in form1 in another form (form2)

Yes, that is what the example was to represent. To expand on the previous example, try this test project. It will show a form (Form1) and allow you to enter a number (VarA). Then, it will calculate a new value (VarB) and show the result in Form2.

Insert a Module and this code:

Public VarA As Single
Public VarB As Single

Sub Main()

    Form1.Show 1

    VarB = 2 * VarA

    Form2.Show 1

    End

End Sub

Insert a Form (name it Form1)
Insert a Label with the string "Enter a Number:"
Insert a TextBox (Text1)
Insert a Command Button (cmdOK)
Insert this code:

Private Sub cmdOK_Click()
    'Get the value for A
    VarA = CSng(Text1.Text)

    Unload Me
End Sub

Insert a Form (name it Form2)
Insert a Label with the string "New Value:"
Insert a TextBox (Text1)
Insert a Command Button (cmdOK)
Insert this code:

Private Sub Form_Load()
    'Show the New Value (B)
    Text1.Text = CStr(VarB)
End Sub

Private Sub cmdOK_Click()
    Unload Me
End Sub

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close