How to Refer to Navigation Subforms and Their Associated Controls

Bilbo_Baggins_Esq

Registered User.
Local time
Today, 01:09
Joined
Jul 5, 2007
Messages
586
Access 2013 x86 running in Windows 10.

I'm working on an unbound form named, "Front"
The Front form contains a Navigation Control with 4 Navigation buttons.
Welcome (click activates the unbound "Welcome" Navigation Subform)
Import (click activates the unbound "ImportData" Navigation Subform)
Export (click activates the unbound "ExportData" Navigation Subform)
Exit

On the "Welcome" and "ImportData" navigation subforms, there are various controls (buttons, text boxes, checkboxes, etc) which I need to pre-populate with values I pull from the database.

Opening each of the navigation subforms directly (without going through the Front form) I wrote code (in a proper module) with which I'm able to successfully accomplish my goals.
However, I cannot for the life of me figure out how to refer to the navigation subform (or controls) when I drop my previously working sub into an event driven action.
This, even when I try to just call the sub in the proper module.

I'm about to shoot this thing.

I've searched a lot of resources and tried various formats of syntax and nothing seems to work.
Sometimes I get the error Access can't find the form, and sometimes I get the error that Access can't find the field (WTH).

Here's the code that actually works when I have the ImportData subform open directly

(the objects you see me setting are declared as public elsewhere)
Code:
Sub CheckPath()


Set obj_WS = DBEngine.Workspaces(0)
Set obj_DB = obj_WS.Databases(0)


If IsNull(GetParameter("Raw_Data_Root_Path", 2)) Then
Forms("ImportData").Controls("Button_Import").Enabled = False
Forms("ImportData").Controls("Button_Import").Caption = "Set Raw Data Root Folder First"
Forms("ImportData").Controls("Text_RootPath").Value = ""
Else
Forms("ImportData").Controls("Button_Import").Enabled = True
Forms("ImportData").Controls("Button_Import").Caption = "Import Data"
Forms("ImportData").Controls("Text_RootPath").Value = GetParameter("Raw_Data_Root_Path", 2)
End If


Set rs_ReportsToRun = obj_DB.OpenRecordset("ReportsToRun", dbOpenDynaset)
Do While Not rs_ReportsToRun.EOF
Select Case rs_ReportsToRun.Fields(2)
Case Is = "MISBC"
Forms("ImportData").Controls("Check_MISBC").Value = rs_ReportsToRun.Fields(3)
Case Is = "SBHD"
Forms("ImportData").Controls("Check_SBHD").Value = rs_ReportsToRun.Fields(3)
Case Is = "SBOT"
Forms("ImportData").Controls("Check_SBOT").Value = rs_ReportsToRun.Fields(3)
End Select
rs_ReportsToRun.MoveNext
Loop

rs_ReportsToRun.Close
Set rs_ReportsToRun = Nothing
obj_DB.Close
Set obj_DB = obj_WS.Databases(0)
obj_WS.Close
Set obj_WS = DBEngine.Workspaces(0)

End Sub

PLEASE HELP BEFORE I RUN OUT OF HAIR TO PULL OUT!!!
 
Now that form is used as a subform, form reference will no longer work because form is not opened as an independent form. Use Me alias and it won't matter how form is used.

Code behind subform, simplified without CASE structure:

Me.Controls("Check_" & rs_ReportsToRun(2)) = rs_ReportsToRun(3)

Are you populating UNBOUND controls?
 
Last edited:
Now that form is used as a subform, form reference will no longer work because form is not opened as an independent form. Use Me alias and it won't matter how form is used.

Code behind subform:

Me.Check_MISBC =

Are you populating UNBOUND controls?

All the controls are unbound.

In your above example, is the context of Me. as the form "Front?"

The form "ImportData" is a navigation subform of the control "NavigationControl0" on the main form "Front".

I've been able to successfully trigger the OnLoad Event when the "ImportData" button on the NavigationControl0 is clicked.
But even if I place my code there, it fails...
 
The context of Me is the form or report code is behind, regardless if used as subform/subreport.

Revised other post, review again.
 
only 1 subform is Loaded at any given time on the Navigation Form.
if "front" form is the first tab, it is the Only subform available at the
moment so you cannot set the controls of the other subforms.
you use a "regular" tab control so all subforms are loaded when the
main form opens.
 
arnel, OP states "Front" is unbound main form that navigation control sits on. Code is behind each form used as subform.
 
so "Front" is the Navigation Form, and the first subform that will get focus is "Welcome".
the other's will not be Loaded until you click on the Tab/Page.
still, only 1 subform will be visible/active at that time.
 
OP, I understand the frustration of having scarce documentation. Hey, that rhymes.
Anyway, I'll start with some facts about what you want to do:
1. You can only reference a form and its stuff when it's loaded.
2. Navigation controls are a NavigationControl and a NavigationSubform control grouped together, the buttons bar is the NavigationControl and the form container is the NavigationSubform.
3. When you click a button from the NavigationControl, only the form associated to that button is loaded inside the NavigationSubform control, the others will not be available, because they're not loaded. You close one to open the other. Only one form at a time.

So how to approach this? Easy, add a Stop wherever you want to get a reference. You want your reference in your CheckPath subroutine? then, if your subroutine is inside a form, just:
Code:
Sub CheckPath()
    'Your code
    Stop
End Sub

Now make sure you have the locals window open in your VBA editor. If that code was in a form, the Me keyword will appear there. Click the plus sign and browse to the control you need to reference, it depends on how your project is setup.

Is your subroutine inside a module, not a form? then:
Code:
Sub CheckPath()
    Dim theForm as Form
    Set theForm = Forms("FormName")
    'Your code
    Stop
End Sub
In your case, the form you should target is "Front", because subforms controls are not forms, they are controls that hold forms. Don't try to access a form alone when it's part of another form.

With that, theForm variable will appear in the locals window and you will be able to browse to the control you need. I recommend being explicit and not using shortcuts like the bang notation or using the default member of the class as those two can be confusing and the information available is either wrong or incomplete. What I mean is instead of Me!Ctrl, use Me.Controls.Item("ItemName") or Me.Controls.Item(N), where N is the Item number, easily visible browsing the variable in the locals window. By the way, Forms is a member of Application, so I would go as far as say you should use Application.Forms.Item("FormName"), instead of just Forms("FormName").

Browsing your variable is the source of truth. You can find some neat tricks browsing it, like being able to reference a control from any property, or build giant nested dot notations combining bang notations that point to the same thing just for your own amusement. The bang notation is easy to understand if you do it this way.

I forgot to add, don't confuse your NavigationControl with your NavigationSubform, if you want a reference to a form inside the NavigationSubform, use something along the lines of Application.Forms.Item("Front").Form.Controls.Item("NavigationSubform").Form.Controls.Item("ItemName").
 
Last edited:

Users who are viewing this thread

Back
Top Bottom