在 VBA 中设置访问颜色代码

Ste*_*nes 4 ms-access

我在 Access 数据库中设置文本框的背景颜色时遇到问题。我想在满足某些条件时将颜色更改为红色。

在设计视图中,我将文本框的背景颜色属性设置为红色,并显示为“#ED1C24”。当我在“表单”视图中查看表单时,控件正确显示为我选择的红色。

但是,当我将此值放入 VBA 代码中时 (Text1.Backcolor = "#ED1C24"),我收到类型不匹配错误。

我尝试将其更改为十六进制数字(Text1.Backcolor = &HED1C24),但随后控件变为蓝色。

任何帮助,将不胜感激。谢谢。

jhT*_*eny 5

我不久前写了一篇关于这个问题的博客,应该可以回答您的问题。

http://www.jht.co.uk/access-colour-color-codes/

这是代码:

Public Function HTMLColour(HTMLCode As String, Optional Red As Variant, _
Optional Green As Variant, Optional Blue As Variant) As Long
On Error GoTo HTMLColour_Error

'Converts an HTML colour code number to a long interger
'Also returns the constituent R,G & B components through supplied parameters

Dim intR As Integer, intG As Integer, intB As Integer
Dim strHTML As String

'Strip # prefix if supplied
If Len(HTMLCode) < 6 Then Exit Function
strHTML = Right(HTMLCode, 6)

'Extract R, G, B values
intR = CInt("&H" & Mid(strHTML, 1, 2))
intG = CInt("&H" & Mid(strHTML, 3, 2))
intB = CInt("&H" & Mid(strHTML, 5, 2))

'Return optional parameters
If Not IsMissing(Red) Then Red = intR
If Not IsMissing(Green) Then Green = intG
If Not IsMissing(Blue) Then Blue = intB

'Convert RGB to Long integer
HTMLColour = RGB(intR, intG, intB)

HTMLColour_Exit:
Exit Function

HTMLColour_Error:
MsgBox Err.Description, vbExclamation, "Function HTMLColour"
Resume HTMLColour_Exit
End Function
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。