How do I turn RGB color statements into variables?

sjs94704

Member
Local time
Yesterday, 22:55
Joined
May 7, 2013
Messages
41
OK, we know that for colors, Access used things like vbYellow, vbRed, vbBlue, etc..

WBUT, I have my own custom colors that I have represented here as RGB statements.

Private Sub SaleDate_GotFocus()
Me.SaleDate.BackColor = RGB(133, 258, 375)
End Sub

Private Sub SaleDate_LostFocus()
Me.SaleDate.BackColor = RGB(166, 166, 166)
End Sub

NOTE: For the RGB #'s I put in this example, I just keyed in random numbers for the sake of demonstrating for you what is going on!

What I would like to be able to do is replace the RGB(133, 258, 375) part of the statement with a word, just like the other vbYellow, vbRed, vbBlue that I can use. Here, check this out!

Private Sub SaleDate_GotFocus()
Me.SaleDate.BackColor = MyCustomColor1
End Sub

Private Sub SaleDate_LostFocus()
Me.SaleDate.BackColor = MyCustomClor2
End Sub

I just like how when the code is written that the word of the color is used in the actual code itself vs the RGB code.
So, please ... what to I do to define the RGB part of the command line?

Much thanks,
-Steven
 
Declare them as constants. Do it at the beginning of a separate module:
Code:
Option Compare Database
Option Explicit

Const lngBackGrdClr = 10921638  ' RGB(166, 166, 166)
Const lngOtherClr =   9868950      ' RGB(150, 150, 150)
You have to use the long value (that's what a colour is stored as)
 
2 options:
Code:
Public Enum CustomColors
    MyCustomColor1 = 16777093 ' = RGB(133, 258, 375)
    MyCustomColor2 = 10921638 ' = RGB(166, 166, 166)
End Enum

Public Function MyCustomColor(ByVal Index As Long) ' ... maybe even with string instead of long for better readability

    Select Case Index
        Case 1
            MyCustomColor = RGB(133, 258, 375)
        Case 2
            MyCustomColor = RGB(166, 166, 166)
        ...
    End Select

End Function
 
Add code like this in a standard module using the OLE colour value

'colour definitions
Public Const ColOrange = 10079487
Public Const ColPaleYellow = 10092543
Public Const ColBlue = 6697728
Public Const ColDarkBlue = 8388608
Public Const ColBorder = 4210752
Public Const ColLemon = 10092543
Public Const ColDarkRed = 128
Public Const ColMidRed = 1643706
Public Const ColPaleGreen = 13434828
Public Const ColDarkGreen = 32768
Public Const ColPaleGrey = 14869218
Public Const ColDarkGrey = 9868950
Public Const ColMidGrey = 11842740

Oops Minty beat me to it
 
Slightly different to the question:
Wouldn't it be more flexible to use the Access theme colors instead of the constant one and adjust the colors as needed?
1692950604714.png
 
When I had to do this, I used the method suggested by Minty and Isladogs. If you have a color you want, use the color picker once to find the RGB combination and then define a constant.
 
Declare them as constants. Do it at the beginning of a separate module:
Code:
Option Compare Database
Option Explicit

Const lngBackGrdClr = 10921638  ' RGB(166, 166, 166)
Const lngOtherClr =   9868950      ' RGB(150, 150, 150)
You have to use the long value (that's what a colour is stored as)
Although you can't use the RGB function when defining a constant, you can use a #bbggrr format like this:
Public Const intCanary = &H99FFFF
 
Last edited:
Thanks everyone for your replies! I really appreciate it!
 
Add code like this in a standard module using the OLE colour value

'colour definitions
Public Const ColOrange = 10079487
Public Const ColPaleYellow = 10092543
Public Const ColBlue = 6697728
Public Const ColDarkBlue = 8388608
Public Const ColBorder = 4210752
Public Const ColLemon = 10092543
Public Const ColDarkRed = 128
Public Const ColMidRed = 1643706
Public Const ColPaleGreen = 13434828
Public Const ColDarkGreen = 32768
Public Const ColPaleGrey = 14869218
Public Const ColDarkGrey = 9868950
Public Const ColMidGrey = 11842740

Oops Minty beat me to it
Just checking... (I am just learning code here!) If I put these in a module, say called modGlobal, with the way this code is declared I can now use them anywhere in my database without having to declare them over and over. Do I have that right?
 
Yes. If they are declared public in a standard module (not a class module) then they are in scope for the whole project.
 
You can do the same conversions in Access. For example, see my Colour Converter

1693075147881.png
 

Users who are viewing this thread

Back
Top Bottom