By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

Change colour of text, based on combo box selection

P: 7
i have a combo box (combo box 1) in a form header that users use to selct item numbers from.
I also have a combo box (combo box 2) that users use to select a priority number (1,2,3) for that item. This is then stored on the items record.

I was wondering if the item has a priority 1 in combo box 2, then it could display the text in combo box 1 as red (or posibly the background, or something to make it stand out).

Also i would like priority 2 items to display as amber and priority 3 items to display as green.
Feb 22 '08 #1
Share this Question
Share on Google+
7 Replies


Expert 100+
P: 446
Hi
The short answer is 'Yes'

If you want the colour to change immediately after combo2 has been set then you will need to put code in the After_Update event of combo2.

If you want the colours to persist and change according to the priority when you are browsing through the records you will have to put the same code into the On_Current event of the Form.

You could change the colour of the text in the combo or it's back colour, or if you wanted to go overboard you could change the colour of the Header section etc

Expand|Select|Wrap|Line Numbers
  1. Private Sub combo2_AfterUpdate()
  2. If Me.combo2 = 2 Then Me.combo2.BackColor = RGB(250, 0, 0)
  3. If Me.combo2 = 2 Then Me.combo2.ForeColor = RGB(255, 255, 255)
  4. If Me.combo2 = 2 Then Me.Form.Section(acHeader).BackColor = vbRed
  5. If Me.combo2 = 2 Then Me.Form.Section(acDetail).BackColor = vbBlack
  6. If Me.combo2 = 2 Then Me.Form.Section(acFooter).BackColor = vbBlue
  7. End Sub
You can use the set 'vb' colours or mix your own using the RGB() function.

Get the idea? (don't forget to drop the u in colour)

Sorry, I have just read your last line! You will have to use If ...ElseIf construction to do what you want. I'll let you do that bit and you can have fun mixing your amber and green colours !


S7
Feb 22 '08 #2

NeoPa
Expert Mod 15k+
P: 31,768
I would rather suggest using a Select Case statement for this (personal preference and an alternative idea).
Expand|Select|Wrap|Line Numbers
  1. Private Sub Combo_Box_2_AfterUpdate()
  2.   Select Case [Combo Box 2]
  3.   Case 1
  4.     Me.[Combo Box 1].BackColor = vbRed
  5.   Case 2
  6.     Me.[Combo Box 1].BackColor = vbYellow
  7.     'Me.[Combo Box 1].BackColor = RGB(255, 128, 0)
  8.   Case 3
  9.     Me.[Combo Box 1].BackColor = vbGreen
  10.   End Select
  11. End Sub
If Yellow isn't a good enough match, then the (commented) RGB() line may be used instead. You can even play with the values to match the colour you want.
Good luck :)

PS. Hi Sierra. I didn't know you were a Brit too (spelling of colour) ;)
PPS. As is Nasher too by the looks of things :)
Feb 24 '08 #3

Expert 100+
P: 446
PS. Hi Sierra. I didn't know you were a Brit too (spelling of colour) ;)
True!
I noticed that Nasher had spelt it 'correctly' too which was why I mentioned it because (not wanting to be condescending here Nasher) it was a Newbie sort of question, so a typo could compound the problem.

I thought I would help further by showing the syntax for reference to Header etc because I remember how long it took me the first time.

Incidentally, I feel that vbGreen is a misnomer. Grass is green, trees are green but the only time I have seen vbGreen has been inside a baby's nappy, or should that be diaper?
S7
Feb 24 '08 #4

NeoPa
Expert Mod 15k+
P: 31,768
...
Incidentally, I feel that vbGreen is a misnomer. Grass is green, trees are green but the only time I have seen vbGreen has been inside a baby's nappy, or should that be diaper?
S7
Yes, I liked that you gave different examples of the code :)
As to vbGreen being a bit off, I think that as Green is a hue (Primary Colour) the value (65,280 or 0xFF00) is simply saying 100% of Green and 0% of both Red & Blue. How a graphics card (or more likely monitor) manages to express this is independant of the software I would think.

PS. Thanks for the reminder :( I've not had to deal with that sort of thing for many years :)
PPS. Congratulations on the 200 posts when you reply to this :D
Feb 24 '08 #5

Expert 100+
P: 446
PPS. Congratulations on the 200 posts when you reply to this :D
Thank you. It's amazing how quickly they add up.

We must be sad doing this on a Sunday afternoon though!

Incidentally I have used you Case Statement on another thread for changing colours of a Tab Control

It get's repeatative after a bit doesn't it!

S7
Feb 24 '08 #6

P: 7
Cheers guys for the help.

And you guessed right, i am a english newbie, so that "color" spelling could have really messed things up for me. Well done for being so alert.
Feb 25 '08 #7

Expert 100+
P: 446
Your welcome Nasher

Just looking over my first posting I hope you realise that it was intended to be a guide to the syntax for a SINGLE STATEMENT.

Looking at it now I think "Why did I put all those If's ?" Obviously, you would not write an 'If Statement' like this in a program if you wanted to change MULTIPLE attributes! (I'm sure you know how to write an If statement but have a look at the Help if you have been brought up on Java or C# or something)

NeoPa's suggestion to use 'Select Case' is far more elegant and I wish I could get into the habit of using it but my formative years were spent learning Fortran V and old habits die hard.

S7
Feb 25 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.