Find Control without For each loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • morrisp
    New Member
    • Nov 2007
    • 3

    Find Control without For each loop

    Newbi here,
    How do I find a specific form control without using a For Each loop? While the loop works to find a control, it seems to have overhead I'm trying to avoid. I'm using A2K.

    Here's the code I'm trying to avoid. There must be another way to find a control.
    Code:
    Dim ctl as Control
    For each ctl in Me
      do this and that
    Next ctl
    Thanks in advance,
    =Paul
    Last edited by NeoPa; Nov 13 '07, 12:12 AM. Reason: Please use [CODE] tags
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    I'm afraid, Paul, that you're going to have to give a better explanation of what you're trying to do in order for us to help you. The code you've cited is generally used to find a group of controls of the same type in order to carry out some action rather than to find a specific control.

    Welcome to TheScripts!

    Linq ;0)>

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Originally posted by morrisp
      Newbi here,
      How do I find a specific form control without using a For Each loop? While the loop works to find a control, it seems to have overhead I'm trying to avoid. I'm using A2K.

      Here's the code I'm trying to avoid. There must be another way to find a control.

      Dim ctl as Control
      For each ctl in Me
      do this and that
      Next ctl

      Thanks in advance,
      =Paul
      Your other option is a For ...Next construct, but I'm not so sure that you will gain any benefits by using it:
      [CODE=vb]
      Dim intNoOfControls As Integer, intCounter As Integer

      intNoOfControls = Me.Controls.Cou nt

      For intCounter = 0 To intNoOfControls - 1
      Debug.Print Me.Controls(int Counter).Name
      Next[/CODE]

      Comment

      • morrisp
        New Member
        • Nov 2007
        • 3

        #4
        Originally posted by missinglinq
        I'm afraid, Paul, that you're going to have to give a better explanation of what you're trying to do in order for us to help you. The code you've cited is generally used to find a group of controls of the same type in order to carry out some action rather than to find a specific control.

        Welcome to TheScripts!

        Linq ;0)>

        To be more specific .... I want to change the value in a Label's caption property when a scenario occurs to the attached 'Text Box' control. How can I do this without using a "For Each Loop" to find the label control? There is an obvious link between Label and Text Box controls (the two are created together). But, how to take advantage of the link in code is escaping me.

        =Paul

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Originally posted by morrisp
          To be more specific .... I want to change the value in a Label's caption property when a scenario occurs to the attached 'Text Box' control. How can I do this without using a "For Each Loop" to find the label control? There is an obvious link between Label and Text Box controls (the two are created together). But, how to take advantage of the link in code is escaping me.

          =Paul
          There is a Linkage as you surmise and it is a Parent/Child Linkage. The Parent Property of a Label Control returns the Control the Label is Linked to. A simple example will illustrate this point. Assume you have a Last Name Text Box on a Form called txtLastName, and its associated Label is named lblLastName:
          [CODE=vb]
          Debug.Print Me![lblLastName].Parent 'returns the Value in txtLastName Field
          Debug.Print Me![lblLastName].Parent.Name 'returns the Name of the associated Text Box
          'Control, namely txtLastName
          [/CODE]

          Comment

          • morrisp
            New Member
            • Nov 2007
            • 3

            #6
            aaahhhh ..... very close. But, I notice that 'Parent' is valid option after the control, but 'Child' is not. Does this mean it's a one way link and I cannot go from 'Text Box' to 'Label' control?

            =Paul

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              Originally posted by morrisp
              aaahhhh ..... very close. But, I notice that 'Parent' is valid option after the control, but 'Child' is not. Does this mean it's a one way link and I cannot go from 'Text Box' to 'Label' control?

              =Paul
              To the best of my knowledge, it is a one way Link as you so indicate. There is no associated Child Property for the Parent.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32668

                #8
                Look in the Controls item of the control you're interested in. There should only be the one (#0) for simple controls associated with a label.
                I tried it with a ComboBox called cboResults. The associated label is called Label3.
                Code:
                Debug.Print "Combo - " & Me.cboResults.Controls(0).Name
                Debug.Print "Label - " & Me.Label3.Parent.Name
                produced :
                Code:
                Combo - Label3
                Label - cboResults

                Comment

                Working...