> What I want to do is have the user click a button which makes the
calendar visible. Then when the user clicks on a date, it sends it to
a text box and then the calendar should disappear.
I encountered this exact same issue. I put the calendar control in a popup
form and passed a comma-delimited string in OpenArgs:
[frmPopupCalenda r]
Option Compare Database
Option Explicit
Private m_astrOpenArgs( ) As String
Private Sub Form_Open(Cance l As Integer)
m_astrOpenArgs = Split(Me.OpenAr gs, ",")
End Sub
I have a number of forms/subforms in my app that use the calendar control.
Each has a button that opens the popup form with an OpenArgs parameter like
this:
"frmFormName,fr mSubFormName,fr mNestedSubFormN ame,txtControlN ame,strPopupFor mCaption"
If a parameter is not needed, I just leave it blank:
"frmMain,txtMyD ate,,,,Please Select a Transaction Date"
In the AfterUpdate event of the Calendar control, I have this:
Private Sub MyCalendarContr ol_AfterUpdate( )
Call SetDateOnClose( m_astrOpenArgs( 0), m_astrOpenArgs( 1), _
m_astrOpenArgs( 2), m_astrOpenArgs( 3))
DoCmd.Close acForm, "frmPopupCalend ar"
End Sub
Here is SetDateOnClose:
Private Sub SetDateOnClose( strFrm As String, strCtl As String, _
strSubFrm As String, strSubFrmCtl As String)
If Len(strSubFrmCt l) = 0 Then
If Len(strSubFrm) = 0 Then
Forms(strFrm).C ontrols(strCtl) = _
CDate(Me!MyCale ndarControl.Val ue & " 12:00:01 AM")
Else
Forms(strFrm).C ontrols(strCtl) .Form.Controls( strSubFrm) = _
CDate(Me!MyCale ndarControl.Val ue & " 12:00:01 AM")
End If
Else
Forms(strFrm).C ontrols(strCtl) .Form.Controls _
(strSubFrm).For m.Controls(strS ubFrmCtl) = _
CDate(Me!MyCale ndarControl.Val ue & " 12:00:01 AM")
End If
End Sub
The only reason I append "12:00:01 AM" is because I have logic elsewhere
that needs a date with a time value.
I often want the calendar to display a particular date on open - usually the
date that's in the control on the form from which the calendar was opened:
Private Sub Form_Load()
Me!ApptCal.Valu e = GetDateOnLoad(m _astrOpenArgs(0 ), m_astrOpenArgs( 1), _
m_astrOpenArgs( 2), m_astrOpenArgs( 3))
Me.Caption = m_astrOpenArgs( 4)
End Sub
Private Function GetDateOnLoad(s trFrm As String, strCtl As String, _
strSubFrm As String, strSubFrmCtl As String) As Date
On Error GoTo HandleErr
If Len(strSubFrmCt l) = 0 Then
If Len(strSubFrm) = 0 Then
Me!ApptCal.Valu e = Nz(Forms(strFrm ).Controls(strC tl), Date)
Else
Me!ApptCal.Valu e = Nz(Forms(strFrm ).Controls(strC tl).Form. _
Controls(strSub Frm), Date)
End If
Else
Me!ApptCal.Valu e = Nz(Forms(strFrm ).Controls(strC tl).Form.Contro ls _
(strSubFrm).For m.Controls(strS ubFrmCtl), Date)
End If
End Function