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?
4 Answers
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

- 10,900
- 3
- 33
- 72
-
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 Sep 12 '09 at 02:10
-
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 Sep 12 '09 at 02:13
-
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 Sep 12 '09 at 02:14
-
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 Sep 12 '09 at 02:16
-
1Well, 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 Sep 12 '09 at 06:25
-
@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 Sep 14 '09 at 13:16
-
@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 Sep 14 '09 at 13:22
If the parent form is open, you can do the following
Forms!MyParentForm!myPublicVariableOfParentForm
There could be other ways, whereby you can call a method on the child form with a parameter from the parent form. Remember the control will likely need focus before being manipulated.

- 1
- 1

- 33,172
- 3
- 63
- 88
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

- 1,239
- 1
- 12
- 20
In reference to David's comment above:
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...
ByRef/ByVal::This is what worked for me. I have a form that contains two calendar objects, and wanted the values for each object be referenced from another modules sub. Didn't seem to matter if I used ByRef or ByVal. The example below I used ByRef.
public sub nameofsub(byref calendarfromdate as string, byref calendartodate as string)
docmd.runsql("select fieldname from table where fieldname between #" & calendarfromdate & "# and #" & calendartodate & "#")
end sub
From the opened form, I had a button that would call this sub when click and perform my needed code.
private sub btn_callqry_click()
call nameofsub(calendarfromdate,calendartodate)
end sub
No need to declare the values publicly for the calendarfromdate and calendartodate in the beginning of the opened form's class such as:
public calendarfromdate as string
public calendartodate as string
NOTE: If using the above example, the strings must be formatted as mm/dd/yyyy
NOTE: If your form contains a calendar control object as found when using Access in Windows XP, the calendar object doesn't exist on Windows 7, best option is to just take out the calendar object and put in two text boxes labeled as FromDate and ToDate and change their properties to format text as a "Date". Doing this will bring up a date selector next to the box automatically upon entering them.

- 75
- 5