Solved

passing a value from a form to another form

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
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…

776 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