Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data from to 1

Status
Not open for further replies.

orisons

Programmer
Feb 18, 2002
49
CA
how can I get a variable from one form to another?
this has been killing me recently as I need two forms (one as a setup form whick can be accesesed throught the menu bar, but cant seem to make the data work between the two forms??????
 
If the variables have been declared as public, then you can access them from other forms.

In Form 1, in the Declarations Section

Code:
Option Explicit

Public Var1 as Integer
Public Var2 as String

Private Sub Form1_Load 
...

Now in Form2, the following Declarations Section

Code:
Option Explicit

Public Var1 as Integer
Public Var2 as String

Private Sub Form1_Load 
...

Now each form has a Var1 and a Var2 as Integer and String respetively. Inside some routine within Form1, I can have the following code.

Code:
Private Sub Some_Routine

   Var1 = 23
   Var2 = "Form 1 String"
   Form2.Var1 = 18
   Form2.Var2 = "Form 2 String"
   MsgBox Var1 + Form2.Var1
' Will Show 41 in the Box
Code:
   MsgBox Var2 & " " & Form2.Var2
' Will Show "Form1 String Form 2 String" in the Box
Code:
End sub

Similarly, from within Form2 you can access the public variables from form1 by qualifying their reference with the form name. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top