Passing Data from one form to another

  • Thread starter Thread starter Deano
  • Start date Start date
D

Deano

At the moment entering data into a form. On one of the
fields is a product list, but the product list is to big
so a button is there for to open a search form. On
selecting a button, a form appears with a list box and
other search criteria. Upon finding the product required,
I double click the product and want this product to pass
to the other. The data being passed back needs to go into
a drop-down box or a text box, which ever is best.

Can anyone Help

Thanks in Advance
 
Hi Deano,

You can either put code in the first form which pulls the value from the
second form before it closes or put the code in the second form to 'push'
the value to the first form (again) before closing.

Often I will go with the first method - first I open the second form in
'dialog' mode using the acDialog option of the WindowMode paremeter of
docmd.Openform. When used, the code in the first form will pause until the
second form (the one being opened) is either hidden or closed. With this in
mind, on the second form put an 'OK' button which the user clicks to
indicate that a selection has been made (you could do the same thing with
the double click event on the product control). The code behind the click
event of the OK button should hide the form (rather than close it). Once the
second form is hidden, the code in original form will resume and can use the
value in the control on the now hidden form to set the value of the combo.
Then the second form is closed by the first form. Here's some sample code:

Code on first Form:

Private Sub Command19_Click()
DoCmd.OpenForm "form2", , , , , acDialog
Me.Productid = Forms!form2.lstProductID
DoCmd.Close acForm, "form2"
End Sub

Code on second form:

Private Sub cmdOK_Click()
Me.Visible = False
End Sub
 
Afterthought from my previous post. This will copy the data from one form to the other, but the form receiving the data may not automatically display it. If it doesn't, add the code:

[Forms]![Form 2]![Control 2].Requery

Or maybe just

[Forms]![Form 2].Requery

I can't remember, I find Requery a bit of an odd one.

Cheers

David
 
Thanks you for getting back to me so quick.

It works just fine.

I had tried the second option before, but found I was
duplicating the forms needed for other data input which
ended up too many forms and naming conventions. I think I
can cut this down with the solution you gave

Thanks again for you help
-----Original Message-----
Hi Deano,

You can either put code in the first form which pulls the value from the
second form before it closes or put the code in the second form to 'push'
the value to the first form (again) before closing.

Often I will go with the first method - first I open the second form in
'dialog' mode using the acDialog option of the WindowMode paremeter of
docmd.Openform. When used, the code in the first form will pause until the
second form (the one being opened) is either hidden or closed. With this in
mind, on the second form put an 'OK' button which the user clicks to
indicate that a selection has been made (you could do the same thing with
the double click event on the product control). The code behind the click
event of the OK button should hide the form (rather than close it). Once the
second form is hidden, the code in original form will resume and can use the
value in the control on the now hidden form to set the value of the combo.
Then the second form is closed by the first form. Here's some sample code:

Code on first Form:

Private Sub Command19_Click()
DoCmd.OpenForm "form2", , , , , acDialog
Me.Productid = Forms!form2.lstProductID
DoCmd.Close acForm, "form2"
End Sub

Code on second form:

Private Sub cmdOK_Click()
Me.Visible = False
End Sub




--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.

At the moment entering data into a form. On one of the
fields is a product list, but the product list is to big
so a button is there for to open a search form. On
selecting a button, a form appears with a list box and
other search criteria. Upon finding the product required,
I double click the product and want this product to pass
to the other. The data being passed back needs to go into
a drop-down box or a text box, which ever is best.

Can anyone Help

Thanks in Advance


.
 
Thanks for help.

I full understood your example but already solved it with
Sandras solution before I saw yours.

Cheers anyway
 
You're welcome - I forgot to one big advantage about using the second
method. The second form doesn't know about the first form since there are no
hardcoded references to it. This makes the solution usable from other forms.
In fact, I will often wrap the calling code in a public procedure and call
it from the other forms that need the same functionality. This is even more
portable.

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.


Thanks you for getting back to me so quick.

It works just fine.

I had tried the second option before, but found I was
duplicating the forms needed for other data input which
ended up too many forms and naming conventions. I think I
can cut this down with the solution you gave

Thanks again for you help
-----Original Message-----
Hi Deano,

You can either put code in the first form which pulls the value from
the second form before it closes or put the code in the second form
to 'push' the value to the first form (again) before closing.

Often I will go with the first method - first I open the second form
in 'dialog' mode using the acDialog option of the WindowMode
paremeter of docmd.Openform. When used, the code in the first form
will pause until the second form (the one being opened) is either
hidden or closed. With this in mind, on the second form put an 'OK'
button which the user clicks to indicate that a selection has been
made (you could do the same thing with the double click event on the
product control). The code behind the click event of the OK button
should hide the form (rather than close it). Once the second form is
hidden, the code in original form will resume and can use the value
in the control on the now hidden form to set the value of the combo.
Then the second form is closed by the first form. Here's some sample
code:

Code on first Form:

Private Sub Command19_Click()
DoCmd.OpenForm "form2", , , , , acDialog
Me.Productid = Forms!form2.lstProductID
DoCmd.Close acForm, "form2"
End Sub

Code on second form:

Private Sub cmdOK_Click()
Me.Visible = False
End Sub




--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.

At the moment entering data into a form. On one of the
fields is a product list, but the product list is to big
so a button is there for to open a search form. On
selecting a button, a form appears with a list box and
other search criteria. Upon finding the product required,
I double click the product and want this product to pass
to the other. The data being passed back needs to go into
a drop-down box or a text box, which ever is best.

Can anyone Help

Thanks in Advance


.
 
Could I trouble you again and ask for an example. I'm not
to bad in understanding the coding through forms but had
no experience through modules and public procedures.

I'm using this situation quite a lot through my dbase
(Access 2003)


Thanks again
 
Back
Top