Open a subform by ID

  • Thread starter Thread starter John Harrington
  • Start date Start date
J

John Harrington

If I want to make a button on MyOpenForm that opens a form with a
table record source to a specific ID (e.g., 2) in the table, I write
the following in the buttons click event:

DoCmd.OpenForm "MyForm", , , "ID = 2"

Suppose I have MyForm embedded in MyOtherForm as a subform. How would
I rewrite the above line so that the subform opens to record ID 2?

Thanks,
John
 
You don't open subforms. Subforms are a bit different. What you have is a
form being used as a subform. It is contained in a subform control on the
main form. One of the properties of a subform control is the SourceObject
property. It identifies which form will be presented in the subform control.

So, the answer is you just set the value of the SourceObject property of the
subform control to the name of the subform you want to present. Because of
this, you don't have the Where argument to use to identify the record. That
can be accomplished by navigating the subform's recordset to the desired
record.

It would be something like this untested "air code":

With Me.MySubFormControl
.SourceObject = "MySubFormName"
With .Form.Recordset
.FindFirst "ID = 2"
If Not .NoMatch Then
Me.MySubFormControl.Form.Bookmark = .Bookmark
End If
End With
End With
 
Back
Top