Access VBA Macro Programming

(Joao Candeias) #1

The first thing you need to do is enter the module for the form you have created by
right-clicking the form, selecting Build Event from the pop-up menu, and then choosing
Code Builder. This will take you into the VBE window for that form.
Enter the following code into the module:


Private Declare Function ChooseColor
Lib "comdlg32.dll" Alias "ChooseColorA"

(pChoosecolor As COLORSTRUC) As Long


Private Type COLORSTRUC
lStructSize As Long
hwnd As Long
hInstance As Long
rgbResult As Long
lpCustColors As String
Flags As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type


This code creates the declaration for the API call ChooseColor and creates a data type
called COLORSTRUC to hold the parameters to display the Color dialog.
You then need to add code to create a procedure to display the Color dialog and to return
the color selected:


Private Sub ShowColorDialog()
Dim x As Long, CSel As COLORSTRUC, CustColor( 16 ) As Long


CSel.lStructSize = Len(CSel)
CSel.hwnd = Me.hwnd
CSel.Flags = &H8 0
CSel.lpCustColors = String$( 16 * 4, 0 )
x = ChooseColor(CSel)
If x = 0 Then
MsgBox "The user pressed Cancel"
Else

MsgBox CSel.rgbResult
End If

End Sub


Notice that the handle property (hwnd) of your form is used to populate CSel.hwnd. This
is why the API call has to work from a form or report.


Chapter 10: Common Dialog Control 127

Free download pdf