Predefined Color Variables

ChronicFear

Registered User.
Local time
Today, 09:32
Joined
Oct 18, 2007
Messages
66
Hi Everyone,

Here is some code to predefine different color variables to make your life easier. This is provided by Alan Simpson via Access VBA for Dummies and is reprinted with permission. Enjoy!

Code:
Sub DefineColors()
       
    'Delcare color names.
    Dim Beige, Brown, Chartreuse, DarkBlue, DarkGreen As Long
    Dim Fuschia, Gold, Gray, HotPink As Long
    Dim Lavender, Maroon, Navy, Olive, Orange As Long
    Dim Pink, Purple, Salmon, Silver, Teal As Long

    'Assign colors to variables as RGB values.
    Beige = RGB(245, 245, 220)
    Black = RGB(0, 0, 0)
    Blue = RGB(0, 0, 255)
    Brown = RGB(165, 33, 33)
    Chartreuse = RGB(127, 255, 0)
    Cyan = RGB(0, 255, 255)
    DarkBlue = RGB(0, 0, 139)
    DarkGreen = RGB(0, 100, 0)
    Fuschia = RGB(255, 0, 255)
    Gold = RGB(255, 215, 0)
    GoldenRod = RGB(218, 165, 32)
    Gray = RGB(128, 128, 128)
    Green = RGB(0, 255, 0)
    HotPink = RGB(255, 105, 180)
    Lavender = RGB(230, 230, 250)
    Magenta = RGB(255, 0, 255)
    Maroon = RGB(255, 0, 255)
    Navy = RGB(0, 0, 128)
    Olive = RGB(128, 128, 0)
    Orange = RGB(255, 165, 0)
    Pink = RGB(255, 192, 203)
    Purple = RGB(128, 0, 128)
    Red = RGB(255, 0, 0)
    Salmon = RGB(241, 128, 114)
    Silver = RGB(192, 192, 192)
    Teal = RGB(0, 192, 192)
    White = RGB(255, 255, 255)
    Yellow = RGB(0, 255, 255)

End Sub
 
Thanks,

I created an enum type eColor using your definition.

[edit]However most of the variables you defined are variant types and not Long.[/edit]
 
Is that variable declaration really in the book? Dummies!
 
As I am clearly a VBA Access beginner (betrayed by my choice of bed-time reading), would either of you be so kind as to explain how I would know that it should be variant instead of long?

Also, Guus2005, what do you mean by enum type eColor? Is that some better way of declaring and utilizing these colors?

Thanks!
 
enum types are more descriptive
Code:
Public Enum eColors
    'Assign colors to variables as RGB values.
    Beige = 14480885    'RGB(245, 245, 220)
    Black = 0           'RGB(0, 0, 0)
    Blue = 16711680     'RGB(0, 0, 255)
end enum
to be used as
Code:
me.lblSomeLabel.ForeColor = eColor.Beige
The intellisense helps you out if you forgot the exact name of the color.
Or when you don't use enum
Code:
me.lblSomeLabel.ForeColor = Beige
 
Doesn't the variable designation I posted do this same thing? I was under the impression that if I had the routine I posted, if I have Whatever.Forecolor = Black then it would be black. I don't see the value of first converting from RGB to numeric then to a name if you can go right from RGB to a name. Am I missing something?

Also, not to harp the point, but how would I recognize that these variables should be variant instead of long?

Thanks!
 
Not that it matters much here, but VBA expects an explicit declaration for each variable. So in the code:

Dim Beige, Brown, Chartreuse, DarkBlue, DarkGreen As Long

Only DarkGreen is declared as long - the others are undeclared and will be assigned Variant by default.

Catches everyone out as other languages would allow this sort of declaration to work as intended.
 
Not that it matters much here, but VBA expects an explicit declaration for each variable. So in the code:

Dim Beige, Brown, Chartreuse, DarkBlue, DarkGreen As Long

Only DarkGreen is declared as long - the others are undeclared and will be assigned Variant by default.

Catches everyone out as other languages would allow this sort of declaration to work as intended.

Been there, done that. Look at the second post.
 
I know. I was explaining for ChronicFear, who asked...
 
I think I'd use Enum or Constant - or was it a point that one should be able to change the value of those public variables?

Also, since vbBlack, vbRed, vbGreen, vbYellow, vbBlue, vbMagenta, vbCyan, vbWhite already exist I'm not sure I see the point in "redefining them" ;)
 
Is there a constant for Off-White? Perhaps vbOffWhite?

No there isn't.

That's the reason.
 

Users who are viewing this thread

Back
Top Bottom