Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Errors When Taking Focus From Calendar Control

Status
Not open for further replies.

kshadden

IS-IT--Management
Jun 18, 2003
19
US
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
 
Check this site.


Hopefully, it will give you what you are looking to do.


An investment in knowledge always pays the best dividends.
by Benjamin Franklin
 
It *may* be that Access is smart enough to prevent you from creating an endless loop, but I'm not certain of that. In any event, if Access didn't bark at you, your code could potentially set up a loop, or at least undesirable behavior. Think about your flow of control for a moment... As soon as the cursor enters the textbox, you're telling it to go to the calendar control. As soon as you click in the calendar control, you're sending the cursor back to the textbox. What's going to happen when the cursor gets there? Bingo, right back to the calendar. I think that's why Martin Green's demo database (which it appears you've based your code on) used a combo and put the code on the Mouse Down event. BTW, the code works fine if you put it on the Mouse Down event for a textbox - I expect he chose a combo as that would be a more intuitive mouse-click for the user.

HTH...

Ken S.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top