Full Version: random color background
UtterAccess Forums > Microsoft® Access > Access Forms
I am wondering if there is a way to set the background color of a form to random so that it would change each time you opened it. This is kinda just for fun, but it would be very cool if I could do it. Thanx for any help on this one.
It's possible. Make a table:
ColorID Number
ColorNumber Text
ColorName Text
Keep the ColorID numbers sequential, 1 - however many colors you decide to use as random colors. I realize it's not an autonumber PK, but, this is a 'system' table, not a data table. For ColorNumber, enter the Access color number and the name (just for your use, you can leave it out if you want). You'd need a function to run in the On Open of the form to pick a random number in the range of colors you have and then set the background of the form to the associated colornumber.

Private Sub Form_Open()
Dim mynum As Double

mynum = Rnd

mynum = mynum * 83886099

Me.Box2.BackColor = mynum

End Sub

This doesn't allow you to limit color choices like Noah's does.....Box2 is a Box added behind all other objects, the size of the form itself. Box2's color is randomized from 0 to 83886099 depending on the randomized decimal value and the result of its multiplication by 83886099, so any number of colors in the range can be randomly displayed.

Otested this with a Command button.

Edit: I found the upper limit before everything is black: 83886099, and changed the code sample accordingly, and tested it on Form_Open.
Pretty complex however the attached may give you some ideas. Was provided by a UA member but unfortunately do not recall the original poster.
Good Luck
Cool cool.gif thanks ...
Glad to Help, Have A Nice Day!
The upper limit before everything is black may be 83886099, but everything above 16,777,215 is a repeat of a lower number.
I thought about that.....but I didn't know what the limit was for discrete colors. So my first guess at 16777215 was right.....should have left it unedited laugh.gif
Thanx for all your help guys. I haven't had a chance to try anything yet, as other projects have been pulling me in other directions.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.