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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing value

Status
Not open for further replies.

EscapeUK

Programmer
Jul 7, 2000
438
0
0
GB
I have a rather complex app that I am looking at. As I am new to vb I have a very simple question that requires a simple answer nothing to complex or clever.

I have a value on one form and would like to pass it to another. What is the simplest way of doing this.

Please keep the steps simple

Thanks
 
In form2, reference the field on form1 absolutely, eg;

form1 contains a textbox, text1

in form2's code (label1 being on form2):
label1.caption = form1.text1.text

 
yes, but - realize that both forms must be open. Form2 will be open, - but form1 needs to be open also, otherwise the reference will not provide the value, but will cause problems.

EscapeUK wants an 'easy' way to do it - not a possible way to do it.

Several 'things' need to be calriified/understood before the 'easy way' can be given.

Perhaps the easy way would be to set the transfer up to not have 'problems' to begin with.Some assumptions here which are necessary because EscapeUK did not provide much information:

[tab]Form2 is NOT the startup form.
[tab]


In the startup object, reference some general (public) module.

In the referenced general module, decalre a public (or global) variable (I'll call it txtToXfer).

In the startup object, set txtToXfer to some general value which would at least NOT cause a problem when used in Form2. This could be a value which, while it does not ause an error, is never a valid value for the transfer. If this is done, Form2 could check for the invalid value and 'know' there was a problem.

In Form1, the control which has the value to be 'transfered' to elsewhere (e.g. Form2) whenever it is changed should set the value of [txtToXfer] to it's own value.

If Form2, set the controlsource of the control which uses/dispalys the transfered value to txtToXfer.
MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
What version of vb are you using?

Assuming you are using vb5 or vb6 (I'm not sure about 4) then MichaelRed is wrong in saying that both forms must be open. If the value you have is in a text box on form1 (open) and you want to pass it to a text box on form2 (open or not), then the statement below (written inside form1)

form2.MyTextBoxOnForm2.text = MyTextBoxOnForm1.Text

will work fine. If form2 is not loaded, then the above statement will load it, exactly as if the form had been declared using

Dim Form2 as New Form.

Note that the form will merely load - in order to see it you must use the form's show method. Note also that it will stay loaded until expressly unloaded.

Finally (and I know you have said you are new to VB, but - I've started so I'll finish!), forms can have properties, just like class modules, so if it is just a value being passed, and not the contents of a text box, then this may be the way to go. You can define Property Lets, Sets and Gets in exactly the same way as you would in a class module.
 
EscapeUK -

There is a FAQ that covers this.

Chip H.
 
Your question has already been answered many times in this forum.  Therefore I recommend you to practice the Search facility or to exploit the FAQ area.

In the meantime, have a look at FAQ222-400 Tranferring data from one form to another. (courtesy Chiph!)

This FAQ is based on questions and their answers exposed in Thread222-37333, Thread222-42216, Thread222-42637 and others.  You may find it usefull to check them out.

Good Luck!
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top