Function to define colors

monfas

Registered User.
Local time
Today, 15:13
Joined
Jun 18, 2012
Messages
32
:confused:Hi, I have one color scheme I want to use all through the database I am developing. The next examples have just one color defined, to make it simpler (a dark blue, that I would call B1)

If I define the color inside the sub, it's ok (see below).

Code:
private sub setlabel()
Dim B1
B1 = RGB (0,52,105)
me.label1.forecolor = B1
end sub
... however this means I have to repeat the color definition every sub, so I thought would be neater to define a function to set my color codes (I have 20 colors).

Code:
Function SetColor()
Dim B1

B1 = RGB (0,52,105)

End function
My objective, is when I'm working in forms, Iwould (ideally) call this function "setcolor" and just write my code for the blue. I tried the examples below:
Code:
Private sub setlabel()
SetColor()
me.label1.forecolor = B1
end sub
or

Code:
private sub setlabel()
SetColor(B1)
me.label1.forecolor = B1
end sub
...but doesn't work.

Again, this is probably some definition of arguments or dimensions that I am not aware of. Does any one have an idea how to predefine the colors in a function to give them a "short" code which I can call in any sub in the database?
 
B1 would need to be declared for the module.

However you would be better off putting the code into a Standard Module and passing the control object to it. Then you can call it from anywhere in the project.

Code:
Public Sub SetControlBlue(ctrl As Access.Control) 
   ctrl.Forecolor = RGB (0,52,105)
End Sub
 
Cool, that makes a lot of sense. But then I need to have one for Forecolor, one for backcolor etc... right?
 
Found another way,

define the colors as public constants. Works fine.

Code:
Public Const B1 As Long = 6894592 ' Dark Blue RGB 0 52 105

and then
Code:
private sub setlabel()
me.label1.forecolor = B1
end sub

Works fine.
 
Yes the constants are a good idea and can also be use inside the sub I posted.

However you will still be repeating almost identical code throughout the project if you don't use the public sub.

With the one public sub you can simply write this line anywhere for any control.
Code:
SetControlBlue Me.labelname

Include the commands for Forecolor and Backcolor in the same sub.
 

Users who are viewing this thread

Back
Top Bottom