tags:

views:

1326

answers:

3

I've got a form and a button on it. I would like to open another form on a button click with a parameter passed from the parent form to the child form (child form's RecordSource has parameters). How can I do it?

A: 

If the parent form is open, you can do the following

Form!MyParentForm!myPublicVariableOfParentForm

There could be other ways, whereby you can call a method on the child form with a parameter from the parent form.

shahkalpesh
+1  A: 

You can use "OpenArgs". When calling a form just pass your OpenArgs string and on the child form read it and parse. It's up to you to prepare custom solution to build an parse more sophisticated "OpenArgs" strings. Refer: http://msdn.microsoft.com/en-us/library/bb215912.aspx

juckobee
+2  A: 

You can refer to any property of the calling form by reference to the form's object. I will not use the ! notation for form's properties (I hate it). so if you want to inherit:

a form's control value:

forms(parentFormName).controls(controlName).value

a form's recordset field value:

forms(parentFormName).recordset.fields(fieldName).value

The recordset turnaround is particularly useful when accessing id's (guid) values. You cannot read these values through form's controls, but you can access them throught the corresponding field in the recordset. If you have a combobox on a form containing a guid field, please compare the following:

 screen.activeForm.controls(myComboControlName).value

and

 screen.activeForm.recordset.fields(myComboControlName).value
Philippe Grondier
Inside Forms(), Controls() and Fields() your answer is written as though what you've supplied are the names of the forms/controls/fields, but your code treats them as variables. If they are string values, i.e., your form is named "parentFormName", then you need the quotes.
David-W-Fenton
Forms have a Fields collection, and before Access 2000, it was quite reliable (which was a good thing, since the form didn't have a Recordset property). Since then, to work with values from the underlying recordset you have to use the form's Recordset (as you do, but I find it as distasteful to do so as you find using the !, which I think is vastly superior to the . operator since it distinguishes controls from properties/methods/members of the form; but I digress), or for reliability, have a hidden control with the field as its ControlSource.
David-W-Fenton
The .Value is unnecessary, as it's the default property. The only case where you might specify it is if you're passing a control value to a subroutine/function that passes the parameter ByRef instead of ByVal. Using .Value forces ByVal even if the parameter is defined as ByRef. But, of course, if the sub/function assumes it's getting a control reference, that can break it.
David-W-Fenton
Forms("parentFormName").Controls("controlName").Value is the same as: Forms("parentFormName")("controlName") -- likewise, Forms("parentFormName").Recordset.Fields("fieldName").Value is the same as Forms("parentFormName").Recordset("fieldName") .
David-W-Fenton
Well, I guess the main advantage of my 'complete' notation is to set a rule so that everybody knows exactly what he's writing even if it requires some extra typing skills from the code writers. And the underlying idea is very clear: there is a form object. This object has collections (such as controls) and properties (such as recordset) which themselves can have collections (fields). Standard, clear and universally understood, so that even guys that have never heard about Access easily understand what we are talking about.
Philippe Grondier
@David W. Fenton: I believe your assumption about forcing ByVal is incorrect (unless you are alluding to a more complex scenario than the one you actually describe).
onedaywhen
@Philippe Grondier: Your notation isn't 'complete': you've omitted a couple of default properties named Item i.e. should be Forms.Item("parentFormName").Controls.Item("controlName").Value.
onedaywhen