简体   繁体   中英

How to set background of a form to a hexidecimal color in MS-Access VBA

I am looking to set the background of a form to a hex value given a condition.

Private Sub Form_Load()
    If Not IsNull(Me.SeparationDate) Then Me.Detail.BackColor = Val("&H" & "ff1111")
End Sub

Problem is that with this condition my background is turning a dark blue color and not an off red.

The value needs to be GBR

&H1111ff

There is also a RGB() function

RGB(255,17,17) 'Same as &H1111ff

Say you have

s = "ff1111"

Then you can do

Me.Detail.BackColor = RGB("&H" & Left$(s, 2), _
                          "&H" & Mid$(s, 3, 2), _
                          "&H" & Right$(s, 2))

or simply

Me.Detail.BackColor = "&H" & Right$(s, 2) & Mid$(s, 3, 2) & Left$(s, 2)

According to @MathieuGuindon the Val function is not required as VBA understands hex strings and automatically converts them to Long .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM