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

pass a value form 1 form to another

Status
Not open for further replies.

AlastairOz

Technical User
Jul 27, 2010
81
AU
I have a form set with 2 forms.
Form1 main form
Form2 look up

Form1 has a search box where you type in an ID and it will go to the required record.
If the user does not know the ID number then I have made a look up with form2.
Form2 ends up with a value.

At the moment I pass the value to a text box on Form1 eg

thisformset.NEWDEFECTS.txtbox4.Value=thisform.txtAssetFind.Value

I tried passing this value directly to Form1's search text box. This does not work, as the text box is looking for the value to be typed in.
Not really sure how to progress, any help would be appreciated.



 
Directly related to your problem: There are two major events: ProgrammaticChange and ItneractiveChange. So the textbox of your form1 should call the same code from both of these events, then you can programmatically set it from form2 and it would have the same effect of finding the id as if it would be typied in.

Indirectly related recommendations: Formsets have some problems, let alone with zorder of forms of several formsets you could start at once. So don't use formsets.


You can also share a datasession between forms, if a secondary form is set to datasession=1 (default datasession). This does not mean it's working in the datasession number 1, which the datasession window displays as name "Dafault(1)", but this means the form will not start a new private datasession and continue working in the current one, so it will share the data with the form it's called by.

What this lacks is the ability to adress controls of the calling form, but that's not good anyway, because you're mixing business logic with interface. So even if you can you shouldn't work that way anyway. That's a chapter on it's own.

A first step for a better decoupling would be to have a form1 method which would seek or loacate one ID. Then you can call that method from form1.txtsearch.interactivechange() and also form1.txtsearch.programmaticchange() and even better call that from form2 when the id is picked there, eg from form2.btnSelect.Click(). That's decoupling the business logic of finding some record from the interface of which you now already have two different ones - a) knowing an ID and typing it within form1 or b) picking from a list in form2.

For being able to address from1 from form2 without a formset you'd pass a form reference to form2 when calling it, simply pass THISFORM from form1 and store an init parameter in form2 to be able to adress form1 within it's btnSelect.click().

Bye, Olaf.
 
Alastair,

First, formsets are very demodé. They are a hang-over from pre-Visual days, and do not fit in with the modern way of doing things.

In this case, a better option would be to make Form 2 modal. That way, you can easily pass a value back to Form 1.

In Form 2, set the WindowType to 1. In the Unload method, issue a RETURN, to pass back whatever value Form 1 is expecting.

In Form 1, in the button (or whatever) which launches Form 2, do something like this:

DO FORM Form2 TO lnReply

The variable lnReply will then contain whatever value Form 2 returned. The nice thing about this approach is that neither form has to know anything about the controls in the other form.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
In the first form, call the second form by passing in parameters.

Example:
In the parent form.


DO childForm with lnValFromParent TO lnValFromChild


Now in the child form, accept the parameter(s) in the INIT event by adding a PARAMETERS statement. Example:


INIT EVENT
PARAMETER t_parentID

I also create a property to hold the value that I want to return so I know it is still available when the form unloads. So create a propery called parentID on the child form.

Prior to the THISFORM.RELEASE save the value to the property.

Example:
Thisform.returnVal = myvalue


In the UNLOAD event of the child form code the following:

RETURN this.myvalue

The parent form will now have control and variable lnValFromChild will be populated.

TIP: If you are going to be searching data or moving the record of the current table you will likely want to make the data session of the 2nd form a private data session or it could end up moving your record pointer and you might not want that.


Jim
 
Alastair,

Why don't you BINDEVENT()? It's a clean and elegant solution.

Brad Sanmarie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top