How do I turn RGB color statements into variables? (1 Viewer)

sjs94704

Member
Local time
Today, 07:03
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
 

Minty

AWF VIP
Local time
Today, 15:03
Joined
Jul 26, 2013
Messages
10,371
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)
 

Josef P.

Well-known member
Local time
Today, 16:03
Joined
Feb 2, 2023
Messages
826
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
 

isladogs

MVP / VIP
Local time
Today, 15:03
Joined
Jan 14, 2017
Messages
18,227
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
 

Josef P.

Well-known member
Local time
Today, 16:03
Joined
Feb 2, 2023
Messages
826
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
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:03
Joined
Feb 28, 2001
Messages
27,187
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.
 

Tom Robinson

New member
Local time
Today, 07:03
Joined
Jan 7, 2015
Messages
5
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:

sjs94704

Member
Local time
Today, 07:03
Joined
May 7, 2013
Messages
41
Thanks everyone for your replies! I really appreciate it!
 

sjs94704

Member
Local time
Today, 07:03
Joined
May 7, 2013
Messages
41
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?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 10:03
Joined
May 21, 2018
Messages
8,529
Yes. If they are declared public in a standard module (not a class module) then they are in scope for the whole project.
 

isladogs

MVP / VIP
Local time
Today, 15:03
Joined
Jan 14, 2017
Messages
18,227
You can do the same conversions in Access. For example, see my Colour Converter

1693075147881.png
 

Users who are viewing this thread

Top Bottom