How to open a subform with a button

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I like the way my subform links my fields and shows relevant info. But I
would rather use a button because sometimes I do not need to see the subform
and with over one of them on a form, the form gets too busy. When I inserted
a command button to open the subform, it does not however use the
relationship and displays all the fields.
For example I have a subform of "employees" to show "employees clients" The
subform shows only clients belonging to that employee. But when I use a
button I can see all the clients not those of that employee. What can I do?
 
What do you mean y openning a sub form using a button?
Do you open a separate form, or you make a sub form visible on the form?
=======================================
For the First option, you can send a criteria to open the form

Dim MyCrit as String
MyCrit = "[Employee]='" & Me.[Employee text field name in the form] & "'"
Docmd.OpenForm "FormName", , , MyCrit

Note, if you filtering on a number type field, change the where condition to
MyCrit = "[Employee]=" & Me.[Employee text field name in the form]

===========================
For the second option, set the child and parent properties of the sub form
control to link the main form to the sub form
 
Erik,

The linking is controlled by the subform control's LinkMasterFields and
LinkChildFields properties. If you open the subform by itself, it is not
embedded as a subform control and therefore does not have these properties.

To display the filtered records, either 1) use the optional Where clause of
the OpenForm method, or 2) embed the subform normally or on another tabbed
page, and toggle its visibility.

1)

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "YourSubform"

stLinkCriteria = "[LinkingField]=" &
Me![ControlOnMainFormWithMatchingData]
DoCmd.OpenForm stDocName, , , stLinkCriteria

2)

Me!YourSubForm.Visible = True

Hope that helps.
Sprinks
 
Another option is to make the subforms still subforms but of another
simplified main form that is filtered to the record you are sitting on
in the original parent form. Don't even have to have any fields shown,
just have it as the data source.

Ron
 
Back
Top