How to set controlsource of a textbox on a subform from the code in its parent form?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HiGu
    New Member
    • Feb 2012
    • 99

    How to set controlsource of a textbox on a subform from the code in its parent form?

    On a subform I have a textbox of which I would need to change control source property on click of a label on the parent form.I do not know how to do this as my trials resulted in errors.I have tried the following with no result:
    Code:
    1. Form_frmWOMAINMENU.[frmWOMAINSUBMENU].Form.[txtDate].Control.ControlSource = "Raised"
    2. Forms("frmWOMAINMENU").[frmWOMAINSUBMENU].Form.[txtDate].ControlSource = "Raised"
    3. Me.[frmWOMAINSUBMENU].Form.[txtDate].ControlSource = "Raised"
    Last edited by NeoPa; Mar 6 '12, 03:19 PM. Reason: Fixed formatting.
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    Ensure, if you have not allready that the FIELD name and the CONTROL name are NOT the same.

    You also must be aware that a subform is contained in a control. Thats why the notation ME.ControlName. Form is used to gain access to the FORM displayed in the control ControlName.
    Access will often name the control the same as the formname, which I usually rename to something like ctrlSubForm_For mname (FormName being the name of my form).

    I do not think taht your example 1. will work, but 2 and 3 should both work, if you have followed the above guides.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      So what errors did you get for each?

      To be able to help you we need more than just a post of the form :
      "I have a problem. What's a solution?"

      To be able to suggest a solution we need to have the problem explained. Details are required. Simply telling us you have a problem is thoroughly inadequate and will generally end up with a useless thread. Useless to you because it's unlikely to receive a helpful answer, and useless to us because it doesn't even present a question we can answer. Lastly, useless to the site because other people searching for a similar answer won't find it here.

      Writing a valid question isn't difficult. It doesn't take a genius. It simply requires you to think a bit before posting.

      Comment

      • HiGu
        New Member
        • Feb 2012
        • 99

        #4
        @TheSmileyCoder : I am a novice in Ms Access,how can I verify that the control name and the name of the form are the same?Is control name the same as the nae of the form in vba which is generally preceded by Form_?
        @NeoPa: the errors were something like "invalid object reference".

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          If you show the Properties pane (See Debugging in VBA) then whenever you select an object while in design-mode looking at your form, the type and name of that object will be shown in the Properties.

          Your original #1 will, indeed, not work. However, the others will work if you have the names right. We can't help you get the names right as we have no access to your database. That said, if you can't manage to work out the names of your objects from what you've been told then I'd be surprised. You have all the info you need. In case you need a reminder - Referring to Items on a Sub-Form.

          Comment

          • HiGu
            New Member
            • Feb 2012
            • 99

            #6
            After many trials,the following has worked for me,
            Code:
            Me.[frmWOMAINSUBMENU]![txtDate].ControlSource = "Raised"
            I guess, removing the word "Form" from code #3 worked.
            Also,I found that the problem with #2 was the name of the parent form also.When I used this
            Code:
            Forms("frmWOMAINMENU").[frmWOMAINSUBMENU].txtDate].ControlSource = "Raised"
            then the error was "cannot find frmWOMAINMENU" which means that vba was trying to find the form frmWOMAINMENU outside the worm in which the code was.This was because I put this code in the on click event of the label of the form frmWOMAINMENU.
            I hope I got the problem right.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Originally posted by HiGu
              HiGu:
              I guess, removing the word "Form" from code #3 worked.
              I doubt it. It wasn't simply removing .Form., but replacing it with !. However, each should work. If the latter version worked then the former should have if tested properly.

              Originally posted by HiGu
              HiGu:
              Code:
              Forms("frmWOMAINMENU").[frmWOMAINSUBMENU].txtDate].ControlSource = "Raised"
              This code is obviously not going to work as it has a close-bracket "]" after txtDate, but no open "[" before.

              As your code with the Me reference illustrates that you're referring to the current form (Critically important information BTW), there is no point in exploring any of the approaches that don't start with Me. They are there to enable the more complicated tasks of referencing objects that are not the parent object of the active module.

              Comment

              Working...