Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Passing variables between forms and "being able to use the varable in form 2..

I thought I knew how to pass a varable.. you know ... on form 1 you say
form2.gettext.text = varable_being_passed
Then on form two you can actually print the varable out or see it in

Now my problem is I am passing a number to be inserted into a counter.  In order to insure that the number is available for processing I am inserting in the form load area........  
In the Timer1 area I am trying to use the number.

The problem is ...  it doesnt work...  The information is passing, as I can see it print out in a label on form two. I can also see it by passing it to a text box.  But i DONT WANT TO SEE IT... i WANT TO USE IT in  math on form two in the timer area...   However in the form load area in debug testing to position of the curser...  the value does not arrive in a timely manner.  The text or label I use shows empty or zero..  

What do I need to do to get the numeric info to form two in a timely manner... so I can use it in a math or other function?

There are actually 30-40 forms... I have tried to simplify the question.
Thanks in advance!

1 Solution
could you possibly paste the code in question...
Try moving the code from Form_Load to the Form_Activate event.
also if you are using the number in a timer event, then set the timer's enabled property to false at design time and set it to true at run-time.  this way you are sure you have gotten the number before processing any events in the timer (if i understand your question right)

Private Sub Form_Load()
   Timer1.Enabled = True
End Sub
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
I did not fully understand, but i think what you need is either of the following:

--- Form's code
Private mlngValue as Long
Public Property Let Value ( Value as Long)
mlngValue = Value
End Property
Public Property Get Value () as Long
 Value = mlngValue
End Property
--- End of Form's code

Now you can read/write the new property of the form, this inside and outside the form's code
Form2.Value = 4
Debug.Print Form2.Value
inside the form
Value = 6
Debug.Print 6

Other idea:
Activate your Timer at the end of Form_Load, during design time set Enabled=False

Private Sub Form_Load()
  ' Other code here

  Timer.Enabled = true
End Sub

I have some other ideas in mind, but i need some more information about the problem,

Here are a few ways you could pass a variable from one form to another:

1. In form1 declare a variable as public (eg Public myNumber as Double).  Then, in form2, you can type "Form1.myNumber".  

2. Use a global variable declared in a module, and a boolean flag to indicate that it has been initiated.  For example, create a public module called modGlobalVariables:

Public MyNumber as Double
Public MyNumberInitialized as Boolean

In Form1:

MyNumber = 20
MyNumberInitialized = True
end sub

Form2: Timer1()
if MyNumberInitialized then { ... }
end sub

3. Make a class and use WithEvents.  Create a property that, when written to (i.e. Property Let) fires off an event.  Instantiate the class in Form2 (as public), then access it from Form1.  When Form1 does a Form2.MyObject.MyNumber = 20, Form2 will automatically fire your event.

4. In form 2, make subroutine for Form1 to call:
Public Sub Form1DataChanged(d as double)
  { do stuff }
end sub

In form 1, call that routine
  Form2.Form1DataChanged 20
end sub

Hope this helps,

Try this:

In the calling form (Form1), first init the GetNum variable, then load the form:

Private Sub Command1_Click()
    Form2.GetNum = 500
End Sub

In the called form (Form2), the GetNum variable has already been initialized when it is loaded, so everything should work peachy.  Make sure GetNum is declared Public:

Public GetNum As Long

Private Sub Form_Load()
    Debug.Print "Form2_Load GetNum = " & GetNum
    gettext.Text = CStr(GetNum)
End Sub
When you call
Form2.gettext.text = varable_being_passed
Form2 is being loaded *before* the contents of the textbox are set.  So in the Load event, the default text for the textbox is read.  You will see in the Activate event, that the textbox has been updated to the correct value.

Anytime you access a control of a form which is not loaded, it first loads, then sets the property.
gracetimeAuthor Commented:
Actually your answer has helped to solve the problem...  

I am still having problems converting from different; string to date, .. numeric to date...boolean to date...
Where can one get info on just  "how to convert values"?

Just curious...  Using the # 2 solution... "boolean" example.. I got a boolean number on form 2..  However I didnt know how to convert it back to an integer....  How do you do that? I tried a few ways but it did not seem to happen for me....

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now