How do I set focus from one subform to another?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #1

    How do I set focus from one subform to another?

    I'm trying to set the focus from one subform to a control on another subform that is on the same parent form. Currently I have:

    Code:
    Forms!frmCustomersfrmFileTypes!FileName.SetFocus
    but it doesn't work. I'm guessing that is because I'm starting from another subform because I've used this before to go from the parent form to the subform and it worked. I thought I could set the focus to the parent form and then use the above code but that didn't work either.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    You don't mention pages or tabs, but I don't believe the answer changes even if you do.

    Assuming a form with two subforms then, and the code running in the first subform you might try something like :

    Code:
    Private Sub cmdSelectOther()
        Call Me.Parent.[OtherSubformControl]![ControlOnOtherSubform].SetFocus
    End Sub

    Comment

    • Seth Schrock
      Recognized Expert Specialist
      • Dec 2010
      • 2965

      #3
      Both subforms are on tabs, but they are on the same tab. However, like you said, I don't think that that should change anything. I should have mentioned this is my OP.

      I will give that a try on Monday when I get back to work.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32669

        #4
        Cool. Let us know how it goes. I don't have any to test on so I'm posting from theory ;-)

        Comment

        • Seth Schrock
          Recognized Expert Specialist
          • Dec 2010
          • 2965

          #5
          Okay, I just tried it and get an error message saying: Compile error: Expected:. or ( and then it highlights the !. I've never used the Call me.Parent command, so I have no idea how to procede.

          Comment

          • Seth Schrock
            Recognized Expert Specialist
            • Dec 2010
            • 2965

            #6
            I'm beginning to think that I might know why it isn't working, although I still don't know how to fix it. My code is set within an OnChange event which checks to see if a control on the other subform is null, and if true (it is null) then it produces an error message and then sets the focus to that null control. However, because it is in an OnChange event, it make the record dirty (changed). Could this be stopping the focus from being able to be transferred? I didn't get any error messages from my original code, it just wouldn't change the focus over to the other control.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32669

              #7
              It's hard to say with just a loose description of the code instead of the code itself Seth. The code could be almost anything which is a little hard to work from.

              Comment

              • Seth Schrock
                Recognized Expert Specialist
                • Dec 2010
                • 2965

                #8
                I will post the code tomorrow when I get to work. I should have posted it in my last post.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32669

                  #9
                  Originally posted by Seth
                  Seth:
                  I should have posted it in my last post.
                  I won't argue with that, but I can say that it's pleasant working with someone who doesn't need everything hammered home.

                  You'll do fine, and the code tomorrow will also do nicely :-)

                  Comment

                  • Seth Schrock
                    Recognized Expert Specialist
                    • Dec 2010
                    • 2965

                    #10
                    Here is the code.
                    Code:
                    Private Sub Email_Change()
                    If IsNull(Forms!frmCustomer!sfrmFileTypes!FileName) Then
                        MsgBox ("Please select a file name before entering an email address")
                        Call Me.Parent.[sfrmFileTypes]![FileName].SetFocus
                    End If
                    End Sub

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32669

                      #11
                      I'm not sure I know the answer to this off the top of my head Seth (and I don't have anything to play with and test).

                      I can't promise anything, but if you attach your sanitised database (Attach Database (or other work)) I'll look at it for you and see if I can determine what's going wrong.

                      Comment

                      • sierra7
                        Recognized Expert Contributor
                        • Sep 2007
                        • 446

                        #12
                        Hi
                        I think you have to do this in stages. First move the focus up to the Parent, then to the sub-form control, then finally to the required fieldname.
                        Code:
                        Me.Parent.SetFocus
                        Me![SubformName].SetFocus
                        Me![SubformName].Form![FieldName].SetFocus
                        A bit tedious but something similar happens when you are dealing with multiple subforms in Design mode, you have to click on the parent container before you can select an element within it.
                        S7

                        Comment

                        • Seth Schrock
                          Recognized Expert Specialist
                          • Dec 2010
                          • 2965

                          #13
                          @sierra7 I just tried your code and it came back with a "{database name} can't find the field 'sfrmFileTypes' referred to in your expression." error message in the second line of code. I tried putting
                          Code:
                          Me!frmCustomer.Form!sfrmFileTypes.SetFocus
                          but all that did was make the error say "can't find field 'frmCustomer'".

                          @NeoPa I will try to get you the sample database here shortly. I'll include some sample data as the real data is confidential.

                          Comment

                          • Seth Schrock
                            Recognized Expert Specialist
                            • Dec 2010
                            • 2965

                            #14
                            Okay... Here is the sample database in .mdb (not .accdb). I tested it after creating the new sample and the problem still exists. You will notice empty combo boxes and tabs, but don't worry about them. I just didn't include the extra forms and tables.
                            Attached Files

                            Comment

                            • NeoPa
                              Recognized Expert Moderator MVP
                              • Oct 2006
                              • 32669

                              #15
                              What do I do to get your code to attempt to execute, and where exactly is this code found?

                              Probably a dumb question, but it's late and I'm tired ;-D

                              Comment

                              Working...