Solved

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

Posted on 2000-05-07
8
167 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
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 142

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ms access #TYPE! error on report when no data 4 75
VBA saving file message display 5 63
to transfer string from C lanaguage to VBA 4 64
Passing a Text Box name to a Sub 6 96
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

774 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