Link to home
Start Free TrialLog in
Avatar of cwbarrett
cwbarrett

asked on

subform sourceobject

Hi,

I am attempting to open a form then change the sourceobject of the subform.  Here is my code:

Private Sub btnEditRpt_Click()
Dim formname As String
Dim formfilter As String
    formname = [Forms]![frmReport]![frmReportReportList_Sub].[Form]![TemplateName]
    formfilter = "[autoReportsAll] = " & [autoReportsAll]
    DoCmd.OpenForm formname, , , [formfilter]
'
    Forms![xtemplate1]![MainReportForm].SourceObject = [ReportName]
End Sub

The form opens and filters fine, however, in the line that changes the SourceObject I would like to substitute "xtemplate1" (which is an actual form name) with a form name residing in a form field called "TemplateName". (as I did to open the form) .

Notes:  
I have two fields in the form/subform where this code executes from a button:  TemplateName and ReportName.

MainReportForm is the name of the subform object.

Help is appreciated.
Charlie
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

if the name of the form you want to use is as source object is ReportName

Forms![xtemplate1]![MainReportForm].SourceObject = "ReportName"


maybe this is wht you are looking for

Forms(formName)("MainReportForm").SourceObject = "ReportName"
Try this

me![MainReportForm].SourceObject = [TemplateName]
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cwbarrett
cwbarrett

ASKER

Tried a few different things.  Still can't do it.  Here is the modified code.  The form opens but getting an error "cannot find the referenced form 'formTemplateName'" and debugging shows the last line as the culprit.  What am I doing wrong?

Private Sub btnEditRpt_Click()
Dim formTemplateName As String
Dim formfilter As String
Dim subformSourceObject As String

    formTemplateName = [Forms]![frmReport]![frmReportReportList_Sub].[Form]![TemplateName]
    formfilter = "[autoReportsAll] = " & [autoReportsAll]
    subformSourceObject = [Forms]![frmReport]![frmReportReportList_Sub].[Form]![ReportName]
   
    DoCmd.OpenForm formTemplateName, , , [formfilter]

    Forms![formTemplateName]![MainReportForm].SourceObject = [subformSourceObject]
   
End Sub
Why does formTemplateName work to open the form (DoCmd.OpenForm formTemplateName, , , [formfilter])  but doesn't work when referencing it to set the SourceObject?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.  You both were a great help.