Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

passing a value from a form to another form

Posted on 1999-01-14
2
Medium Priority
?
147 Views
Last Modified: 2010-05-03
i had problem passing a value that had obtain in a form
then i had to use the value in another form
how do i do that ?
i know that i can declare it in the first form
but the value of the variable can't be send to another form
please help me
0
Comment
Question by:winson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 300 total points
ID: 1456145
When you declare a variable in form1 declare it as

Public Myvar as Integer   'Example

in form2 reference it using form2.Myvar

e.g.

msgbox cstr(form2.myvar)


0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1456146
deighton's answer works, but I spotted a small typing error
it should read:
- in form2, reference the variable as form1.Myvar
- msgbox cstr(form1.Myvar)

With this minor modification, his code works, and for that he deserves to have his answer accepted.

Another solution might be to define Myvar in an ordinary VB module, and define it as a Public or a Global variable:
Public Myvar As Integer 'or
Global Myvar As Integer
That way it is available to both form1 and form2

There are some "advanced" VB considerations that are not be a problem to you if you are only beginning to learn VB but could become more important later.

"form1" and "form2" should be regarded as "Classes" defined in VB.

From a "purist" point of view you are doing something iffy when you are referring to the "Class names" form1 and form2 as if they are actual object variables. That is what you are doing when you are using form1 and form2 in statements such as
form1.Show
form1.Myvar = Value

But VB does allow you to do it, so it's OK.

If you want to start using multiple instances of a form however, it becomes very important to avoid using this "implicit" behaviour. A couple of rules of thumb:

1 Only use "form1" (or whatever you decided to name the form) after an "As", and declare object variables to refer to instances of this class:
   Dim objFrmMain As Form1
   Public objX As Form1
   Private objY As New Form1

2 If you haven't used the New keyword in the declaration, you must use it in a Set statement before you can start using one of these object variables:
    Set objX = New Form1

3 Within the code module for form1, use the Me keyword whenever you have to refer to the current instance of this class:
   Unload Me  ', not Unload form1

4 When you need to access variables such as the variable Myvar in your example, refer to the correct object variable
   objY.Myvar

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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

718 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