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

Posted on 2006-04-07
Last Modified: 2009-01-05

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


Question by:shirt10
    LVL 65

    Accepted Solution

    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
    LVL 8

    Assisted Solution

    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?
    LVL 58

    Assisted Solution


    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.

    LVL 58

    Expert Comment

    Forgot a +1:

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    MsgBox 2 28
    Sql code problem 6 12
    Aggregate Function error 3 26
    Open Args 16 21
    Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now