Question

I created a db in access where an article list is shown with its status in a table subform. there are 5 different status option and I need them to have a specific color. but in access 2007 I can only use 3 conditional formatting statements (4 if you count the default setting).

But I need 5. (01, 05, 06, 08, 09). I've been looking everywhere, but I can't seem to find away around this.

I've been trying from this angle:

If Me.txtArtikelStatus = "05" Then
    Me.txtArtikelStatus.BackColor = RGB(215, 228, 188)
ElseIf Me.txtArtikelStatus = "01" Then
    Me.txtArtikelStatus.BackColor = RGB(219, 238, 243)
ElseIf Me.txtArtikelStatus = "06" Then
    Me.txtArtikelStatus.BackColor = RGB(252, 213, 180)
Else
    Me.txtArtikelStatus.BackColor = RGB(230, 182, 184)
End If

But that's as far as I got. all records are now the same color (green according to status 05)

My subforms name is fsubAlleArtikelen, and it's based on a query called qryAlleArtikelen which is based on tables tblZMMPC and tblZMAC

The column for the status is called txtArtikelStatus and is a text column.

I'm guessing I need to create a loop, but I don't know enough about VBA to be able to create one that actually works.

Was it helpful?

Solution

Use a Select case statement.

This is much cleaner to do a lot of this sort of thing. You can have options which then effectively do a large if/elseif statement like you are trying to do but the code works a lot better (and is cleaner).

See this link for more documentation.

Here is what will work for your situation:

Select Case Me.txtArtikelStatus

    Case "05"
        Me.txtArtikelStatus.BackColor = RGB(215, 228, 188)

    Case "01"
        Me.txtArtikelStatus.BackColor = RGB(219, 238, 243)

    Case "06"
        Me.txtArtikelStatus.BackColor = RGB(252, 213, 180)

    Case "08"
        Me.txtArtikelStatus.BackColor = RGB(252, 213, 180)
    Case "09"
        Me.txtArtikelStatus.BackColor = RGB(252, 213, 180)

    Case Default
        'this captures everything else

End Select

OTHER TIPS

I'm thinking by your code post that you are trying to do this in a continuous subform. Unfortunately, if you change a property of one control such as the color, it will do it for all visible controls of the same name in the continuous subform. I will try to explain a workaround.

1) Create a separate table that has 2 columns: an ID column which for you will store "05", "01", etc. The next column will be an OLE Object where you can store an image that contains this color.

2) Modify your form's data source to join the column txtArtikelStatus is fed from to the ID column from #1 and add the OLE column to the output.

3) Place a "Bound Object Frame" on your form and make its control source property the OLE column from #2.

If everything is ok then you will now see the status colors.

thqanks guys. I still haven't been able to do what I want. But for the time being I adjusted my expectations and just left one of the statuses out so I could use the conditional formatting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top