Solved

set color to a label

Posted on 2002-05-21
6
595 Views
Last Modified: 2012-08-14
Hi,
I have a form with 5 labels on it. The caption is 1,2,3,4,5. I also have a listbox on the form with
2 columns and 5 records. The first column is a number
1 to 5 and the second column has a value of either P
or C like this.
1  P
2  C
3  P
4  P
5  C
What i want is to change the back color of all labels
where the second column is P.
Label 1 red, label 3 red and label 4 red all others should remain green.
Any help is appreciated
Manny
0
Comment
Question by:MannyN
6 Comments
 
LVL 11

Expert Comment

by:joekendall
Comment Utility
You would do something like this (assuming you have a listbox called ListBox1):

If ListBox1.Column(1) = "P" Then
    Label1.BackColor = 255 '255 is Red
    Label3.BackColor = 255
    Label4.BackColor = 255
End If

I think that will give you the concept.

Thanks!

Joe
0
 

Author Comment

by:MannyN
Comment Utility
Joe, I've been that far to change the color of the label
but the records are changeing. Next time label 1 has a C
then the back color should not be red.
I think it should be someting like
If Me!List48.Column(0)= "1" and Me!List48.Column(1)="P" Then
Label1.Backcolor = 255
else
End If
That changes my color right but only for the first item in the list box not for all records.
0
 
LVL 4

Expert Comment

by:zuijdhoek
Comment Utility
MannyN,

If you want to change the colors of the labels each time you change item in the listbox, you should use the AfterUpdate-event of your listbox-control.

Private Sub ListBox1_AfterUpdate()
    If ListBox1.Column(1) = "P" Then
       label1.BackColor = vbRed
       label2.BackColor = vbGreen
       label3.BackColor = vbRed
       label4.BackColor = vbRed
       label5.BackColor = vbGreen
    Else
       label1.BackColor = vbGreen
       label2.BackColor = vbGreen
       label3.BackColor = vbGreen
       label4.BackColor = vbGreen
       label5.BackColor = vbGreen
    End If
End Sub

Hope this might help you out,

Mark
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Accepted Solution

by:
andrejaa earned 100 total points
Comment Utility
Hi MannyN ,
here is function which you should run every time when
data in your listbox are changed .

----------code begin -----------------
Function ChgLabelBColor()

'this function should be called every time when
'you change values in your listbox

Dim row As Variant
Dim r_value As Variant

'enumerate all rows in list box
'and run through loop to check second column
'values in each row

For i = 0 To lstbox.ListCount - 1
    row = lstbox.Column(0, i) '1st column value
    r_value = lstbox.Column(1, i) '2nd column value
    'let asume that you had follow some rules
    'in assigning labels names
    'ie . L1 - L2 - L3 -L4 -L5 ......
    Select Case r_value
    'check the value of second column
    '( P = vbRed and C = vbGreen )
        Case "P"
            Me("L" & row).BackColor = vbRed
        Case "C"
            Me("L" & row).BackColor = vbGreen
        Case Else
            'if there is error
            '(<> P or <> C , make back color yellow)
             Me("L" & row).BackColor = vbYellow
    End Select
Next i

End Function
----------code end -----------------
0
 
LVL 11

Expert Comment

by:joekendall
Comment Utility
Try this code in the form's code module. You should have a Current event for the form.

Private Sub Form_Current()

If ListBox1.Column(1) = "P" Then
   Label1.BackColor = vbRed 'Or use 255
   Label3.BackColor = vbRed
   Label4.BackColor = vbRed
Else
   Label1.BackColor = vbGreen
   Label3.BackColor = vbGreen
   Label4.BackColor = vbGreen
End If

End Sub

This should do the trick. For each record that you go to, the color will change based on the value in the ListBox.

Thanks!

Joe

0
 

Author Comment

by:MannyN
Comment Utility
Andrejaa, Thanks for your help.
Works like a charm.
Manny
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now