If you make use of ActiveControl, there really is no need to know which
form. I use a common calendar form for all date fields. I also do some
cool things with API functions to make it look like the popup calendar is a
drop down from a given date field....I've left the API calls out though
because they're somewhat involved.
You might also consider using hot keys in the KeyPress event to insert or
update the field, i.e., t/T for Today, +/- to add/subtract, y/Y to
add/subtract a year, etc.. . The hot keys could also open the popup
calendar. Code and some comments follow:
'In your calendar form, create a property that accepts a control as a
parameter. As follows:
Private mctl As Control
Public Property Set ActiveDateControl(RHS As Control)
On Error Resume Next
Set mctl = RHS
If IsNothing(mctl.Value) = False Then
Me.ocxDate.Date = mctl.Value
End If
End Property
Public Function PickNewDate(KeyAscii As Integer, ActiveControl As Control)
As Integer
' Comments : Picks a new date to use for the control value
' This function is normally used as part of a control's KeyPress
event
' and looks for the Ascii value of T, t, +, or - to determine
which date to return.
' T/t = Today's date
' + = Increment the control's current date by 1
' - = Decrement the control's current date by 1
' Parameters: KeyAscii - Integer - the Ascii value of the key
pressed
' ActiveControl - Control - The control that will receive the
new value
' Returns : Integer - 0 if the control was assigned a new date, or the
value passed for KeyAscii if not
On Error Resume Next
'Check for value in control
If IsNothing(ActiveControl) Then
'No value...return today's date if one of the special keys was
pressed
Select Case KeyAscii
Case 116, 84, 43, 45, 72, 104, 62, 60, 89, 121
ActiveControl = Date
PickNewDate = 0
Case 67, 99
Call SetDateFromPopup(ActiveControl)
PickNewDate = 0
Case Else
PickNewDate = KeyAscii
End Select
ElseIf IsDate(ActiveControl) Then
'Has value
'Return date and time based on which special key was pressed
Select Case KeyAscii
Case 116, 84
ActiveControl = Date
PickNewDate = 0
Case 43
ActiveControl = ActiveControl + 1
PickNewDate = 0
Case 45
ActiveControl = ActiveControl - 1
PickNewDate = 0
Case 72
ActiveControl = ActiveControl + (1 / 24)
PickNewDate = 0
Case 89
ActiveControl = DateSerial(Year(ActiveControl) + 1,
Month(ActiveControl), Day(ActiveControl))
PickNewDate = 0
Case 121
ActiveControl = DateSerial(Year(ActiveControl) - 1,
Month(ActiveControl), Day(ActiveControl))
PickNewDate = 0
Case 104
ActiveControl = ActiveControl - (1 / 24)
PickNewDate = 0
Case 62
ActiveControl = ActiveControl + (1 / 1440)
PickNewDate = 0
Case 60
ActiveControl = ActiveControl - (1 / 1440)
PickNewDate = 0
Case 67, 99
Call SetDateFromPopup(ActiveControl)
PickNewDate = 0
Case Else
PickNewDate = KeyAscii
End Select
Else
PickNewDate = KeyAscii
End If
End Function
Sub SetDateFromPopup(ctl As Control)
'You call this from your double click event or the routine above
Dim frm As Form_fdlgCalendar
On Error Resume Next
'Open the form hidden while we get it set up
DoCmd.OpenForm "fdlgCalendar", , , , , acHidden
Set frm = Forms("fdlgCalendar")
Set frm.ActiveDateControl = ctl
'Make pop-up visible
frm.Visible = True
End Sub
Private Sub txtDateReceived_KeyPress(KeyAscii As Integer)
'This is an example of the KeyPress event code
On Error Resume Next
KeyAscii = picknewdate(KeyAscii, Me.ActiveControl)
End Sub