Back color and fore color for list box options (1 Viewer)

Petrosunis

Registered User.
Local time
Today, 13:10
Joined
Mar 8, 2019
Messages
11
The DB was made with MS Access 2010 and contains our local Pool League information.
This is the form code for Week 21 of 30, Round 1 of 5 "Result" list box's I use to input game results. *How you won, or if you lost the game.*
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
The default option is "Choose" which has black back color and white fore color. No change to that.
When "Win" is chosen and clicked on: the back color change to light green(#A7DA4E) and the fore color change to dark green(#22B14C).
When "Loss" is chosen and clicked on: the back color change to (Red Accent2,Lighter60%) and the fore color to change to Red(#ED1C24).
When "Bye" is chosen and clicked on the back color change to (#00B7EF) and the Fore color change to (2F3699).
When "8oB" is chosen and clicked on the back color change to (#FFF200) and the fore color change to (7A4E2B).
When "BaR" is chosen and clicked on the back color change to (PurpleAccent4,Lighter60%) and the fore color to change to (#6F3198).

There is a "Module" in the DB that controls an image that represents the players current rank based on # of wins, and if they are tied with anyone else. But it doesn't do much else. *not sure if a few lines of code in the module code would cover each option OR if each box on the form(s) would have to have code added seperately. I'm a NOOB and have no idea how to make this happen..lol
I have almost no experience with code.
Either way if a solution can be found, I will try to help make it happen on my forms.
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------

Code:
Private Sub W21R1Result_AfterUpdate()

   Select Case Me.W21R1Result

    Case "Choose":
        Me.W21R1Win.Value = 0
        Me.W21R1Loss.Value = 0
        Me.W21R1Bye.Value = 0
        Me.W21R18oB.Value = 0
        Me.W21R1BaR.Value = 0
    Case "Win":
        Me.W21R1Win.Value = 1
        Me.W21R1Loss.Value = 0
        Me.W21R1Bye.Value = 0
        Me.W21R18oB.Value = 0
        Me.W21R1BaR.Value = 0
    Case "Loss":
        Me.W21R1Win.Value = 0
        Me.W21R1Loss.Value = 1
        Me.W21R1Bye.Value = 0
        Me.W21R18oB.Value = 0
        Me.W21R1BaR.Value = 0
    Case "Bye":
        Me.W21R1Win.Value = 1
        Me.W21R1Loss.Value = 0
        Me.W21R1Bye.Value = 1
        Me.W21R18oB.Value = 0
        Me.W21R1BaR.Value = 0
    Case "8oB":
        Me.W21R1Win.Value = 1
        Me.W21R1Loss.Value = 0
        Me.W21R1Bye.Value = 0
        Me.W21R18oB.Value = 1
        Me.W21R1BaR.Value = 0
    Case "BaR":
        Me.W21R1Win.Value = 1
        Me.W21R1Loss.Value = 0
        Me.W21R1Bye.Value = 0
        Me.W21R18oB.Value = 0
        Me.W21R1BaR.Value = 1

    Case Else

        Err.Raise 600, , "Unhandled result"

   End Select
    
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:10
Joined
Sep 21, 2011
Messages
14,235
Well, I would change that code and set the values all to 0 at the start, as you only appear to set 2 controls at the most. Then add the backcolor/forecolours the same way.

You could use this converter.

https://www.excel-pratique.com/en/vba_tricks/hexadecimal-color-function.php
and of course you would have to set the correct control

HTH
Code:
Private Sub W21R1Result_AfterUpdate()

	Me.W21R1Win.Value = 0
    Me.W21R1Loss.Value = 0
    Me.W21R1Bye.Value = 0
    Me.W21R18oB.Value = 0
    Me.W21R1BaR.Value = 0
	
   Select Case Me.W21R1Result

    Case "Choose"
       
    Case "Win"
        Me.W21R1Win.Value = 1
        Me.W21R1Win.Forecolor = hexa_color("#22B14C")
        Me.W21R1Win.Backcolor = hexa_color("#A7DA4E")

    Case "Loss"
        Me.W21R1Loss.Value = 1
    Case "Bye"
        Me.W21R1Win.Value = 1
        Me.W21R1Bye.Value = 1
    Case "8oB"
        Me.W21R1Win.Value = 1
        Me.W21R18oB.Value = 1
    Case "BaR"
        Me.W21R1Win.Value = 1
        Me.W21R1BaR.Value = 1

    Case Else

        Err.Raise 600, , "Unhandled result"

   End Select
    
End Sub
 

CJ_London

Super Moderator
Staff member
Local time
Today, 18:10
Joined
Feb 19, 2013
Messages
16,608
not very clear on what you are trying to achieve or what your question is. Are you saying the code you supplied works or doesn't work? What have the back/fore colors to do with the problem? and how does the image affect what you are trying to do?
 

Petrosunis

Registered User.
Local time
Today, 13:10
Joined
Mar 8, 2019
Messages
11
Sorry I should of been more clear on the problem..
The code i posted works fine inputting the data on the different tables.


How do i get the color of the box to change to Green when i click the result of a win
or red for a loss
or blue for a Bye
or yellow for 8oB
or purple for BaR


after the option has been clicked on and updated in the form
 

Petrosunis

Registered User.
Local time
Today, 13:10
Joined
Mar 8, 2019
Messages
11
let me try and i'll get back to you
like i said im a noob and know very little about code of any sort


Thank you for your time
 

Petrosunis

Registered User.
Local time
Today, 13:10
Joined
Mar 8, 2019
Messages
11
I tried the Hexa_color and got a compile error: sub or function not defined.


the yellow highlight was on the Private sub W21R1 after update for an error at the top

and the text of the Hexa_color was selected..
 

isladogs

MVP / VIP
Local time
Today, 18:10
Joined
Jan 14, 2017
Messages
18,209
I've never heard of 'hexacolor' being allowed
Any of these work: RGB values or vbConstants or long values e.g.

Code:
Me.MembersID.BackColor = RGB(213, 0, 15)
Me.MembersID.ForeColor = vbYellow
Me.LastName.BackColor = 234567

I have a colour converter that may help... http://www.mendipdatasystems.co.uk/colour-converter/4594450413

If you need Me.Requery put it as the final line in your code
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 13:10
Joined
May 21, 2018
Messages
8,525
hexa_color was a function provided in the link.
Code:
Function hexa_color(ByVal hexa) 'Returns -1 in case of error

    'Convert a hexadecimal color to a Color value - Excel-Pratique.com
    'www.excel-pratique.com/en/vba_tricks/hexadecimal-color-function.php

    If Len(hexa) = 7 Then hexa = Mid(hexa, 2, 6) 'If color with #
    
    If Len(hexa) = 6 Then

        num_array = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f")
        
        char1 = LCase(Mid(hexa, 1, 1))
        char2 = LCase(Mid(hexa, 2, 1))
        char3 = LCase(Mid(hexa, 3, 1))
        char4 = LCase(Mid(hexa, 4, 1))
        char5 = LCase(Mid(hexa, 5, 1))
        char6 = LCase(Mid(hexa, 6, 1))
        
        For i = 0 To 15
            If (char1 = num_array(i)) Then position1 = i
            If (char2 = num_array(i)) Then position2 = i
            If (char3 = num_array(i)) Then position3 = i
            If (char4 = num_array(i)) Then position4 = i
            If (char5 = num_array(i)) Then position5 = i
            If (char6 = num_array(i)) Then position6 = i
        Next
        
        If IsEmpty(position1) Or IsEmpty(position2) Or IsEmpty(position3) Or IsEmpty(position4) Or IsEmpty(position5) Or IsEmpty(position6) Then
            hexa_color = -1
        Else 
            hexa_color = RGB(position1 * 16 + position2, position3 * 16 + position4, position5 * 16 + position6)
        End If
        
    Else
        hexa_color = -1
    End If
    
End Function
 

isladogs

MVP / VIP
Local time
Today, 18:10
Joined
Jan 14, 2017
Messages
18,209
Ah yes, sorry. I didn't click the link provided by Gasman
 

Petrosunis

Registered User.
Local time
Today, 13:10
Joined
Mar 8, 2019
Messages
11
You guys are awsome !
We are on the right path for sure.
Its the
Code:
Select Case Me.W21R1Result
box that i want to change.
the win, loss,bye box's do change and work nicely. thanks for that.
But the box i want to change is not changeing.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:10
Joined
Feb 28, 2001
Messages
27,146
I'm not sure about the other things going on here, but I can simplify your life a little.

All of those cases of Me.xxxx.Value can be me.xxxx - because .Value is the default property for any control that HAS a value. I.e. you can omit .Value because it is assumed. Less typing, if nothing else.
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:10
Joined
Sep 21, 2011
Messages
14,235
You guys are awsome !
We are on the right path for sure.
Its the
Code:
Select Case Me.W21R1Result
box that i want to change.
the win, loss,bye box's do change and work nicely. thanks for that.
But the box i want to change is not changeing.

You need to apply the code to the relevant control(s)
 

Petrosunis

Registered User.
Local time
Today, 13:10
Joined
Mar 8, 2019
Messages
11
You need to apply the code to the relevant control(s)


I'm not sure if that is a "format" thing or "input mask" thing


I dont know "where" in the properties of the result box to tell it

if win is clicked to change the box to green or if its a loss then change it to red, ect


is that set on the "Row Source" ?



I'm very new to all of this and doing my best to keep up.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:10
Joined
Feb 28, 2001
Messages
27,146
The simplest way to do this is, in the "case ladder" where you are setting those numbers, add the part that I highlight in blue:

Code:
    Case "Win"
        Me.W21R1Win = 1
        Me.W21R1Loss = 0
        Me.W21R1Bye = 0
        Me.W21R18oB = 0
        Me.W21R1BaR = 0
        Me.LBox.Forecolor = somecolor
        Me.LBox.Backcolor = someothercolor
    Case "Loss"
        Me.W21R1Win = 0
        Me.W21R1Loss = 1
        Me.W21R1Bye = 0
        Me.W21R18oB = 0
        Me.W21R1BaR = 0
        Me.LBox.Forecolor = anothercolor
        Me.Lbox.Backcolor = anotherdifferentcolor

Pick and choose the colors as appropriate. You had a list of colors you wanted to use; that case ladder is where you use them. I.e. the case ladder tested the cases for you and resolved it to one particular case. So once you have decided which case applies, don't only set the numbers - also set the colors at the same time.
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:10
Joined
Sep 21, 2011
Messages
14,235
In the AfterUpdate event of the combo that holds the Win, Loss etc(Me.W21R1Result)?, apply the change of the property to that control that you want the colour changed.

In the previous post I took that to be Me.W21R1Win
Code:
Me.W21R1Win.Forecolor = hexa_color("#22B14C")
Me.W21R1Win.Backcolor = hexa_color("#A7DA4E")
 

Petrosunis

Registered User.
Local time
Today, 13:10
Joined
Mar 8, 2019
Messages
11
Its actually the W21R1Result list box I'm trying to get to change.


I got the color to set on the button. BUT when I scroll from the default choice to "Win"
All of the options are LtGreen back and Green fore. UNTIL i click on an option.

When I click on "Win" the back changes to purple with blue fore AND all of the other options are still Green. Not thier proper colors.


Is there a way to say IF any "Win" is chosen from any "Result" on any of the 24 forms to change to green. or red for "Loss" , ect ?


I'm new to most of this and trying to keep up.


You guys are awsome !
Thank you so much.
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:10
Joined
Feb 28, 2001
Messages
27,146
Is there a way to say IF any "Win" is chosen from any "Result" on any of the 24 forms to change to green. or red for "Loss" , ect ?

The question is worded just vaguely enough that it is unclear in scope. By that I mean, it is possible to read that question as wanting to change ALL controls when ANY of them is set to WIN and it is also possible that you just mean you want all of them to behave the exact same way but one control at a time.

For the latter, I suggest that you take that CASE ladder and put it in a subroutine, then just call that subroutine when you need to treat any of the members of a given control-set the same way. You CAN pass a control to a subroutine and have the subroutine make changes to its properties. Look up ByRef as a keyword for how to do this. Then just call the common subroutine from the appropriate place each time you want to apply that type of change.

IF you place the subroutine in the form's class module, you can still use the "Me." construct and have reasonable expectations of it working. It its possible to put the subroutine in a general module (if more than one form requires the same treatment) but in that case, special considerations apply because "Me." no longer works right.
 

Petrosunis

Registered User.
Local time
Today, 13:10
Joined
Mar 8, 2019
Messages
11
Thanks Doc
It looks like the latter is the case so I'm going to have to do some homework.
 

Users who are viewing this thread

Top Bottom