Use variable to set label backcolor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tuxalot
    New Member
    • Feb 2009
    • 200

    #1

    Use variable to set label backcolor

    I am trying to set the backcolor of a label and am getting a type mismatch. This code fires in the after update event of the combobox.

    strColor = vbRed in this example. I have also tried it using hex values. In the underlying table for cboSourceType, Column 3 is a text data type.

    Code:
        Me!lblColor.BackColor = FFFFFF
        strColor = cboSourceType.Column(3)
        MsgBox strColor
        Me!lblColor.BackColor = strColor
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    In line 1 you need to tell VBA interpreter that the value concerned is hex using the &H operator:

    Code:
    Me!lblColor.BackColor = &HFFFFFF
    -Stewart

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      vbRed is just a constant for the number 255. Convert your colors to its corresponding base 10 number.

      Comment

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

        #4
        Another comment about your original code segment is that the Backcolor property is numeric. Although Access often converts types on the fly you mention that the column in your combo you are using is text. I'd suggest you use the Val function to convert the value explicitly to a numeric value.

        -Stewart

        Comment

        • tuxalot
          New Member
          • Feb 2009
          • 200

          #5
          Thanks for the advice.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32669

            #6
            Also, VBA has a whole bunch of colour constants defined for use - including vbBlack, vbWhite, vbRed, vbBlue, ... The list goes on. Code is far neater if such mnemonics are used in place of literal numbers with no obvious connection to their use.

            Comment

            Working...