Just a quick post to discuss colors in Access.
Colors can be an asset to designers, if used correctly, to greatly enhance your application(s), no doubt!
But how can we define colors is the question I thought I’d quickly touch upon today.
All too often I see in code people defining thing along the lines of
'Set the font color of a control
Me.ControlName.ForeColor = vbRed
where developer use one of the predefined ColorConstants, which include:
- vbBlack
- vbBlue
- vbCyan
- vbGreen
- vbMagenta
- vbRed
- vbWhite
- vbYellow
There are 2 major issues with the above (excluding the use of vbBlack and vbWhite): the choices are very limited and the colors are very aggressive (not colors I’d recommend to use as they are hard to look at, so not a good user experience).
So what are our options?
Luckily for use, as with almost everything Access (or VBA for that matter), we have other, better, approaches we can use for designating colors.
RBG
We can use the RGB function to specify the quantity of each color resulting in our final color.
Thus, we can implement it in code by doing something along the lines of
'Set the font color of a control
Me.ControlName.ForeColor = RGB(255, 0, 0) 'Red
Below are a few examples of color specifications using RGB:
- RGB(0, 0, 0) ‘Black
- RGB(0, 0, 255) ‘Blue
- RGB(0, 255, 255) ‘Cyan
- RGB(0, 255, 0) ‘Green
- RGB(255, 0, 255) ‘Magenta
- RGB(255, 0, 0) ‘Red
- RGB(255, 255, 255) ‘White
- RGB(255, 255, 0) ‘Yellow
What is nice with RGB is you can specify a much greater range of colors. Thus you are able to utilize much nicer color pallets that then built-in vb ColorConstants.
Web/HMTL/CSS/HEX Colors
If you have any HEX value you which to use, then use my utility (the link is below) and use it to convert it to an OLE value and use it.
The last approach I wanted to cover today was using web colors, HTML Hex color values. These have become such common place that, to me, this is the way to go when it comes to designating colors.
To designate a HEX color in VBA we simply do
'Set the font color of a control
Me.ControlName.ForeColor = Val("&H" & "FF0000") 'Red
Below are a few examples of color specifications using HEX:
- Val(“&H” & “000000”) ‘Black
- Val(“&H” & “0000FF”) ‘Blue
- Val(“&H” & “00FFFF”) ‘Cyan
- Val(“&H” & “00FF00”) ‘Green
- Val(“&H” & “FF00FF”) ‘Magenta
- Val(“&H” & “FF0000”) ‘Red
- Val(“&H” & “FFFFFF”) ‘White
- Val(“&H” & “FFFF00”) ‘Yellow
Do you notice any similarities between RGB and HEX colors yet?!
A great website regarding colors (HEX & RGB) is HTML Color Codes
Color Convertion Utility
Many moons ago, I created a simple little utility that you can use to find a color and its’ corresponding OLE, RGB and HEX values, refer to MS Access Sample – Colors Converter for the full article or simply use the button below to download your own copy (as usual, no strings attached!).
Disclaimer/Notes:
If you do not have Microsoft Access, simply download and install the freely available runtime version (this permits running MS Access databases, but not modifying their design):Microsoft Access 2010 Runtime
Microsoft Access 2013 Runtime
Microsoft Access 2016 Runtime
Microsoft 365 Access Runtime
In no event will Devhut.net or CARDA Consultants Inc. be liable to the client/end-user or any third party for any damages, including any lost profits, lost savings or other incidental, consequential or special damages arising out of the operation of or inability to operate the software which CARDA Consultants Inc. has provided, even if CARDA Consultants Inc. has been advised of the possibility of such damages.
Feel free to download a 100% unlocked demo copy by using the link provided below: Download “Access - Color Converter Utility (mdb x32)” Colors.zip – Downloaded 48107 times – 51.32 KB
I hope this help some of you out there understand some of the possible ways of working with colors in Access, and yes, there are still more ways, but that will be for another day. One way or another, RGB and HEX are the best and most versatile approaches to use.
I improved your color converter a little.
At the beginning of the txtHEX_AfterUpdate procedure, I added the following handling
If Left(Me.txtHEX.Value, 1) = “#” Then Me.txtHEX.Value = Right(Me.txtHEX.Value, Len(Me.txtHEX.Value) – 1)
It helps remove the hashtag that is added to the HEX value of the color by the pipette tool embedded in the Firefox developer’s tools console