Solved

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

Posted on 2000-05-07
8
170 Views
Last Modified: 2010-05-02
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
gettext.text

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........  
Getnum=Val(gettext.text)
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!
Dansvb



0
Comment
Question by:gracetime
[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
8 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2786118
could you possibly paste the code in question...
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2786133
Try moving the code from Form_Load to the Form_Activate event.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2786145
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()
   Getnum=Val(gettext.text)
   Timer1.Enabled = True
End Sub
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2786160
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,


0
 
LVL 1

Accepted Solution

by:
philchang earned 100 total points
ID: 2786161
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:

Form1_Load()
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
Form_Load()
  Form2.Form1DataChanged 20
end sub

Hope this helps,

Phil
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2786163
Try this:

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

'~~~~~
Private Sub Command1_Click()
    Form2.GetNum = 500
    Form2.Show
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
'~~~~~
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2786190
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.
0
 

Author Comment

by:gracetime
ID: 2794126
Actually your answer has helped to solve the problem...  

I am still having problems converting from different formats...ie; 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....

..
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 Month3 days, 13 hours left to enroll

630 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