View Full Version : Reference a SubForm Control on a Second From the First Form


Steve R.
10-27-2011, 06:06 AM
Recently, I had a situation (http://www.access-programmers.co.uk/forums/showthread.php?t=216934) where I needed to have TWO forms open at once. One Form (Form "A") had a subform in datasheet view which contained a list of all projects. What I wanted to do was to double click on a particular project, have the focus changed from Form "A" to Form "B", and have Form "B" display that project. The problem faced, was how to pass the project number from Form "A" to Form "B".

The basic structure in Form "B" to reference a value in a control on subform of Form "A" is below:
Forms(csCriteriaForm).Controls("child1").Controls("projectnum01").ValueThanks to spikepl and vbaInet for their guidance.

----------------------------------------------------
If you wish to refer to a control on the main form of Form "A" from Form "B" you would use the syntax below:Forms("FormName").Controls("TexboxName")If you wish to refer to controls within the same form please click on the article following. Refer to Form and Subform properties and controls (http://access.mvps.org/access/forms/frm0031.htm)

----------------------------------------------------
Sample code (in the subform) for Form "A":Private Sub ProjectNum01_DblClick(Cancel As Integer)
Forms(csEditForm).SetFocus
End SubSample code for Form "B" to display the selected record: If CurrentProject.AllForms(csCriteriaForm).IsLoaded Then
strLinkCriteriaPVT = "ProjectNum = " & Forms(csCriteriaForm).Controls("child1").Controls("projectnum01").Value
DoCmd.SearchForRecord , , , strLinkCriteriaPVT
End If