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 Record
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
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.
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
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
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.