Need random colors for multiple text boxes on single form

P

psandrew

I have a form with 15 text boxes and want Access 2003 to randomly set a
different background color for each text box... Any ideas? Do I need some
code? Need to K.I.S.S. Thanx!
 
D

Damon Heron

The simplest way would be to use the QBColors (0 thru 15).
For each textbox, in the load event of the form, put:
Private Sub Form_Load()
Randomize
Me.Text0.BackColor = QBColor(Int(15 * Rnd))
Me.Text1.BackColor = QBColor(Int(15 * Rnd))
etc.....
End Sub

This is the easiest to code, but you could use a for each statement to cycle
thru the textboxes if you want.
If you want a greater variety of colors, check out the RGB function.

Damon
 
P

psandrew

This works fine, thank you, but I don't want any two or more text boxes to
ever have the same random color. Also would like to know how to use the RGB
function for the same situation, except that the text box color and shade
would be based on the text box value. For example, the values in the 15 text
bxs are 1 through 15. I want boxes with values 1 to 5 to be EACH a random
shade of blue, the boxes with values 6 to 10 to be ALL a random shade of red,
and boxes with values 11 to 15 to be EACH a random shade of any color. Your
help is most excellent. K.I.S.S for me please. Thanx.
 
D

Damon Heron

First, recognize that what you are asking isn't truly random, in that you
are restricting the color ranges. With that
said, you can use the RGB function in the same way. The function has 3
numbers, 0 to 255, such that RGB(255,0,0) is red,
RGB(0,255,0) is green, and RGB(0,0,255) is blue.
Any combination of numbers in the two zero fields will give you variations
of tints. So, if you put this code in the form load (or current event):
Dim x As Integer
Randomize
x = 250
Me.Text0.BackColor = RGB(255, Int(x * Rnd), Int(x * Rnd)) 'red shades
Me.Text1.BackColor = RGB(Int(x * Rnd), 255, Int(x * Rnd)) ' green shades
Me.Text2.BackColor = RGB(Int(x * Rnd), Int(x * Rnd), 255) 'blue shades
(substituting your textboxes for the above)
Play with the value of x. A low value gives much less variation than a high
value, but the high values will more likely
cross the "threshhold" from one color to another. Although I haven't tried
it, you could also have a second variable, y, that
will give you even more control over the tints.

Damon
..
 
Top