简体   繁体   中英

How to navigate from a form to a subform in a different form?

I'm working on a Access-Database that is split into multiple parts. Each has a Main-Form with Subforms in nested Tabs.

Walkthrough

We have a starting-Area that is separated into different Themes. Those theme-forms have buttons that represent Subthemes.

主题2的起始表格

When I hit Button A i want to navigate from Mainform 1 to Mainform 2 Subtheme A (easy as it is the default).

When I hit Button BI want to navigate from Mainform 1 to Mainform 2 Subtheme B. (I can't get this to work)

主表单2-SubTheme2默认在子表单A上打开

What i know

I can easily get from Mainform 1 to Mainform 2 landing per default on Theme 1

Private Sub buttonB_Click()
   DoCmd.openForm "Mainform2", _
End Sub

I don't know how to navigate to Mainform2->Sub Theme B. I can open the right subform using the OpenArgs but i cant do it with the Tabs. I tried to use the DoCmd.browseTo in onLoad() of mainform2 but that breaks the process.

If i am not mistaken there should be a way to use DoCmd.browseTo in the buttonB_click() but i cant get the path right.

DoCmd.OpenForm Method (Access)

DoCmd.BrowseTo Method (Access)

I tried to describe the problem as general as possible so answers can be helpful for others as well. I hope you can help me!

So I managed to resolve it:

First we navigate to the Main-Form of Theme2 and tell it which topic we want with the help of OpenArgs:

Private Sub buttonThemeB_Click()
   DoCmd.openForm "Mainform2", _
   OpenArgs:="Theme B"
End Sub

In the FormLoad() of Mainform2 we check the OpenArgs and navigate the Subform-Controle:

Private Sub Form_Load()

   Dim strSubFormToken As String

   ' If OpenArgs property contains a subform name, open corresponding subform  
  If Me.OpenArgs <> 0 Then strSubFormToken = Me.OpenArgs

  Select Case strSubFormToken
     Case "Theme A"
        strSubForm = "form_themeA"
     Case "Theme B"
        strSubForm = "form_themeB"
  End Select

  strSubForm =     
  If Len(strSubForm) > 0 Then
     DoCmd.BrowseTo acBrowseToForm, strSubForm, "frmTheme2.Navigationsunterformular" 'Your Navigationtarget
  End If

End Sub

The Mainproblem in all this is that if you use the DoCmd.BrowseTo Path to Subform Control you have to know some rules that aren't documented:

the “>” character seems unusual, that's because it has been introduced specifically for the Path parameter of BrowseTo. Its meaning is this: The form to the right of the “>” character is loaded into the subform control specified to its left.

  • The path must specify a series of Form.SubFormControl pairs. Each subform control specified must be a control in the form that precedes it in the path.
  • The pairs must be separated by the “>” character. and most important:
  • The first form in the path must be the form that is currently loaded directly in the Access window (or browser window if the application is running on the web.) This means that the Path parameter only allows you to change the contents of a currently-visible subform control.

So we have to first open the new main-form before navigating the navigation-subcontrol.

Seems to be the best way to handle this. Thanks for the suggestions!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM