Access an attached label in VBA

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • troy_lee@comcast.net

    Access an attached label in VBA

    I have a sub with this code:

    Sub ColorLabel()
    Dim ctlCurrentContr ol As Control
    Dim lngYellow As Long

    lngYellow = RGB(255, 255, 0)

    Set ctlCurrentContr ol = Screen.ActiveCo ntrol
    With ctlCurrentContr ol.???
    .BackColor = lngYellow
    End With
    End Sub

    I want to run this code On Enter of a text box. Basically, I want to
    color the label of the active text box. I am calling the procedure on
    enter and will call another sub to restore the default formatting of
    the label once the text box loses focus.

    I have found how to reference a text box using a label's parent
    property but I want to reference the label attached to the active text
    box but don't know which property to reference (thus the question
    marks above). Can anyone tell me how this is done or if it's even
    possible?

    Thanks in advance.

    Troy Lee
  • MGFoster

    #2
    Re: Access an attached label in VBA

    troy_lee@comcas t.net wrote:
    I have a sub with this code:
    >
    Sub ColorLabel()
    Dim ctlCurrentContr ol As Control
    Dim lngYellow As Long
    >
    lngYellow = RGB(255, 255, 0)
    >
    Set ctlCurrentContr ol = Screen.ActiveCo ntrol
    With ctlCurrentContr ol.???
    .BackColor = lngYellow
    End With
    End Sub
    >
    I want to run this code On Enter of a text box. Basically, I want to
    color the label of the active text box. I am calling the procedure on
    enter and will call another sub to restore the default formatting of
    the label once the text box loses focus.
    >
    I have found how to reference a text box using a label's parent
    property but I want to reference the label attached to the active text
    box but don't know which property to reference (thus the question
    marks above). Can anyone tell me how this is done or if it's even
    possible?
    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Labels are separate controls in Access VBA, i.e., they are not
    properties of the attached TextBox. What I do is name the label similar
    to the TextBox I will be working on, e.g.:

    TextBox name: txtFirstName
    Lable name: lblFirstName

    Then when the TextBox has the focus I just do something like this to get
    to the Label's properties:

    Dim ctlLabel As Label

    Set ctlLabel=Contro ls(Replace(Scre en.ActiveContro l.Name, "txt","lbl" ))

    ctlLabel.BackCo lor = vbYellow

    --
    MGFoster:::mgf0 0 <atearthlink <decimal-pointnet
    Oakland, CA (USA)
    ** Respond only to this newsgroup. I DO NOT respond to emails **

    -----BEGIN PGP SIGNATURE-----
    Version: PGP for Personal Privacy 5.0
    Charset: noconv

    iQA/AwUBSNms54echKq OuFEgEQIq1gCfWB wOHIDhoHti4oKa8 Ln7NyjU62MAnRwL
    WNwMMau5JpIecj/mdrGMCzj9
    =TxSH
    -----END PGP SIGNATURE-----

    Comment

    • troy_lee@comcast.net

      #3
      Re: Access an attached label in VBA

      Thanks a lot. I will give it a try.

      Troy Lee

      Comment

      • rkc

        #4
        Re: Access an attached label in VBA

        On Sep 24, 8:46 am, troy_...@comcas t.net wrote:
        Thanks a lot. I will give it a try.
        >
        Troy Lee
        A label attached to a textbox would be the one and only control in the
        textboxes controls collection.

        With ctlCurrentContr ol
        .Controls(0).Ba ckColor = lngYellow
        End With

        Comment

        • troy_lee@comcast.net

          #5
          Re: Access an attached label in VBA

          On Sep 24, 7:22 pm, rkc <r...@rkcny.com wrote:
          On Sep 24, 8:46 am, troy_...@comcas t.net wrote:
          >
          Thanks a lot. I will give it a try.
          >
          Troy Lee
          >
          A label attached to a textbox would be the one and only control in the
          textboxes controls collection.
          >
          With ctlCurrentContr ol
          .Controls(0).Ba ckColor = lngYellow
          End With
          Wow, that's amazing. The more I work with VBA the more I like it.

          Thanks a lot.

          Troy Lee

          Comment

          • Tony Toews [MVP]

            #6
            Re: Access an attached label in VBA

            rkc <rkc@rkcny.comw rote:
            >A label attached to a textbox would be the one and only control in the
            >textboxes controls collection.
            >
            >With ctlCurrentContr ol
            .Controls(0).Ba ckColor = lngYellow
            >End With
            Interesting. I was quite unaware of that syntax. Although I'd just use

            ctlCurrentContr ol.Controls(0). BackColor = lngYellow

            I don't mind using With and End With for a large number of lines but for only one not
            worth it.

            Tony
            --
            Tony Toews, Microsoft Access MVP
            Please respond only in the newsgroups so that others can
            read the entire thread of messages.
            Microsoft Access Links, Hints, Tips & Accounting Systems at

            Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/

            Comment

            • lyle fairfield

              #7
              Re: Access an attached label in VBA

              On Sep 24, 7:22 pm, rkc <r...@rkcny.com wrote:
              A label attached to a textbox would be the one and only control in the
              textboxes controls collection.
              >
              With ctlCurrentContr ol
                         .Controls(0).Ba ckColor = lngYellow
              End With
              Good One! Of course, I had to test the one and only one statement. I
              couldn't add two child labels to a textbox with the gui nor in code. I
              didn't try other kinds of children, but I'm guessing they wouldn't add
              either. So it seems one and only is true.

              As an aside to the OP, vbYellow exists as an intrinsic constant making
              lngYellow unnecessary.

              Dim lngYellow@
              lngYellow = RGB(255, 255, 0)
              Debug.Print lngYellow = vbYellow
              ' True

              It may not be commonly known that we can more or less code color
              intuitively by using simple math with the vb color constants. For
              instance (vbRed + vbYellow / 2) gives a deep orange.


              Comment

              • Tony Toews [MVP]

                #8
                Re: Access an attached label in VBA

                rkc <rkc@rkcny.comw rote:
                >A label attached to a textbox would be the one and only control in the
                >textboxes controls collection.
                >
                >With ctlCurrentContr ol
                .Controls(0).Ba ckColor = lngYellow
                >End With
                BTW I decided to blog this tip. See


                Tony
                --
                Tony Toews, Microsoft Access MVP
                Please respond only in the newsgroups so that others can
                read the entire thread of messages.
                Microsoft Access Links, Hints, Tips & Accounting Systems at

                Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/

                Comment

                Working...