Summary. If a mainform has a subform with a recordsource set to a passthru, the subform will not open... So, before the main form is opened (the on_click that opens the form before the docmd.Open main_form, I run the initialize subform routine to set the recordsource of the subforms to a dummy recordset. Then, on open of the main for, the subform's Form_Open event, resets the recordsource to the passthru. Solved.
Code:
Public Sub Initialize_Sub_Form_Recordsources_For_Passthru_Change_Later()
Dim form_name As String
'Global variable set in code to control recordsources of subforms.
'Global Const USE_PASS_THRU_AS_SOURCE = True ' this variable set if forms are pass thru queries, non-direct edit. SQL Code to edit records
'Global Const USE_PASS_THRU_AS_SOURCE = False ' this variable set if forms are direct edit (dao queries)
'set recordsource for all subforms, before the main form opens, to a dummy non-passthru recordset.
'Must be done before the Main Form On_Open event. Subforms will not open from main form is subform is set to a passthru.
'Change the subform's recordsource, to what you want, pass or not, in the subform's form_open event
Status_Message_Open True, "Local: Initializing Subforms"
form_name = "ews 51 4b) Sub Form - Work Elements"
DoCmd.OpenForm form_name, acDesign, , , , acHidden
Forms(form_name).RecordSource = "ews 1) Dummy Table - Initial Recordsource for Subforms"
DoCmd.Close acForm, form_name, acSaveYes
'pass thru query: ews 2 b2) Work Elements LZ Project Current Gate XX - sql server procedure
'non pass thru query: ews 51 3b) Work Elements - Global Project - Current - dao query using linked sql server ables
form_name = "ews 54 2b) Action - Update sub main"
DoCmd.OpenForm form_name, acDesign, , , , acHidden
Forms(form_name).RecordSource = "ews 1) Dummy Table - Initial Recordsource for Subforms"
DoCmd.Close acForm, form_name, acSaveYes
'pass thru query: not created yet
'non pass thru query: ews 54 2b) Action - Update sub main - qry
form_name = "ews 55 2b) Update - Update sub main"
DoCmd.OpenForm form_name, acDesign, , , , acHidden
Forms(form_name).RecordSource = "ews 1) Dummy Table - Initial Recordsource for Subforms"
DoCmd.Close acForm, form_name, acSaveYes
'pass thru query: not created yet
'non pass thru query: ews 53 3b) Action Updates - Global Project - Current
Status_Message_Open False
End Sub
subform's Form_Open Event:
Code:
Private Sub Form_Open(Cancel As Integer) ' subform of Main form
' reset recordset to passthru or not based on GLOBAL variable USE_PASS_THRU_AS_SOURCE. Before Main form is open, this form's
' recordset was set to a dummy recordsource.
Set EWS_MAIN_ELEMENT_FORM = Me ' set form to global variable of type form... this makes it easier to reference later. Like EWS_MAIN_ELEMENT_FORM.requery
If USE_PASS_THRU_AS_SOURCE Then ' Global variable set (either by code or by cmdbtn click)
EWS_MAIN_ELEMENT_FORM.RecordSource = "ews 2 b2) Work Elements LZ Project Current Gate XX"
Else
EWS_MAIN_ELEMENT_FORM.RecordSource = "ews 51 3b) Work Elements - Global Project - Current"
End If
End Sub