Solved

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

Posted on 2000-05-07
8
162 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
Comment Utility
could you possibly paste the code in question...
0
 
LVL 32

Expert Comment

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

Expert Comment

by:AzraSound
Comment Utility
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Accepted Solution

by:
philchang earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now