Solved

passing a value from a form to another form

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

707 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

18 Experts available now in Live!

Get 1:1 Help Now