how to call subs on another form in the same tab control?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bbacher
    New Member
    • Jan 2012
    • 5

    #1

    how to call subs on another form in the same tab control?

    I have a form 'frmHome'. It has a tab control 'tabCtrl'. The tab control has two forms: 'frmContacts' and 'frmProjects'.

    On the contacts form I can see the projects a person is involved with. On the Projects form I can see the people associated with a project.

    I want to dbl-click on a project from the contacts form, and have it take me to that project on the projects form. I want to similarly click a person on the projects form, and have it take me to that person on the contacts form.

    So far, I can dbl-click on a project in the contacts form, and the code will change the tab control to the appropriate page with the projects form. Also, there is a public sub on the projects page which will set the current record to the value of a global variable. I tested that sub with a command button and it works great.

    BUT - I can't figure out how to call that public sub from the other form. I can't figure out the proper syntax to reference it.

    Help? Thanks.
  • Mihail
    Contributor
    • Apr 2011
    • 759

    #2
    You lost me .

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      You should find enough reference material in Referring to Items on a Sub-Form to help with this. If not then try to explain what you need further in clear terms.

      Comment

      • Stewart Ross
        Recognized Expert Moderator Specialist
        • Feb 2008
        • 2545

        #4
        From VBA code you can refer to a public sub or function in another open form (frmProjects in this case)as follows:

        Code:
        Form_frmProjects.YourSubName

        More generally, to assign the value of a function on another form to a variable, and to call a sub:

        Code:
        SomeVar = Form_YourFormName.YourFunctionName([Arg1], [Arg2], ..., [ArgN])
        Form_YourFormName.YourSubName [Arg1], [Arg2], ..., [ArgN]
        This will only work for public subs or functions; those declared as private are not accessible outside the scope of their own form. After you type the dot separator Intellisense will show you the properties and methods available to you. Your public sub or function should show on the Intellisense list if all is well.

        -Stewart
        Last edited by Stewart Ross; Jan 30 '12, 10:28 AM.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32669

          #5
          While all that Stewart says is correct, if you want to refer to items within a closed list such as this, and I'm assuming you want to refer to frmProjects from withing frmContacts, you have another option, which is to refer to it relationally.

          I don't actually know the names of your subform controls on your main form, but if they were called sfmContacts and sfmProjects then referring to a procedure MyProcedure found in frmProjects from code in frmContacts could be handled by saying :
          Code:
          Me.Parent.sfmProjects.Form.MyProcedure
          or even :
          Code:
          Me.Parent.sfmProjects!MyProcedure

          Comment

          • bbacher
            New Member
            • Jan 2012
            • 5

            #6
            Thanks, everyone. Stewart's answer did the trick: Form_frmProject s.PublicSubName

            NeoPa, I couldn't get your syntax to work - that's similar to the other things I tried before coming here (which also didn't work). I've done that sort of thing many times before, but never when the forms are on a tab control. I think the tab control confuses the syntax somehow. In the past I've emulated a tab control just by having buttons on the forms - and that seemed easier in many ways than the tab control is turning out to be.

            At any rate, thanks for the help.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32669

              #7
              Tab controls don't actually effect the referencing at all. They confuse mostly by people thinking they need to include it as some part of the reference.

              Anyway, I'm sure you're happy with Stewart's solution - and it's something that's very useful to understand anyway (The full name of the object modules themselves).

              Comment

              • bbacher
                New Member
                • Jan 2012
                • 5

                #8
                So, from a syntax standpoint, I should just consider the two tabbed forms as if they were subforms on the main form?

                Code:
                Instead of this:
                Main form
                  |-- tab ctrl
                        |--- form1
                        |--- form2
                
                
                I should think this?
                Main form
                  |--- subform1
                  |--- subform2

                Comment

                • Stewart Ross
                  Recognized Expert Moderator Specialist
                  • Feb 2008
                  • 2545

                  #9
                  Forms on a tab control are subforms of the main form itself - the tab control does not affect their referencing at all, as NeoPa said. This is as you have noted in the second part of your post above.

                  -Stewart

                  Comment

                  • bbacher
                    New Member
                    • Jan 2012
                    • 5

                    #10
                    Thank you both. That clarifies a lot.

                    One more question: When the main form is open, are the subforms (the forms on the tab control) also considered 'open'?

                    One of the options I tried early on gave me an error saying that I couldn't reference controls on the projects form unless it was open.

                    Comment

                    • Stewart Ross
                      Recognized Expert Moderator Specialist
                      • Feb 2008
                      • 2545

                      #11
                      The subforms are indeed open - but they are not open in the same way they would be if they were opened on their own outside of the main form.

                      When you open a main form, the subforms are forms embedded as controls within the main form itself. If you were to check that, say, your form called frmProjects is loaded you will find that it is not. If you attempt to access its controls as if it is a run-time error will occur.

                      To access the controls within one of your subforms when the main form is open you have to refer to the subform control's form property, as detailed in the article hyperlinked by NeoPa in post #3. There are many (equivalent) ways to do so, so this example is only one of the possibilities:

                      Code:
                      Forms("frmHome")!frmProjects![Your control name]
                      -Stewart
                      Last edited by Stewart Ross; Jan 31 '12, 01:55 PM.

                      Comment

                      • bbacher
                        New Member
                        • Jan 2012
                        • 5

                        #12
                        Now I get it! NeoPa's linked article confused me before because I wasn't thinking of the forms on the tab control as subforms. Now that I re-read the article it makes a lot more sense.

                        Thanks again.

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32669

                          #13
                          Everything Stewart said :-)

                          I'm pleased to hear things make more sense now. I never felt it was necessary before, as logically they have so little effect, but I may revisit the article and explain explicitly that tab controls don't effect the referencing of items 'contained' within them at all.

                          PS. I've updated it now. I must admit though, I found the explanation of how sub-forms are handled (within Subform controls) already clearly stated in the introduction. No updates were required for that part at least.
                          Last edited by NeoPa; Jan 31 '12, 04:33 PM. Reason: Added PS

                          Comment

                          Working...