Storing colors then using in VBA

steve1111

Registered User.
Local time
Today, 00:34
Joined
Jul 9, 2013
Messages
170
I am trying to see if it is possible to store colors in a form or table and then reference them while in VBA. What I am hoping to do is when I write all my code for command buttons to change On Got Focus, instead of writing xxx.backcolor = RGB (255,255,255) i could do something like xxx.backcolor = Forms!HiddenColors!Command That way if i want to ever change the color scheme of the db, I can change it in one place rather than hunt lines of code.
I have played around but with no success.


Thanks for the help.
 
All colors have a numeric code. I've never seem a list of them. some have an "ac prefix, such as, acred, acwhite, acgreen, etc. When I need a partiocular color, I set the back color of a text box to the appropriate AND THEN LOOK AT THE BACKCOLOR property for the value. Of course, that number can be stored.
 
Thanks for the reply, i can get the RGB number or the code for the color i need just fine. I want to store that in a master form or table so that when i want to change 100 control colors all at once from black to white, i can just change the color value in the one form and since hopefully i can write the code to point to the stored value place and not the actual color code, so that all controls are updated at once. that is the part i am having trouble with, assigning the color to a control and not a color code.
 
dim ctl as control

for each ctl in me.controls
if ctl.controltype = actextbox or ctl.controltype = combobox then 'controls changed
if ctl.backcolor = acwhite then
ctl.backcolor = acblack
else
ctl.backcolor = acwhite
end if
end if

The foregoing should get you started. You'll have to allow for initiali entry and probably change the forecolor appropriately too.

You might have to use me(ctl.name).backcolor = . . .
 
a colour is stored a long integer (therefore up to about 2 billion). a colour has 3 components, RGB, each of which can take a value between 0 and 255

the full colour is stored thus

(redvalue * 1) + (greenvalue * 256) + (bluevalue *65536)

so rgb(255,255,255) produces white - a full mix of red, green and blue
you need your own function to go the other way - ie select a colour from a palette and then divide it back into RGB constituents

so it's a matter of taste whether you store the full value, or the separate RGB values

A handful (not all) colours have a vbconstant value - so vbBlack is 0, vbRed is 255 etc.

this sort of basic code will work

Code:
 mycolour = dlookup("storedcolour","sometable")
 for each ctrl in me.controls
     ctrl.backcolor = mycolour
 next
 
Thanks everyone for the tips. I am not super experienced so in case someone else is looking, building off the premise from Gemma, I just set my on load command for the form to:

Dim ButtonColor as String
ButtonColor = RGB( 125,159,159) ' Manual Change

cmdButton.backcolor = ButtonColor


I did the same for the .ForeColor, .HoverColor and .PressedColor.

Rather than storing in a table and doing the Dlookup as Gemma suggested, i can use the find feature in VBA to search for the 'Manual Entry

probably not the best way to do it but since i was having issues with the dlookup and you have to change the color some place, i just went this route.

hope it helps someone.
 
I can change the colours on loading the form using an event procedure in VBA. This has to be added to each form. I want to give users the option of 2 or 3 colour sets and am reasonably certain that I can do this by accessing table based options. I have never used any vba code that is not local to a form. Can I code this once and simply invoke it from each form? If so, how?
 

Users who are viewing this thread

Back
Top Bottom