none
Color Picker for Access Project RRS feed

  • Question

  • I would like to include a control where I can click an open the color picker. After selecting the color from the color picker, the color code (in HTML color code) is saved in a field.

    Is the color picker function available in Access? If not, is there any other solution ?

    Kindly advise.

    Thank you.


    TS Lim

    Monday, February 13, 2012 1:55 AM

Answers

  • Hi TS Lim
     
    You can use the Access internal color dialog as follows:
    (copy code to a new standard module)
     
    Option Compare Database
    Option Explicit
     
    Declare Sub wlib_AccColorDialog _
      Lib "msaccess.exe" _
        Alias "#53" (ByVal Hwnd As Long, lngRGB As Long)
     
    Public Function ChooseWebColor(DefaultWebColor As Variant) As String
      Dim lngColor As Long
      lngColor = CLng("&H" & Right("000000" + _
                      Replace(Nz(DefaultWebColor, ""), "#", ""), 6))
      wlib_AccColorDialog Screen.ActiveForm.Hwnd, lngColor
      ChooseWebColor = "#" & Right("000000" & Hex(lngColor), 6)
    End Function
    Call this function within a form using following code:
     
    Me!txtYourColor = ChooseWebColor(me!txtYourColor)
     
    HTH
    Henry
     

    I would like to include a control where I can click an open the color picker. After selecting the color from the color picker, the color code (in HTML color code) is saved in a field.

    Is the color picker function available in Access? If not, is there any other solution ?

    Kindly advise.

    Thank you.


    TS Lim

    • Marked as answer by TS Lim Monday, February 13, 2012 4:28 AM
    Monday, February 13, 2012 3:25 AM

All replies

  • Hi TS Lim
     
    You can use the Access internal color dialog as follows:
    (copy code to a new standard module)
     
    Option Compare Database
    Option Explicit
     
    Declare Sub wlib_AccColorDialog _
      Lib "msaccess.exe" _
        Alias "#53" (ByVal Hwnd As Long, lngRGB As Long)
     
    Public Function ChooseWebColor(DefaultWebColor As Variant) As String
      Dim lngColor As Long
      lngColor = CLng("&H" & Right("000000" + _
                      Replace(Nz(DefaultWebColor, ""), "#", ""), 6))
      wlib_AccColorDialog Screen.ActiveForm.Hwnd, lngColor
      ChooseWebColor = "#" & Right("000000" & Hex(lngColor), 6)
    End Function
    Call this function within a form using following code:
     
    Me!txtYourColor = ChooseWebColor(me!txtYourColor)
     
    HTH
    Henry
     

    I would like to include a control where I can click an open the color picker. After selecting the color from the color picker, the color code (in HTML color code) is saved in a field.

    Is the color picker function available in Access? If not, is there any other solution ?

    Kindly advise.

    Thank you.


    TS Lim

    • Marked as answer by TS Lim Monday, February 13, 2012 4:28 AM
    Monday, February 13, 2012 3:25 AM
  • Hi Henry,

    It works!   Thank you very much.


    TS Lim

    Monday, February 13, 2012 4:28 AM
  • Very slick. What other useful functions can be exposed ?
    Is there a color picker for Excel as well ?


    • Edited by Syswizard Saturday, February 25, 2012 11:52 PM
    Saturday, February 25, 2012 11:51 PM
  • Duno what else can be exposed and there is always a risk that these exposures won't work in later versions, so I'd not rely on these to much. The Color Picker itself works IIRC since A95 (or was it even A2). I found this exposure long, long time ago in the utilities database (MDA) at the time it's source code was open source by MS. In later version they didn't include this MDAs in open source anymore.

    If there are similar exposures available in Excel? I don't know, maybe you have to ask in the Excel forum.

    Henry

    Monday, February 27, 2012 2:39 AM
  • I am trying to use this but I am receiving the error message "Type Mismatch"

    Forms![CPARSearchTool].Detail.BackColor = ChooseWebColor(Forms![CPARSearchTool].Detail.BackColor)

    Is this because it is in web color format instead of VB?

    Thanks

    Dan

    Friday, November 1, 2013 1:51 PM
  • You can use this to convert Hex Colors to Access Color Code.

    (I made this because using &h frequently FAILS with specific hex codes...)

    Public Function Color(strHex As String) As Long
    
        ' Usage – EG.: txtBox.BacKColor = Color("#ABC123")
        '              txtBox.ForeColor = Color("#000")
        '
        ' Returns – LONG value 0—>16777215 (Black—>White)
    
        If ET Then On Error GoTo ErrorHandler
        
        
        
        'Run length test
        If Len(strHex) <> 4 And Len(strHex) <> 7 Then
            Call Err.Raise(9001, , "Invalid color hex code — must begin with '#' and end with either 3 or 6 characters of [A-F], [a-f], or [0-9]." & _
                                    BR2 & "<b>Example:</b> '#A4B127'  -or-  '#238'")
            GoTo CleanUpAndExit
        End If
        
        
        
        'Run regex test
        Dim regEx As Object
        Set regEx = CreateObject("VBScript.RegExp")
        With regEx
            .IgnoreCase = False
            .Pattern = "#[0-9A-Fa-f]{6}"
            If Not .Test(strHex) Then
                .Pattern = "#[0-9A-Fa-f]{3}"
                If Not .Test(strHex) Then
                    Call Err.Raise(9001, , "Invalid color hex code — must begin with '#' and end with either 3 or 6 characters of [A-F], [a-f], or [0-9]." & _
                                            BR2 & "<b>Example:</b> '#A4B127'  -or-  '#238'")
                    GoTo CleanUpAndExit
                End If
            End If
        End With
        
        
        
        ' Do the conversion
        Dim lngColor As Long
        Dim i As Integer
        
        ' 16 ^   5   4   3   2   1   0
        '      [ B | B | G | G | R | R ]
        '
        ' 16 ^   5   3   1
        '      [ B | G | R ]
        
        Select Case Len(strHex)
        
            Case 7
                strHex = "#" & Mid(strHex, 6, 2) & Mid(strHex, 4, 2) & Mid(strHex, 2, 2) ' <— Convert to #BBGGRR
                For i = 0 To 5
                    lngColor = lngColor + (LVAL(Mid(strHex, 2 + i, 1)) * (16 ^ (5 - i)))
                Next i
            
            Case 4
                strHex = "#" & Mid(strHex, 4, 1) & Mid(strHex, 3, 1) & Mid(strHex, 2, 1) ' <— Convert to #BGR
                For i = 0 To 2
                    lngColor = lngColor + (LVAL(Mid(strHex, 2 + i, 1)) * (16 ^ (5 - (2 * i))))
                    lngColor = lngColor + (LVAL(Mid(strHex, 2 + i, 1)) * (16 ^ (5 - ((2 * i) + 1))))
                Next i
        
        End Select
        
        
        
        ' Dump the result
        Color = lngColor
        
    CleanUpAndExit:
        On Error Resume Next
        
        Set regEx = Nothing
        
        Exit Function
    
    ErrorHandler:
        Color = -1
        Call ErrorHandler(Err)
        Resume CleanUpAndExit
    
    End Function
    
    
    
    Public Function LVAL(ByVal C As String) As Long
    
        ' Takes in one character string from Color function;
        ' Returns 10-15 for "A" - "F" (including lowercase),
        ' or 0-9 for "0" - "9".
        
        On Error Resume Next
    
        Select Case Asc(C)
        
            Case Asc("A") To Asc("Z")
            
                LVAL = Asc(C) - (Asc("A") - 10)
            
            Case Asc("a") To Asc("z")
            
                LVAL = Asc(C) - (Asc("a") - 10)
            
            Case Asc("0") To Asc("9")
            
                LVAL = Asc(C) - (Asc("0"))
        
        End Select
    
    End Function



    • Edited by djdave238 Thursday, April 6, 2017 6:36 PM
    Thursday, April 6, 2017 6:32 PM
  • Hi djdave -- how would you call your function into the previous one on this post -- would you replace the &H call with your color call?

    Sunday, August 20, 2017 1:18 PM
  • i have use it with another function to convert it to RGB so it works good In the VBA Code

    like this   

    And Also With a button to select a text box then it fills in the color and the Color # as you type in to the vba code

    (look down for , Private Sub Command17_Click() ) 

    Public Function ChooseWebColor_FrVBA_RGB(DefaultWebColor As Variant) As String
      Dim lngColor As Long
      lngColor = CLng("&H" & Right("000000" + Replace(Nz(DefaultWebColor, ""), "#", ""), 6))

      wlib_AccColorDialog Screen.ActiveForm.Hwnd, lngColor

     ChooseWebColor_FrVBA_RGB = "#" & Right("000000" & Hex(lngColor), 6)
     ChooseWebColor_FrVBA_RGB = Color_Hex_To_Long(ChooseWebColor_FrVBA_RGB)
    End Function
    Public Function Color_Hex_To_Long(strColor As String) As Long
        Dim iRed As Integer
        Dim iGreen As Integer
        Dim iBlue As Integer

        strColor = Replace(strColor, "#", "")
        strColor = Right("000000" & strColor, 6)
        iBlue = Val("&H" & Mid(strColor, 1, 2))
        iGreen = Val("&H" & Mid(strColor, 3, 2))
        iRed = Val("&H" & Mid(strColor, 5, 2))

        Color_Hex_To_Long = RGB(iRed, iGreen, iBlue)
    End Function

    'here is how you use it under a button 

    Private Sub Command17_Click()
    'On Error Resume Next

    Dim slctdctrl  As String
    slctdctrl = Screen.PreviousControl.Name
    Me.Controls(slctdctrl).BackColor = ChooseWebColor_FrVBA_RGB(Me.Controls(slctdctrl))
    Me.Controls(slctdctrl) = Me.Controls(slctdctrl).BackColor

    End Sub

    But one problem i have when a button was PreviousControl then your function Generate an error and i would like to use it on buttons to 

    And The main problem is that i cant save the color's on controls on the form 

    i just got the RGB numbers to copy it in my code



    • Edited by Ben Programer Wednesday, May 9, 2018 8:08 PM extere word
    Wednesday, May 9, 2018 8:06 PM