Solved

passing a value from a form to another form

Posted on 1999-01-14
2
144 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 100 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

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month7 days, 18 hours left to enroll

617 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