Here's my method using a form frmCalendar with a calendar control ocxCalendar:
1. Add the following function to a standard module:
Public Function OpenCalendar(strCurrentForm As String, _
strCurrentControl As String) As Boolean
On Error GoTo ErrHandler
OpenCalendar = True
DoCmd.OpenForm "frmCalendar", WindowMode:=acDialog, _
OpenArgs:=strCurrentForm & "/" & strCurrentControl
ExitHere:
Exit Function
ErrHandler:
OpenCalendar = False
Resume ExitHere
End Function
2. In the calendar form's Close event procedure out:
Dim intSlashPos As Integer
Dim strForm As String, strControl As String
' parse the form's OpenArgs property
' to get names of calling form and control
If Len(Me.OpenArgs) > 0 Then
intSlashPos = InStr(1, Me.OpenArgs, "/")
strForm = Left$(Me.OpenArgs, intSlashPos - 1)
strControl = Mid$(Me.OpenArgs, intSlashPos + 1)
Forms(strForm).Controls(strControl) = ocxCalendar
End If
3. In the calendar control's Click event procedure put:
DoCmd.Close acForrm, Me.Name
NB: the Click event procedure is not shown in the control's properties
sheet, but is available in the VBA window with the form's module open by
selecting the calendar control from the top left combo box and its Click
event procedure from the top right combo box.
4. To open the calendar form put the following code in a suitable event
procedure in any form with a control into which you want to insert the date;
I generally use the date control's DblClick event procedure:
If Not OpenCalendar(Me.Name, "YourDateControl") Then
MsgBox "Unable to open calendar", vbExclamation, "Error"
End If
where " YourDateControl" is the name of the date control
Ken Sheridan
Stafford, England