Source Object

L

Little Penny

Can I set the Properties of a subforms Source Object from a cmd button in another form that opens the
Main form of the subform.
 
K

Ken Snell \(MVP\)

Because of timing issues, it's usually best to set the SourceObject of a
subform via programming in the main form that contains the subform. Pass the
name of the SourceObject to the main form via the OpenArgs argument of
DoCmd.OpenForm action, and then use that in the form's Load event to set the
subform's SourceObject.

DoCmd.OpenForm "MainForm", OpenArgs:="SourceObjectName"


And in the opened form's Load event:

Me.SubformName.SourceObject = Me.OpenArgs
 
L

Little Penny

Ken you are the man.

Can I use more the one OpenArgs (DoCmd.OpenForm "MainForm", OpenArgs:="SourceObjectName")

I also want to set the Label of the subform and the Caption of the form as well


Thanks
 
K

Ken Snell \(MVP\)

Concatenate all the text values into one string, but put a delimiter between
them. I usually use the pipe (|) character.

Then you can parse out the OpenArgs string into the separate names in the
opened form.

Here's some sample code:

(in the calling form)
Dim strOpenArgs As String
strOpenArgs = "SourceObjectName" & "|" & "LabelText" & _
"|" & "CaptionText"
DoCmd.OpenForm "MainForm", OpenArgs:=strOpenArgs


(in the main form that is called)
Dim varOpenArgs As Variant
varOpenArgs = Split(Me.OpenArgs, "|")
' varOpenArgs(0) contains the "SourceObjectName"
' varOpenArgs(1) contains the "LabelText"
' varOpenArgs(2) contains the "CaptionText"
 
Top