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

Posted on 2006-04-07
Medium Priority
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
  • 2
LVL 66

Accepted Solution

Jim Horn earned 136 total points
ID: 16403675
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

Assisted Solution

dds110 earned 132 total points
ID: 16403756
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

harfang earned 132 total points
ID: 16404272

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

ID: 16404280
Forgot a +1:

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


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

807 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