Setting Back Colour to Hex Value in Table Field

Alright, I've attempted to use this code, but it's not happening and I can't see what I'm doing wrong. Any hints?

Code:
Box18.BackColor = rgb(CInt(Mid(Me.rgb, 2, 2)), CInt(Mid(Me.rgb, 4, 2)), CInt(Mid(Me.rgb, 6, 2)))
 
Okay, now I've got the following, but Visual Basic isn't happy with the last line, and I'm sure it's just because I don't know the correct command/syntax to achieve what I'm attempting, but it will likely me most obvious! :-)

Code:
Private Sub Form_Current()
    Red = Val("&H" & Mid(Me.rgb, 1, 2))
    Green = Val("&H" & Mid(Me.rgb, 3, 2))
    Blue = Val("&H" & Mid(Me.rgb, 5, 2))
 
    HEXCOL2RGB = rgb(Red, Green, Blue)
 
    Box18.BackColor = HEXCOL2RGB
 
End Sub
 
Code:
Box18.BackColor = rgb(CInt(Mid(Me.rgb, 2, 2)), CInt(Mid(Me.rgb, 4, 2)), CInt(Mid(Me.rgb, 6, 2)))

What type of number is "me.rgb"? If it is a long integer (11323658) it won't work. It has to be a hexadecimal number (#00B95C). Not only that, but it must in the format #RRGGBB. A "regular" hexadecimal will not work. A "regular" hexadecimal value will not work because the "number" you are using really consists of three discrete numbers.

Either do what VBAInet suggests Convert a hex color string to an rgb color or use:
Code:
Me.Detail.BackColor = HTMLConvert("#D8B190")

Code:
Public Function HTMLConvert(strHTML As String) As Long
    Rem converts a HTML color code number such as #D8B190 to an RGB value.
    HTMLConvert = RGB(CInt("&H" & Mid(strHTML, 2, 2)), CInt("&H" & Mid(strHTML, 4, 2)), CInt("&H" & Mid(strHTML, 6, 2)))
End Function

I see that while I was preparing this response, that you also posted. Have you, assuming that your number is correctly formatted, skipped the "#" sign?
 
Last edited:
There you go RWRinn. Just tested it with your db.

Code:
Box18.BackColor = rgb(Val("&H" & Mid(Me.rgbColor, 1, 2)), Val("&H" & Mid(Me.rgbColor, 3, 2)), Val("&H" & Mid(Me.rgbColor, 5, 2)))
Change the name of your rgb field to rgbColor. It's clashing with the function. Or find attached.

Just noticed you replied Steve. I've just tweaked yours to make the above code. RWRinn's color codes are not prefixed with the "#" character.
 

Attachments

Users who are viewing this thread

Back
Top Bottom