SetFocus in If Statement

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Will someone please explain why the Setfocus in the MIInit sub respnds with
an error can't move the f0ocus to the control MIDate.

The intent is to force the user date to enter a correct date.

Private Sub MIDate_GotFocus()
Me.ThisCntrol = Me.ActiveControl.Name
Me.MyCalendar = Date
Me.MyCalendar.Visible = True
Me.MyCalendar.SetFocus
End Sub

Private Sub MIInit_GotFocus()
Me.MyCalendar.Visible = False
If Me.MIDate < Me.IssueDate Then
MsgBox "Submit Date " & Me.MIDate & " Is less than Issue Date of " &
Me.IssueDate, 16, "Critical Error"
Me.MIDate.SetFocus
End If
End Sub


TIA

Lee
 
The appears fine. I would suggest though using the AfterUpdate event of
MIDate to handle the validation. Since theoretically its entirely
possible for the user to enter a value and then click on a different
control instead of using the tab key.
 
DUH! Stupid me. What exactly is the MyCalendar control and its
relationship to MIDate? I've found the majority of my problems using
setFocus have to do with the fact that the control is not visible.
 
Thanks David.

As you suspected, the calendar control is the ActiveX control, which does
not, because it set values from VBA code, trigger the AfterUpdate event.
That's why I'm using the next tabbed field's GotFocus.

I'm passing the active control name to the calendar code to set date in 4
different fields from a single calendar control.

I have replicated the code MiDate and MIInit to the three other sets of
controls and they all fail the same way. At least it's consistent.

Here's the calendar control code, as well.

Private Sub MyCalendar_Click()
Me.Controls(Me.ThisCntrol).Value = Me.MyCalendar
End Sub


Any further thoughts?
 
1. I would spin out the code to a single sub and call it when neccessary
as in

Sub control1_event()
call validate
end sub
Sub control2_event()
call validate
end sub
Sub validate()
end sub

2. What is the EXACT error message being given?

3. Try Me!MyCalendar.SetFocus. My mind blurs when it comes to BANGS and
DINGS, but I vaguely remember something that makes me think that that
might be the problem.
 
Thanks David.
1. Remember that each of the 4 date validators, if invalid, need to return
to the input control to enter the valid date. I can't just return in line,
that's why I got into this mess.

2. "Can't move focus to control Me.MIDate"

3. The problem is not in passing control to the ActiveX control. That works
just fine. The problem is passing control from the date validator to the
date receiver that's causing the problem.

I have traced the logic with MsgBox statements and the code operates this way.

First control is passed to the date receiver, like the one below

Private Sub MIDate_GotFocus()
Me.ThisCntrol = Me.ActiveControl.Name
Me.MyCalendar = Date
Me.MyCalendar.Visible = True
Me.MyCalendar.SetFocus
End Sub

Then it makes the calendar visible and goes to the ActiveX calendar, with
the Me.MyCalendar.SetFocus

Private Sub MyCalendar_Click()
MsgBox "At MyCalendar Click"
Me.Controls(Me.ThisCntrol).Value = Me.MyCalendar
End Sub

The date is set and control returns to the "end sub" following the
Me.MyCalendar.Setfocus, which exits the sub and return control to the next
tabbed field, which is the validator, Me.MIInit.

Then the validator gets control, finds the error and passes control back to
the date receiver, Me.MIDate, which gets control a second time.
Private Sub MIInit_GotFocus()
Me.MyCalendar.Visible = False
If Me.MIDate < Me.IssueDate Then
MsgBox "Submit Date " & Me.MIDate & " Is less than Issue Date of " &
Me.IssueDate, 16, "Critical Error"
'Me.MIDate.SetFocus
End If
End Sub

It's this last SetFocus that fails trying to pass control to the date
receiverwhich is Me.MIDate, again.

Theoretically. it should stay in this loop until the date is correct.

Does that help you see it clearer?

Lee
 
Ok, try snooping around by creating a command button with the statement

Me.MIDate.SetFocus

in the SUB. It *SHOULD* set the focus to the MIDate. I suggest it
because one of the key things in Debugging is to isolate where the
problem is. But what stumps me is that as long as the control is visible
you should be able to move the focus there. I'm assuming that you don't
have anything funky going on like having the control disabled.
 
Thanks David,

I created the Command Button "PushMe".

Here's the code:
Private Sub PushMe_Enter()
MsgBox "Entered PushMe"
Me.MIDate.SetFocus
MsgBox "Returned from MIDate GotFocus"
End Sub

Here's the code for MIDate.
Private Sub MIDate_GotFocus()
MsgBox "Arrived at MIDate GotFocus"
Me.ThisCntrol = Me.ActiveControl.Name
Me.MyCalendar = Date
Me.MyCalendar.Visible = True
Me.MyCalendar.SetFocus
MsgBox "returned from MyCalendar"
End Sub

Here is the message stream.
"Entered PushMe"
"Arrived at MIDate GotFocus"
"Returned from MyCalendar"
Error -> Can't move the focus to the control MIDate

The error shows at the Me.MIDate.SetFocus in the MIDate_GotFocus Sub

What's going on here? The MsgBox within MIDate_GotFocus was executed. and
the MSGBox was executed on return. Why does the system report that it can't
move the focus when it already had?

Lee
 
Probably should have clarified this, the command button code was to
isolate the .SetFocus statement from everything else. The idea was for
you click the button and observe the focus being set to the MIDate
control. It was only intended as a test.

Did you try that?

Second to that, and after I posted the last email, I occurred to me that
the GotFocus and LostFocus events happen sequentially with other events.
For example if you're in the control txtName (assuming that you update
it and then tab) the following events occurr

txtName BeforeUpdate
txtName AfterUpdate
txtName LostFocus
txtNextControl GotFocus

Do you have any other code in the module?
 
David,

I found a clue. The problem seems to be that when Calendar_OnClick exits,
control is given to the next tabbed field. In this case the next tab order
was MIDate which gave up control back to the OnClick. MIDate had never
executed it's END SUB so was still, in a manner of speaking, still in focus.

I tried chaning the tab order so that the calendar was the last, but then
when it exited it just created a new record. I turned the tab stop property
Off but it made no difference.

It seems as though I have to "Know" what control to give focus to when I
leave the on click.

Does that make sense to you?

Lee
 
I tried chaning the tab order so that the calendar was the last, but >
then when it exited it just created a new record. I turned the tab
stop property Off but it made no difference.

When you did this did the code work properly other than a new record
being created? Also, I'm assuming that the Calendar that you mentioned
is an ActiveX Control?
 
Back
Top