First Published 31 Jan 2025
The ShowColorDialog function is used to display a standard Windows color selector dialog for use in any Access application.
There are 2 versions of the color dialog - basic and custom. From the basic version, click Define Custom Colors to open the custom dialog with additional colors available.
Basic Color Dialog
![]() |
Custom Color Dialog
![]() |
NOTE: Although I am using US spellings in this article, my Windows language is UK English so my color dialogs are localized to show the UK English spelling: Colour
Choose a basic or custom color then click OK to import this into your application
To use this color dialog, first copy all the code below to a standard module. The standard OLE Automation reference is REQUIRED.
CODE:
Option Compare Database
Option Explicit
Private Type ChooseColorStruct
lStructSize As Long
hwndOwner As LongPtr
hInstance As LongPtr
rgbResult As Long
lpCustColors As LongPtr
flags As Long
lCustData As LongPtr
lpfnHook As LongPtr
lpTemplateName As String
End Type
Private Declare PtrSafe Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" _
(pChoosecolor As ChooseColorStruct) As LongPtr
Private Declare PtrSafe Function OleTranslateColor Lib "oleaut32.dll" (ByVal lOleColor _
As Long, ByVal lHPalette As Long, lColorRef As Long) As Long
' Define constants
Private Const CC_RGBINIT = &H1&
Private Const CC_FULLOPEN = &H2&
Private Const CC_PREVENTFULLOPEN = &H4&
Private Const CC_SHOWHELP = &H8&
Private Const CC_ENABLEHOOK = &H10&
Private Const CC_ENABLETEMPLATE = &H20&
Private Const CC_ENABLETEMPLATEHANDLE = &H40&
Private Const CC_SOLIDCOLOR = &H80&
Private Const CC_ANYCOLOR = &H100&
Private Const CLR_INVALID = &HFFFF
' Show the common dialog for choosing a color.
' Return the chosen color, or -1 if the dialog is canceled
' hParent is the handle of the parent form
' bFullOpen specifies whether the dialog will be open with the Full style
' (allows to choose many more colors)
' InitColor is the color initially selected when the dialog is open
' Example:
' Dim oleNewColor As OLE_COLOR
' oleNewColor = ShowColorsDialog(Me.hwnd, True, vbRed)
' If oleNewColor <> -1 Then Me.BackColor = oleNewColor
'---------------------------------------------
Function ShowColorDialog(Optional ByVal hParent As LongPtr, _
Optional ByVal bFullOpen As Boolean, Optional ByVal InitColor As OLE_COLOR) As Long
Dim CC As ChooseColorStruct
Dim aColorRef(15) As Long
Dim lInitColor As Long
On Error GoTo Err_Handler
' translate the initial OLE color to a long value
If InitColor <> 0 Then
If OleTranslateColor(InitColor, 0, lInitColor) Then
lInitColor = CLR_INVALID
End If
End If
'fill the ChooseColorStruct struct
With CC
.lStructSize = LenB(CC)
.hwndOwner = hParent
.lpCustColors = VarPtr(aColorRef(0))
.rgbResult = lInitColor
.flags = CC_SOLIDCOLOR Or CC_ANYCOLOR Or CC_RGBINIT Or IIf(bFullOpen, CC_FULLOPEN, 0)
End With
' Show the dialog
If ChooseColor(CC) Then
'if not canceled, return the color
ShowColorDialog = CC.rgbResult
Else
'else return -1
ShowColorDialog = -1
End If
Exit_Handler:
Exit Function
Err_Handler:
MsgBox "Error " & Err & " in ShowColorDialog procedure: " & Err.Description
GoTo Exit_Handler
End Function
Typical usage:
Run the above code from a textbox or command button. In this example, the forecolor and backcolor of a textbox are specified using the color dialog:
Private Sub cmdFillColor_Click()
'Show color dialog for fill color
Dim oleNewColor As OLE_COLOR
oleNewColor = ShowColorDialog(Me.Hwnd, True, vbBlue)
If oleNewColor <> -1 Then
Me.txtFillColor.BackColor = oleNewColor
Me.txtFillColor.ForeColor = Me.txtFillColor.BackColor
End If
End Sub
Using True in the second argument e.g. ShowColorDialog(Me.Hwnd, True, vbBlue) will open direct to the Custom Colors dialog.
Setting that argument to False will open the Basic Colors dialog.
For additional usage examples, see my articles: Gradient Background Fill on Forms and Modern Charts Improvements: Part 3 - Use VBA Code to set Chart Properties
Download
Click to download the module code as a text file:
modColorChooser.bas.txt
Rename (removing the .txt suffix) and import into your application by clicking File . . . Import File in the Visual Basic Editor
Feedback
Please use the contact form below to let me know whether you found this article interesting/useful or if you have any questions/comments.
Please also consider making a donation towards the costs of maintaining this website. Thank you
Colin Riddington Mendip Data Systems Last Updated 31 Jan 2025
Return to Code Samples Page
|
Return to Top
|