How To Set A Forms Background Color To Be One of X Colors?

Hey all,

I have a small access database which my department enters review information into all day.
Last week for April Fools I added the code below to randomly set the background color below for some (and for three of the guys to always set it to hot pink).

Private Sub Associate_LostFocus()
    Select Case Associate
        Case "Mary Anderson", "Kelly Wright"
            Me.Section(acDetail).BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)
        Case "Nick Dormer", "Shawn Justice", "Earl Onstott"
            Me.Section(acDetail).BackColor = RGB(255, 105, 180)
        Case Else
            Me.Section(acDetail).BackColor = RGB(238, 238, 209)
    End Select

End Sub

This turned out to be a big hit, and I now have a request from them to alternate between a list of 5-10 specific colors (they are picking them out at the moment!!!).

Is there an small, easy code I can use instead of above to acomplish this?

Thanks in advance


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Dump the following into the top of any code module, then in your code you can refer to the color as glColorWhatever.

'Colors (used in reports to simulate alternating greybars, and forms to indicate fields that do not accept data entry.)
Global Const glColorLightGrey As Long = -2147483633
Global Const glColorMediumGrey As Long = 13684944
Global Const glColorWhite As Long = 16777215
Global Const glColorBlack As Long = 0
Global Const glColorRed As Long = 255
Global Const glColorYellow As Long = 65535
Global Const glColorGreen As Long = 65280

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Assuming you need the user to load a color based on their individual preferences, I'd write a simple INI file that can be stored on their machine and then read/write to it whenever they want a new color.

Alternatively, assuming you have a table of users, add a field for color and then write some simple code to change the form color based on the table.

Make sense?

I don't want to pick, but -2147483633 is not a color. It's the constant vbButtonFace, which isn't necessarily light gray, as it depends on the desktop color scheme. Also, basic colors are already defined in VB: vbRed, vbWhite, etc.


> alternate between a list of 5-10 specific colors

This isn't very clear. For all users? for those who select "fancy colors" in an options dialog? randomly? sequentially?

Anyway, you can create a list like this:

    Dim varColors
    Dim intC as Integer

    varColors = Array( _
        RGB(1,2,3), _
        RGB(4,5,6), _
        [...] _

    ' loop:
    For intC = 0 To UBound(varColors)

    ' random:
    Me.Detail.Backcolor = varColors(Int(Rnd()*UBound(varColors)))

But it's probalby best to store all that outside of the application, as dds110 suggests.

Forgot a +1:

    ' random:
    Me.Detail.Backcolor = varColors(Int(Rnd()*UBound(varColors)+1))

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.