Using Colors in an Option Group - RGB Function

S

ssignore

Hello, MS Online Community!
I'm new to VBA and trying to learn with a beginner's book.
Can anyone give me the appropriate structure for a procedure which assigns a
color to an option in an option group?
Below is the failed code.

'I know this is incorrect, but I can't seem to find the right structure to
assign the color lime or 65408 to the Option of Lime.

Private Sub OptLime_GotFocus()
Me.txtMain.Forecolor = RGB(65408)
End Sub


Any help is appreciated!
Thanks,
Simone
 
M

Marshall Barton

ssignore said:
I'm new to VBA and trying to learn with a beginner's book.
Can anyone give me the appropriate structure for a procedure which assigns a
color to an option in an option group?
Below is the failed code.

'I know this is incorrect, but I can't seem to find the right structure to
assign the color lime or 65408 to the Option of Lime.

Private Sub OptLime_GotFocus()
Me.txtMain.Forecolor = RGB(65408)
End Sub


The RGB function wants the red, green and blue values
separately:
Me.txtMain.Forecolor = RGB(0,255,128)

OTOH. you can use your color number directly:
Me.txtMain.Forecolor = 65408
 
S

ssignore

Thank you, Marsh.
How do you get the individual red, green and blue values? In other words,
how did you know that RGB(0,255,128) would make the color lime?
Much appreciated.
 
M

Marshall Barton

Given the color number X:
Red (X And &HFF0000) \ 256^2
Green (X And &H00FF00) \ 256
Blue (X And &H0000FF)
 
S

ssignore

Whoa! Okay, I couldn't really figure that out, but
Me.txtMain.Forecolor = 65408
worked just fine!
Thanks very much again!
S

Marshall Barton said:
Given the color number X:
Red (X And &HFF0000) \ 256^2
Green (X And &H00FF00) \ 256
Blue (X And &H0000FF)
--
Marsh
MVP [MS Access]

How do you get the individual red, green and blue values? In other words,
how did you know that RGB(0,255,128) would make the color lime?
 
M

Marshall Barton

Whoa? What do you mean Whoa? There's no Whoaing in Access
;-)

&H signifies that the following string is a hexadecimal
number (eg &HFF is 255, &HA is 10, etc).

AND sets the result to 0 if either value has a 0 bit in a
position. If both values have a 1 in a position, the result
will have a 1 in that position.

Dividing a value by 256 shifts the bits to the right by 8
positions.

It's not rocket science and if you worked it out with pencil
and paper, you could see all before too long.
 
M

Marshall Barton

Dale_Fye via AccessMonster.com said:
Any idea why they are converting these long integers into hexidecimal in 2007?


I know you can divide by 65536 (256^2) to get the red value
cRed = int(65408/65536)

and you can take the integer of the remainder / 256 to get the green value
cGreen = int(65408 - (cRed* 65536))/256

cBlue = 65408 - (cRed * 65536) - (cGreen * 256)

But now they are displaying the hex # in the color boxes, is there a function
for converting the hex values back to long integer? or the reverse?


No idea what or why, Dale. I haven't used A2007 enough to
even notice that.

You can convert a string of hex digits to a Long by using
CLng("&H" & stringvar) and the Hex function will convert the
other way.

Another thing to use is the Mod operator:
Blue = 65408 Mod 256

I prefer to use the Hex mask approach because it is easier
for me to understand.
 
M

Marshall Barton

Absolutely. The VBA AND operator (SQL And is different) is
rarely needed with Byte, Integer and Long types, but in some
situations can be very powerful in isolating particular bits
from a variable that is made up of multiple values (eg a
color).
--
Marsh
MVP [MS Access]


Dale_Fye via AccessMonster.com said:
I hadn't thought about the hex mask, guess you could do that with long
integers as well.


Marshall said:
Any idea why they are converting these long integers into hexidecimal in 2007?
[quoted text clipped - 8 lines]
But now they are displaying the hex # in the color boxes, is there a function
for converting the hex values back to long integer? or the reverse?

No idea what or why, Dale. I haven't used A2007 enough to
even notice that.

You can convert a string of hex digits to a Long by using
CLng("&H" & stringvar) and the Hex function will convert the
other way.

Another thing to use is the Mod operator:
Blue = 65408 Mod 256

I prefer to use the Hex mask approach because it is easier
for me to understand.
 
L

laskowv

Just click on the "Define Custom Colors" and pick your color. The R,G,B
values will show up on the right side of the box and voila!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top