One way to do this is to use an invisible text box on your main form, and
use the On Close event of the second form to fire some code to set the value
of the hidden text box on the main form to the parameter value, so the
latter becomes available in the main form.
First form, design view: add a text box, call it, say, txtParam, and set its
visible property to No.
Second form, design view: form properties, tab Events; put the cursor in the
box next to the On Close event, and click on the little button with the
threee dots appearing on the right. Select Code Builder. You will be taken
to the VBA window, and see the following lines:
Private Sub Form_Close()
End Sub
Copy and paste the following line of code in between:
Forms("FirstFormName").Controls("txtParam") = Me.ControlName
where you change FirstFormName to the name of your first form, ControlName
to the name of the control on the second form which holds the parameter you
wish to pass back to the first one (and txtParam to whatever name you
selected for the hidden text box on your first form). Save and close. The
code will run every time you close the second form, and put the parameter
value in the hidden text box on the first form.
Alternative:
Another way to do it is declare a public variable in a general module, and
use the On Close event of the second form to set the variable value to the
parameter value. Then the parameter value is available to any object in your
application either through code, or using a small fucntion that rerurns the
variable's value.
Open any standard module (or make a new one if you don't have one) and paste
the following line at the top, right after the existing one "Option Compare
Database":
Public MyParam As String (or Double, or Long, whatever is your parameter
type)
This is the public variable declaration.
Then paste the following code underneath:
Function Get_MyParam()
Get_MyParam = MyParam
End Function
Through this function the value of MyParam is made available to non-code
objects such as queries, forms and reports.
To set the value of the public variable when closing the second form, use
the On Close event like before, and paste the following line of code:
MyParam = Me.ControlName
To retrieve the value of MyParam in the first form, use the following
expression in the control source property of a control or in an expression:
=Get_MyParam()
Hope this is clear now.
Nikos
NguyenTheMy said:
Hello Nikos,
Thank you for your reply, but I'm a beginner in Access, so could you
please tell me more detail. How to declare a public variable, and how to
access a textbox in the main form when the subform close ?