I have added a calendar control to one of my forms. It is attached to a field called medication_start_date. When I enter the field the calendar control becomes visible and has focus. When I click on a date to populate the field with the selected date the calendar is suppose to lose focus, give focus to the date field, and then hide. When I click the date on the calendar I get a runtime error message that says, "Microsoft Access can't move the focus to the control Medication_Start_Date. Below is my code. What am I doing wrong?
Option Compare Database
Option Explicit
Dim Dateupdate As TextBox
Private Sub Medication_Start_Date_Enter()
'Note which text box called the calendar
Set Dateupdate = Medication_Start_Date
'Unhide the calendar and give it focus
ocxCalendar.Visible = True
ocxCalendar.SetFocus
'Match calendar date to existing date if present or today's date
If Not IsNull(Dateupdate) Then
ocxCalendar.Value = Dateupdate.Value
Else
ocxCalendar.Value = Date
End If
End Sub
Private Sub ocxCalendar_Click()
'Copy chosen date from calendar to originating text box
Dateupdate.Value = ocxCalendar.Value
'Return the focus to the text box and hide the calendar
Dateupdate.SetFocus
ocxCalendar.Visible = False
'Empty the variable
Set Dateupdate = Nothing
End Sub
Option Compare Database
Option Explicit
Dim Dateupdate As TextBox
Private Sub Medication_Start_Date_Enter()
'Note which text box called the calendar
Set Dateupdate = Medication_Start_Date
'Unhide the calendar and give it focus
ocxCalendar.Visible = True
ocxCalendar.SetFocus
'Match calendar date to existing date if present or today's date
If Not IsNull(Dateupdate) Then
ocxCalendar.Value = Dateupdate.Value
Else
ocxCalendar.Value = Date
End If
End Sub
Private Sub ocxCalendar_Click()
'Copy chosen date from calendar to originating text box
Dateupdate.Value = ocxCalendar.Value
'Return the focus to the text box and hide the calendar
Dateupdate.SetFocus
ocxCalendar.Visible = False
'Empty the variable
Set Dateupdate = Nothing
End Sub